Skip to content

Commit e1fd532

Browse files
committed
Make examples compile, add new examples, remove 'Usage' sections for Date-related types
1 parent fb947c0 commit e1fd532

File tree

3 files changed

+127
-257
lines changed

3 files changed

+127
-257
lines changed

doc/Type/Date.pod6

Lines changed: 57 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -34,36 +34,32 @@ an object the current day according to the system clock.
3434
3535
Defined as:
3636
37-
proto method new() {*}
37+
=begin code :skip-test
38+
proto method new() {*}
39+
=end code
3840
multi method new($year, $month, $day, :&formatter) returns Date:D
3941
multi method new(:$year!, :$month = 1, :$day = 1) returns Date:D
4042
multi method new(Str $date) returns Date:D
4143
multi method new(Instant:D $dt) returns Date:D
4244
multi method new(DateTime:D $dt) returns Date:D
4345
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-
5246
Creates a new C<Date> object, either from a triple of (year, month, day)
5347
that can be coerced to integers, or from a string of the form C<YYYY-MM-DD>
5448
(L<ISO 8601|https://en.wikipedia.org/wiki/ISO_8601>), or from an Instant
5549
or DateTime object. Optionally accepts a formatter as a named parameter.
5650
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+
5757
=head2 method new-from-daycount
5858
5959
Defined as:
6060
6161
method new-from-daycount($daycount,:&formatter) returns Date:D
6262
63-
Usage:
64-
65-
$date = Date.new-from-daycount(DAYCOUNT, FORMATTER?)
66-
6763
Creates a new C<Date> object given C<$daycount> which is the number
6864
of days from epoch Nov. 17, 1858, i.e. the
6965
L<Modified Julian Day|https://en.wikipedia.org/wiki/Julian_day>.
@@ -77,10 +73,6 @@ Defined as:
7773
7874
method clone(:$year, :$month, :$day)
7975
80-
Usage:
81-
82-
DATE.clone(YEAR, MONTH, DAY)
83-
8476
Creates a new C<Date> object based on the invocant, but with the given
8577
arguments overriding the values from the invocant.
8678
@@ -92,23 +84,17 @@ Defined as:
9284
9385
method today(:&formatter) returns Date:D
9486
95-
Usage:
96-
97-
Date.today
98-
9987
Returns a C<Date> object for the current day. Optionally accepts a
10088
formatter named parameter.
10189
90+
say Date.today;
91+
10292
=head2 method later
10393
10494
Defined as:
10595
10696
method later(Date:D: *%unit)
10797
108-
Usage:
109-
110-
DATE.later(DELTA)
111-
11298
Returns a C<Date> object based on the current one, but with a date delta
11399
applied. The date delta can be passed as a named argument where the argument
114100
name is the unit.
@@ -139,23 +125,18 @@ Defined as:
139125
140126
method earlier(Date:D: *%unit)
141127
142-
Usage:
143-
144-
DATE.earlier(DELTA)
145-
146128
Returns a C<Date> object based on the current one, but with a date delta
147129
towards the past applied. See L<#method later> for usage.
148130
131+
my $d = Date.new('2015-02-27');
132+
say $d.earlier(month => 5).earlier(:2days); # 2014-09-25
133+
149134
=head2 method truncated-to
150135
151136
Defined as:
152137
153138
method truncated-to(Date:D: Cool $unit)
154139
155-
Usage:
156-
157-
DATE.truncated-to(UNIT)
158-
159140
Returns a C<Date> truncated to the first day of its year, month or week.
160141
For example
161142
@@ -170,10 +151,6 @@ Defined as:
170151
171152
method succ(Date:D:) returns Date:D
172153
173-
Usage:
174-
175-
DATE.succ
176-
177154
Returns a C<Date> of the following day. "succ" is short for "successor".
178155
179156
say Date.new("2016-02-28").succ; # 2016-02-29
@@ -184,10 +161,6 @@ Defined as:
184161
185162
method pred(Date:D:) returns Date:D
186163
187-
Usage:
188-
189-
DATE.pred
190-
191164
Returns a C<Date> of the previous day. "pred" is short for "predecessor".
192165
193166
say Date.new("2016-01-01").pred; # 2015-12-31
@@ -198,10 +171,6 @@ Defined as:
198171
199172
multi method Str(Date:D:) returns Str:D
200173
201-
Usage:
202-
203-
DATE.Str
204-
205174
Returns a string representation of the invocant, as specified by the
206175
L<the formatter|/type/Dateish#method_formatter>. If no formatter was
207176
specified, an (L<ISO 8601|https://en.wikipedia.org/wiki/ISO_8601>) date
@@ -218,12 +187,10 @@ Defined as:
218187
219188
multi method gist(Date:D:) returns Str:D
220189
221-
Usage:
222-
223-
DATE.gist
224-
225190
Returns the date in C<YYYY-MM-DD> format (L<ISO 8601|https://en.wikipedia.org/wiki/ISO_8601>)
226191
192+
say Date.new('2015-12-24').gist; # 2015-12-24
193+
227194
=head1 Functions
228195
229196
=head2 sub sleep
@@ -234,24 +201,25 @@ Attempt to sleep for the given number of C<$seconds>. Returns C<Nil> on
234201
completion. Accepts C<Int>, C<Num>, C<Rat>, or C<Duration> types as an
235202
argument since all of these also do C<Real>.
236203
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
241209
242210
It is thus possible to sleep for a non-integer amount of time. For
243211
instance, the following code shows that C<sleep (5/2)> sleeps for 2.5
244212
seconds and C<sleep 5.2> sleeps for 5.2 seconds:
245213
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
250218
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
255223
256224
=head2 sub sleep-timer
257225
@@ -261,7 +229,7 @@ This function is just like C<sleep>, but returns the amount of time
261229
remaining to sleep as a C<Duration> (which will be 0 if the call was not
262230
interrupted).
263231
264-
say sleep-timer 3.14 #=> 0
232+
say sleep-timer 3.14; # 0
265233
266234
=head2 sub sleep-until
267235
@@ -275,30 +243,32 @@ sleep until a time in the past.
275243
276244
To sleep until 10 seconds into the future, one could write something like this:
277245
278-
say sleep-until now+10 #=> True
246+
say sleep-until now+10; # True
279247
280248
Trying to sleep until a time in the past doesn't work:
281249
282250
my $instant = now - 5;
283-
say sleep-until $instant # => False
251+
say sleep-until $instant; # False
284252
285253
However if we put the instant sufficiently far in the future, the sleep
286254
should run:
287255
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
291260
292261
To specify an exact instant in the future, first create a C<DateTime> at the
293262
appropriate point in time, and cast to an C<Instant>.
294263
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...)
302272
303273
This could be be used as a primitive kind of alarm clock. For instance, say
304274
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
308278
C<sleep-until>, after which you can play an mp3 file to wake you up instead
309279
of your normal alarm clock. This scenario looks roughly like this:
310280
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};
323294
324295
=end pod

0 commit comments

Comments
 (0)