@@ -34,36 +34,32 @@ an object the current day according to the system clock.
34
34
35
35
Defined as:
36
36
37
- proto method new() {*}
37
+ = begin code :skip-test
38
+ proto method new() {*}
39
+ = end code
38
40
multi method new($year, $month, $day, :&formatter) returns Date:D
39
41
multi method new(:$year!, :$month = 1, :$day = 1) returns Date:D
40
42
multi method new(Str $date) returns Date:D
41
43
multi method new(Instant:D $dt) returns Date:D
42
44
multi method new(DateTime:D $dt) returns Date:D
43
45
44
- Usage:
45
-
46
- $date = Date.new(YEAR, MONTH, DAY, FORMATTER?)
47
- $date = Date.new(year => YEAR, month => MONTH?, day => DAY?, FORMATTER?)
48
- $date = Date.new(YYYY-MM-DD, FORMATTER?)
49
- $date = Date.new(now, FORMATTER?)
50
- $date = Date.new(DateTime.now, FORMATTER?)
51
-
52
46
Creates a new C < Date > object, either from a triple of (year, month, day)
53
47
that can be coerced to integers, or from a string of the form C < YYYY-MM-DD >
54
48
(L < ISO 8601|https://en.wikipedia.org/wiki/ISO_8601 > ), or from an Instant
55
49
or DateTime object. Optionally accepts a formatter as a named parameter.
56
50
51
+ my $date = Date.new(2042, 1, 1);
52
+ $date = Date.new(year => 2042, month => 1, day => 1);
53
+ $date = Date.new("2042-01-01");
54
+ $date = Date.new(now);
55
+ $date = Date.new(DateTime.now);
56
+
57
57
= head2 method new-from-daycount
58
58
59
59
Defined as:
60
60
61
61
method new-from-daycount($daycount,:&formatter) returns Date:D
62
62
63
- Usage:
64
-
65
- $date = Date.new-from-daycount(DAYCOUNT, FORMATTER?)
66
-
67
63
Creates a new C < Date > object given C < $daycount > which is the number
68
64
of days from epoch Nov. 17, 1858, i.e. the
69
65
L < Modified Julian Day|https://en.wikipedia.org/wiki/Julian_day > .
@@ -77,10 +73,6 @@ Defined as:
77
73
78
74
method clone(:$year, :$month, :$day)
79
75
80
- Usage:
81
-
82
- DATE.clone(YEAR, MONTH, DAY)
83
-
84
76
Creates a new C < Date > object based on the invocant, but with the given
85
77
arguments overriding the values from the invocant.
86
78
@@ -92,23 +84,17 @@ Defined as:
92
84
93
85
method today(:&formatter) returns Date:D
94
86
95
- Usage:
96
-
97
- Date.today
98
-
99
87
Returns a C < Date > object for the current day. Optionally accepts a
100
88
formatter named parameter.
101
89
90
+ say Date.today;
91
+
102
92
= head2 method later
103
93
104
94
Defined as:
105
95
106
96
method later(Date:D: *%unit)
107
97
108
- Usage:
109
-
110
- DATE.later(DELTA)
111
-
112
98
Returns a C < Date > object based on the current one, but with a date delta
113
99
applied. The date delta can be passed as a named argument where the argument
114
100
name is the unit.
@@ -139,23 +125,18 @@ Defined as:
139
125
140
126
method earlier(Date:D: *%unit)
141
127
142
- Usage:
143
-
144
- DATE.earlier(DELTA)
145
-
146
128
Returns a C < Date > object based on the current one, but with a date delta
147
129
towards the past applied. See L < #method later > for usage.
148
130
131
+ my $d = Date.new('2015-02-27');
132
+ say $d.earlier(month => 5).earlier(:2days); # 2014-09-25
133
+
149
134
= head2 method truncated-to
150
135
151
136
Defined as:
152
137
153
138
method truncated-to(Date:D: Cool $unit)
154
139
155
- Usage:
156
-
157
- DATE.truncated-to(UNIT)
158
-
159
140
Returns a C < Date > truncated to the first day of its year, month or week.
160
141
For example
161
142
@@ -170,10 +151,6 @@ Defined as:
170
151
171
152
method succ(Date:D:) returns Date:D
172
153
173
- Usage:
174
-
175
- DATE.succ
176
-
177
154
Returns a C < Date > of the following day. "succ" is short for "successor".
178
155
179
156
say Date.new("2016-02-28").succ; # 2016-02-29
@@ -184,10 +161,6 @@ Defined as:
184
161
185
162
method pred(Date:D:) returns Date:D
186
163
187
- Usage:
188
-
189
- DATE.pred
190
-
191
164
Returns a C < Date > of the previous day. "pred" is short for "predecessor".
192
165
193
166
say Date.new("2016-01-01").pred; # 2015-12-31
@@ -198,10 +171,6 @@ Defined as:
198
171
199
172
multi method Str(Date:D:) returns Str:D
200
173
201
- Usage:
202
-
203
- DATE.Str
204
-
205
174
Returns a string representation of the invocant, as specified by the
206
175
L < the formatter|/type/Dateish#method_formatter > . If no formatter was
207
176
specified, an (L < ISO 8601|https://en.wikipedia.org/wiki/ISO_8601 > ) date
@@ -218,12 +187,10 @@ Defined as:
218
187
219
188
multi method gist(Date:D:) returns Str:D
220
189
221
- Usage:
222
-
223
- DATE.gist
224
-
225
190
Returns the date in C < YYYY-MM-DD > format (L < ISO 8601|https://en.wikipedia.org/wiki/ISO_8601 > )
226
191
192
+ say Date.new('2015-12-24').gist; # 2015-12-24
193
+
227
194
= head1 Functions
228
195
229
196
= head2 sub sleep
@@ -234,24 +201,25 @@ Attempt to sleep for the given number of C<$seconds>. Returns C<Nil> on
234
201
completion. Accepts C < Int > , C < Num > , C < Rat > , or C < Duration > types as an
235
202
argument since all of these also do C < Real > .
236
203
237
- sleep 5 # Int
238
- sleep 5.2 # Num
239
- sleep (5/2) # Rat
240
- sleep (now - now + 5) # Duration
204
+ = for code :skip-test
205
+ sleep 5; # Int
206
+ sleep 5.2; # Num
207
+ sleep (5/2); # Rat
208
+ sleep (now - now + 5); # Duration
241
209
242
210
It is thus possible to sleep for a non-integer amount of time. For
243
211
instance, the following code shows that C < sleep (5/2) > sleeps for 2.5
244
212
seconds and C < sleep 5.2 > sleeps for 5.2 seconds:
245
213
246
- my $before = now
247
- sleep (5/2)
248
- my $after = now
249
- say $after-$before #=> 2.502411561
214
+ my $before = now;
215
+ sleep (5/2);
216
+ my $after = now;
217
+ say $after-$before; # 2.502411561
250
218
251
- my $before = now
252
- sleep 5.2
253
- my $after = now
254
- say $after-$before #=> 5.20156987
219
+ $before = now;
220
+ sleep 5.2;
221
+ $after = now;
222
+ say $after-$before; # 5.20156987
255
223
256
224
= head2 sub sleep-timer
257
225
@@ -261,7 +229,7 @@ This function is just like C<sleep>, but returns the amount of time
261
229
remaining to sleep as a C < Duration > (which will be 0 if the call was not
262
230
interrupted).
263
231
264
- say sleep-timer 3.14 #=> 0
232
+ say sleep-timer 3.14; # 0
265
233
266
234
= head2 sub sleep-until
267
235
@@ -275,30 +243,32 @@ sleep until a time in the past.
275
243
276
244
To sleep until 10 seconds into the future, one could write something like this:
277
245
278
- say sleep-until now+10 #=> True
246
+ say sleep-until now+10; # True
279
247
280
248
Trying to sleep until a time in the past doesn't work:
281
249
282
250
my $instant = now - 5;
283
- say sleep-until $instant # => False
251
+ say sleep-until $instant; # False
284
252
285
253
However if we put the instant sufficiently far in the future, the sleep
286
254
should run:
287
255
288
- my $instant = now + 30
289
- # assuming the two commands are run within 30 seconds of one another...
290
- say sleep-until $instant # => True
256
+ = for code :skip-test
257
+ my $instant = now + 30;
258
+ # assuming the two commands are run within 30 seconds of one another...
259
+ say sleep-until $instant; # True
291
260
292
261
To specify an exact instant in the future, first create a C < DateTime > at the
293
262
appropriate point in time, and cast to an C < Instant > .
294
263
295
- my $instant = DateTime.new(
296
- year => 2020,
297
- month => 9,
298
- day => 1,
299
- hour => 22,
300
- minute => 5);
301
- say sleep-until $instant.Instant # => True (eventually...)
264
+ = for code :skip-test
265
+ my $instant = DateTime.new(
266
+ year => 2020,
267
+ month => 9,
268
+ day => 1,
269
+ hour => 22,
270
+ minute => 5);
271
+ say sleep-until $instant.Instant; # True (eventually...)
302
272
303
273
This could be be used as a primitive kind of alarm clock. For instance, say
304
274
you need to get up at 7am on the 4th of September 2015, but for some reason
@@ -308,17 +278,18 @@ C<DateTime.new> uses UTC by default) as an C<Instant> and pass this to
308
278
C < sleep-until > , after which you can play an mp3 file to wake you up instead
309
279
of your normal alarm clock. This scenario looks roughly like this:
310
280
311
- # DateTime.new uses UTC by default, so get timezone from current time
312
- my $timezone = DateTime.now.timezone;
313
- my $instant = DateTime.new(
314
- year => 2015,
315
- month => 9,
316
- day => 4,
317
- hour => 7,
318
- minute => 0,
319
- timezone => $timezone
320
- ).Instant;
321
- sleep-until $instant;
322
- qqx{mplayer wake-me-up.mp3};
281
+ = for code :skip-test
282
+ # DateTime.new uses UTC by default, so get timezone from current time
283
+ my $timezone = DateTime.now.timezone;
284
+ my $instant = DateTime.new(
285
+ year => 2015,
286
+ month => 9,
287
+ day => 4,
288
+ hour => 7,
289
+ minute => 0,
290
+ timezone => $timezone
291
+ ).Instant;
292
+ sleep-until $instant;
293
+ qqx{mplayer wake-me-up.mp3};
323
294
324
295
= end pod
0 commit comments