Skip to content

Commit d5363c1

Browse files
authored
move sleep routines from Date to independent-routines (#4644)
1 parent 4abd334 commit d5363c1

File tree

2 files changed

+121
-121
lines changed

2 files changed

+121
-121
lines changed

doc/Type/Date.rakudoc

Lines changed: 0 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -218,127 +218,6 @@ Available as of release 2023.02 of the Rakudo compiler.
218218

219219
=head1 Functions
220220

221-
=head2 sub sleep
222-
223-
sub sleep($seconds = Inf --> Nil)
224-
225-
Attempt to sleep for the given number of C<$seconds>. Returns L<C<Nil>|/type/Nil> on
226-
completion. Accepts L<C<Int>|/type/Int>, L<C<Num>|/type/Num>, L<C<Rat>|/type/Rat>, or L<C<Duration>|/type/Duration> types as an
227-
argument since all of these also do L<C<Real>|/type/Real>.
228-
229-
=for code
230-
sleep 5; # Int
231-
sleep 5.2; # Num
232-
sleep (5/2); # Rat
233-
sleep (now - now + 5); # Duration
234-
235-
It is thus possible to sleep for a non-integer amount of time. For
236-
instance, the following code shows that C<sleep (5/2)> sleeps for 2.5
237-
seconds and C<sleep 5.2> sleeps for 5.2 seconds:
238-
239-
my $before = now;
240-
sleep (5/2);
241-
my $after = now;
242-
say $after-$before; # OUTPUT: «2.502411561␤»
243-
244-
$before = now;
245-
sleep 5.2;
246-
$after = now;
247-
say $after-$before; # OUTPUT: «5.20156987␤»
248-
249-
=head2 sub sleep-timer
250-
251-
sub sleep-timer(Real() $seconds = Inf --> Duration:D)
252-
253-
This function is implemented like C<sleep>, but unlike the former it does
254-
return a L<C<Duration>|/type/Duration> instance with the number of seconds the system did not
255-
sleep.
256-
257-
In particular, the returned L<C<Duration>|/type/Duration> will handle the number of seconds remaining
258-
when the process has been awakened by some external event (e.g., Virtual Machine
259-
or Operating System events).
260-
Under normal condition, when sleep is not interrupted, the returned L<C<Duration>|/type/Duration>
261-
has a value of C<0>, meaning no extra seconds remained to sleep.
262-
Therefore, in normal situations:
263-
264-
say sleep-timer 3.14; # OUTPUT: «0␤»
265-
266-
The same result applies to edge cases, when a negative or zero time to sleep
267-
is passed as argument:
268-
269-
=begin code
270-
say sleep-timer -2; # OUTPUT: 0
271-
say sleep-timer 0; # OUTPUT: 0
272-
=end code
273-
274-
See also L<sleep-until|/routine/sleep-until>.
275-
276-
=head2 sub sleep-until
277-
278-
sub sleep-until(Instant $until --> Bool)
279-
280-
Works similar to C<sleep> but checks the current time and keeps sleeping
281-
until the required instant in the future has been reached.
282-
It uses internally the C<sleep-timer> method in a loop to ensure that,
283-
if accidentally woken up early, it will wait again for the specified
284-
amount of time remaining to reach the specified instant.
285-
goes back to sleep
286-
287-
Returns C<True> if the L<C<Instant>|/type/Instant> in the future has been achieved (either
288-
by mean of sleeping or because it is right now), C<False> in the case
289-
an L<C<Instant>|/type/Instant> in the past has been specified.
290-
291-
To sleep until 10 seconds into the future, one could write something like this:
292-
293-
say sleep-until now+10; # OUTPUT: «True␤»
294-
295-
Trying to sleep until a time in the past doesn't work:
296-
297-
my $instant = now - 5;
298-
say sleep-until $instant; # OUTPUT: «False␤»
299-
300-
However if we put the instant sufficiently far in the future, the sleep
301-
should run:
302-
303-
=for code
304-
my $instant = now + 30;
305-
# assuming the two commands are run within 30 seconds of one another...
306-
say sleep-until $instant; # OUTPUT: «True␤»
307-
308-
To specify an exact instant in the future, first create a L<C<DateTime>|/type/DateTime> at the
309-
appropriate point in time, and cast to an L<C<Instant>|/type/Instant>.
310-
311-
=for code
312-
my $instant = DateTime.new(
313-
year => 2023,
314-
month => 9,
315-
day => 1,
316-
hour => 22,
317-
minute => 5);
318-
say sleep-until $instant.Instant; # OUTPUT: «True␤» (eventually...)
319-
320-
This could be used as a primitive kind of alarm clock. For instance, say
321-
you need to get up at 7am on the 4th of September 2015, but for some reason
322-
your usual alarm clock is broken and you only have your laptop. You can
323-
specify the time to get up (being careful about time zones, since
324-
C<DateTime.new> uses UTC by default) as an L<C<Instant>|/type/Instant> and pass this to
325-
C<sleep-until>, after which you can play an mp3 file to wake you up instead
326-
of your normal alarm clock. This scenario looks roughly like this:
327-
328-
=for code
329-
# DateTime.new uses UTC by default, so get time zone from current time
330-
my $timezone = DateTime.now.timezone;
331-
my $instant = DateTime.new(
332-
year => 2015,
333-
month => 9,
334-
day => 4,
335-
hour => 7,
336-
minute => 0,
337-
timezone => $timezone
338-
).Instant;
339-
sleep-until $instant;
340-
qqx{mplayer wake-me-up.mp3};
341-
342221
=head2 sub infix:<->
343222

344223
multi infix:<-> (Date:D, Int:D --> Date:D)

doc/Type/independent-routines.rakudoc

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,127 @@ operator:
11871187
say [42, "42"].squish(with => &infix:<eq>); # OUTPUT: «(42)␤»
11881188
# The resulting item is Int
11891189

1190+
=head2 sub sleep
1191+
1192+
sub sleep($seconds = Inf --> Nil)
1193+
1194+
Attempt to sleep for the given number of C<$seconds>. Returns L<C<Nil>|/type/Nil> on
1195+
completion. Accepts L<C<Int>|/type/Int>, L<C<Num>|/type/Num>, L<C<Rat>|/type/Rat>, or L<C<Duration>|/type/Duration> types as an
1196+
argument since all of these also do L<C<Real>|/type/Real>.
1197+
1198+
=for code
1199+
sleep 5; # Int
1200+
sleep 5.2; # Num
1201+
sleep (5/2); # Rat
1202+
sleep (now - now + 5); # Duration
1203+
1204+
It is thus possible to sleep for a non-integer amount of time. For
1205+
instance, the following code shows that C<sleep (5/2)> sleeps for 2.5
1206+
seconds and C<sleep 5.2> sleeps for 5.2 seconds:
1207+
1208+
my $before = now;
1209+
sleep (5/2);
1210+
my $after = now;
1211+
say $after-$before; # OUTPUT: «2.502411561␤»
1212+
1213+
$before = now;
1214+
sleep 5.2;
1215+
$after = now;
1216+
say $after-$before; # OUTPUT: «5.20156987␤»
1217+
1218+
=head2 sub sleep-timer
1219+
1220+
sub sleep-timer(Real() $seconds = Inf --> Duration:D)
1221+
1222+
This function is implemented like C<sleep>, but unlike the former it does
1223+
return a L<C<Duration>|/type/Duration> instance with the number of seconds the system did not
1224+
sleep.
1225+
1226+
In particular, the returned L<C<Duration>|/type/Duration> will handle the number of seconds remaining
1227+
when the process has been awakened by some external event (e.g., Virtual Machine
1228+
or Operating System events).
1229+
Under normal condition, when sleep is not interrupted, the returned L<C<Duration>|/type/Duration>
1230+
has a value of C<0>, meaning no extra seconds remained to sleep.
1231+
Therefore, in normal situations:
1232+
1233+
say sleep-timer 3.14; # OUTPUT: «0␤»
1234+
1235+
The same result applies to edge cases, when a negative or zero time to sleep
1236+
is passed as argument:
1237+
1238+
=begin code
1239+
say sleep-timer -2; # OUTPUT: 0
1240+
say sleep-timer 0; # OUTPUT: 0
1241+
=end code
1242+
1243+
See also L<sleep-until|/routine/sleep-until>.
1244+
1245+
=head2 sub sleep-until
1246+
1247+
sub sleep-until(Instant $until --> Bool)
1248+
1249+
Works similar to C<sleep> but checks the current time and keeps sleeping
1250+
until the required instant in the future has been reached.
1251+
It uses internally the C<sleep-timer> method in a loop to ensure that,
1252+
if accidentally woken up early, it will wait again for the specified
1253+
amount of time remaining to reach the specified instant.
1254+
goes back to sleep
1255+
1256+
Returns C<True> if the L<C<Instant>|/type/Instant> in the future has been achieved (either
1257+
by mean of sleeping or because it is right now), C<False> in the case
1258+
an L<C<Instant>|/type/Instant> in the past has been specified.
1259+
1260+
To sleep until 10 seconds into the future, one could write something like this:
1261+
1262+
say sleep-until now+10; # OUTPUT: «True␤»
1263+
1264+
Trying to sleep until a time in the past doesn't work:
1265+
1266+
my $instant = now - 5;
1267+
say sleep-until $instant; # OUTPUT: «False␤»
1268+
1269+
However if we put the instant sufficiently far in the future, the sleep
1270+
should run:
1271+
1272+
=for code
1273+
my $instant = now + 30;
1274+
# assuming the two commands are run within 30 seconds of one another...
1275+
say sleep-until $instant; # OUTPUT: «True␤»
1276+
1277+
To specify an exact instant in the future, first create a L<C<DateTime>|/type/DateTime> at the
1278+
appropriate point in time, and cast to an L<C<Instant>|/type/Instant>.
1279+
1280+
=for code
1281+
my $instant = DateTime.new(
1282+
year => 2023,
1283+
month => 9,
1284+
day => 1,
1285+
hour => 22,
1286+
minute => 5);
1287+
say sleep-until $instant.Instant; # OUTPUT: «True␤» (eventually...)
1288+
1289+
This could be used as a primitive kind of alarm clock. For instance, say
1290+
you need to get up at 7am on the 4th of September 2015, but for some reason
1291+
your usual alarm clock is broken and you only have your laptop. You can
1292+
specify the time to get up (being careful about time zones, since
1293+
C<DateTime.new> uses UTC by default) as an L<C<Instant>|/type/Instant> and pass this to
1294+
C<sleep-until>, after which you can play an mp3 file to wake you up instead
1295+
of your normal alarm clock. This scenario looks roughly like this:
1296+
1297+
=for code
1298+
# DateTime.new uses UTC by default, so get time zone from current time
1299+
my $timezone = DateTime.now.timezone;
1300+
my $instant = DateTime.new(
1301+
year => 2015,
1302+
month => 9,
1303+
day => 4,
1304+
hour => 7,
1305+
minute => 0,
1306+
timezone => $timezone
1307+
).Instant;
1308+
sleep-until $instant;
1309+
qqx{mplayer wake-me-up.mp3};
1310+
11901311
=head2 sub emit
11911312

11921313
sub emit(\value --> Nil)

0 commit comments

Comments
 (0)