/
DateTime.pod6
501 lines (318 loc) · 15.8 KB
/
DateTime.pod6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
=begin pod :kind("Type") :subkind("class") :category("basic")
=TITLE class DateTime
=SUBTITLE Calendar date with time
class DateTime does Dateish {}
For handling points in civil time, a C<DateTime> object stores year, month,
day, hour, minute (all L<Int|/type/Int>), second (potentially fractional) and
a time zone.
It provides methods for calculating with date and time.
C<DateTime> methods are immutable; if you are tempted to modify one, create a
modified copy instead.
Time zones are handled as L<Integers|/type/Int> in B<seconds> offset from UTC,
not by time zone name.
=begin code
my $dt = DateTime.new(
year => 2015,
month => 11,
day => 21,
hour => 16,
minute => 1,
);
say $dt; # OUTPUT: «2015-11-21T16:01:00Z»
say $dt.later(days => 20); # OUTPUT: «2015-12-11T16:01:00Z»
say $dt.truncated-to('hour'); # OUTPUT: «2015-11-21T16:00:00Z»
say $dt.in-timezone(-8 * 3600); # OUTPUT: «2015-11-21T08:01:00-0800»
my $now = DateTime.now(formatter => { sprintf "%02d:%02d", .hour, .minute });
say $now; # 12:45 (or something like that)
=end code
Since version 6.d, using synthetic codepoints such as 7̈ will result in an
error.
=head1 Methods
=head2 method new
Defined as:
=for code :skip-test<compile time error>
multi method new(Int :$year!, Int :$month = 1, Int :$day = 1,
Int :$hour = 0, Int :$minute = 0, :$second = 0,
Int :$timezone = 0, :&formatter)
multi method new(Date :$date!,
Int :$hour = 0, Int :$minute = 0, :$second = 0,
Int :$timezone = 0, :&formatter)
multi method new(Int() $year, Int() $month, Int() $day,
Int() $hour, Int $minute, $second,
Int() :$timezone = 0, :&formatter)
multi method new(Instant:D $i, :$timezone=0, :&formatter)
multi method new(Int:D $posix, :$timezone=0, :&formatter)
multi method new(Str:D $format, :$timezone=0, :&formatter)
Creates a new C<DateTime> object. One option for creating a new DateTime object
is from the components (year, month, day, hour, ...) separately. Another is to
pass a L<Date|/type/Date> object for the date component, and specify the time
component-wise. Yet another is to obtain the time from an
L<Instant|/type/Instant>, and only supply the time zone and formatter. Or
instead of an Instant you can supply an L<Int|/type/Int> as a UNIX timestamp.
You can also supply a L<Str|/type/Str> formatted in ISO 8601 timestamp
notation or as a full L<RFC 3339|https://tools.ietf.org/html/rfc3339>
date and time. Strings should be formatted as C<yyyy-mm-ddThh:mm:ssZ>
or C<yyyy-mm-ddThh:mm:ss+0100>. We are somewhat less restrictive than the
ISO 8601 standard, as we allow Unicode digits and mixing of condensed
and extended time formats.
An invalid input string throws an exception of type
L<X::Temporal::InvalidFormat|/type/X::Temporal::InvalidFormat>. If you supply a string that includes a time
zone and supply the C<timezone> named argument, an exception of type
L<X::DateTime::TimezoneClash|/type/X::DateTime::TimezoneClash> is thrown.
my $datetime = DateTime.new(year => 2015,
month => 1,
day => 1,
hour => 1,
minute => 1,
second => 1,
timezone => 1);
$datetime = DateTime.new(date => Date.new('2015-12-24'),
hour => 1,
minute => 1,
second => 1,
timezone => 1);
$datetime = DateTime.new(2015, 1, 1, # First January of 2015
1, 1, 1); # Hour, minute, second with default time zone
$datetime = DateTime.new(now); # Instant.
# from a Unix timestamp
say $datetime = DateTime.new(1470853583); # OUTPUT: «2016-08-10T18:26:23Z»
$datetime = DateTime.new("2015-01-01T03:17:30+0500") # Formatted string
=head2 method now
Defined as:
method now(:$timezone = $*TZ, :&formatter --> DateTime:D)
Creates a new C<DateTime> object from the current system time. A custom
L<formatter|/routine/formatter> and L<timezone|/routine/timezone> can be provided. The C<:$timezone> is
the offset B<in seconds> from L<GMT|https://en.wikipedia.org/wiki/Greenwich_Mean_Time>
and defaults to the value of L«C<$*TZ> variable|/language/variables#index-entry-%24*TZ».
say DateTime.now; # OUTPUT: «2018-01-08T13:05:32.703292-06:00»
Note that one may use the methods shown below chained to the C<.now> to easily express current
values, e.g.,
say DateTime.now.year; # OUTPUT: «2018»
=head2 method clone
Defined as:
method clone(:$year, :$month, :$day, :$hour, :$minute, :$second, :$timezone, :&formatter)
Creates a new C<DateTime> object based on the invocant, but with the given
arguments overriding the values from the invocant.
say DateTime.new('2015-12-24T12:23:00Z').clone(hour => 0);
# OUTPUT: «2015-12-24T00:23:00Z»
Note that this can lead to invalid dates in some circumstances:
say DateTime.new("2012-02-29T12:34:56Z").clone(year => 2015);
CATCH { default { put .^name, ': ', .Str } };
# OUTPUT: «X::OutOfRange: Day out of range. Is: 29, should be in 1..28»
=head2 method hh-mm-ss
Defined as:
method hh-mm-ss(DateTime:D: --> Str:D)
Returns the time represented by the object as a string in 24-hour HH:MM:SS format:
say DateTime.new("2052-02-29T22:34:56Z").hh-mm-ss;
# OUTPUT: «22:34:56»
=head2 method hour
Defined as:
method hour(DateTime:D: --> Int:D)
Returns the hour component.
say DateTime.new('2012-02-29T12:34:56Z').hour; # OUTPUT: «12»
=head2 method minute
Defined as:
method minute(DateTime:D: --> Int:D)
Returns the minute component.
say DateTime.new('2012-02-29T12:34:56Z').minute; # OUTPUT: «34»
=head2 method second
Defined as:
method second(DateTime:D:)
Returns the second component, including potentially fractional seconds.
say DateTime.new('2012-02-29T12:34:56Z').second; # OUTPUT: «56»
say DateTime.new('2012-02-29T12:34:56.789Z').second; # OUTPUT: «56.789»
say DateTime.new('2012-02-29T12:34:56,789Z').second; # comma also ok
=head2 method whole-second
Defined as:
method whole-second(DateTime:D:)
Returns the second component, rounded down to an L<Int|/type/Int>.
say DateTime.new('2012-02-29T12:34:56.789Z').whole-second; # OUTPUT: «56»
=head2 method timezone
Defined as:
method timezone(DateTime:D: --> Int:D)
Returns the time zone B<in seconds> as an offset from UTC.
say DateTime.new('2015-12-24T12:23:00+0200').timezone; # OUTPUT: «7200»
=head2 method offset
Defined as:
method offset(DateTime:D: --> Int:D)
Returns the time zone B<in seconds> as an offset from UTC. This is an alias for
L<#method timezone>.
say DateTime.new('2015-12-24T12:23:00+0200').offset; # OUTPUT: «7200»
=head2 method offset-in-minutes
Defined as:
method offset-in-minutes(DateTime:D: --> Real:D)
Returns the time zone in minutes as an offset from UTC.
say DateTime.new('2015-12-24T12:23:00+0200').offset-in-minutes; # OUTPUT: «120»
=head2 method offset-in-hours
Defined as:
method offset-in-hours(DateTime:D: --> Real:D)
Returns the time zone in hours as an offset from UTC.
=for code
say DateTime.new('2015-12-24T12:23:00+0200').offset-in-hours; # OUTPUT: «2»
=head2 method Str
Defined as:
method Str(DateTime:D: --> Str:D)
Returns a string representation of the invocant, as done by
L<the formatter|/type/Dateish#method_formatter>.
If no formatter was specified, an ISO 8601 timestamp will be returned.
=for code
say DateTime.new('2015-12-24T12:23:00+0200').Str;
# OUTPUT: «2015-12-24T12:23:00+02:00»
=head2 method Instant
Defined as:
method Instant(DateTime:D: --> Instant:D)
Returns an L<Instant|/type/Instant> object based on the invocant.
say DateTime.new('2015-12-24T12:23:00+0200').Instant; # OUTPUT: «Instant:1450952616»
=head2 method posix
Defined as:
method posix(Bool:D: $ignore-timezone = False --> Int:D)
Returns the date and time as a POSIX/UNIX timestamp (seconds since the Epoch,
1st January 1970 UTC).
If C<$ignore-timezone> is C<True>, the C<DateTime> object will be treated as if
the time zone offset is zero.
=for code
say DateTime.new('2015-12-24T12:23:00Z').posix; # OUTPUT: «1450959780»
=head2 method later
Defined as:
method later(DateTime:D: *%unit)
Returns a DateTime object based on the current one, but with a time delta
applied. The time delta can be passed as a named argument where the argument
name is the unit.
Unless the given unit is C<second> or C<seconds>, the given value will be
converted to an L<Int|/type/Int>.
Allowed units are C<second>, C<seconds>, C<minute>, C<minutes>, C<hour>,
C<hours>, C<day>, C<days>, C<week>, C<weeks>, C<month>, C<months>, C<year>,
C<years>. Please note that the plural forms can only be used with
the C<later> and C<earlier> methods.
The C<:2nd> form of colonpairs can be used as a compact and self-documenting
way of specifying the delta:
say DateTime.new('2015-12-24T12:23:00Z').later(:2years);
# OUTPUT: «2017-12-24T12:23:00Z»
Since addition of several different time units is not commutative, only one
unit may be passed.
my $d = DateTime.new(date => Date.new('2015-02-27'));
say $d.later(month => 1).later(:2days); # OUTPUT: «2015-03-29T00:00:00Z»
say $d.later(days => 2).later(:1month); # OUTPUT: «2015-04-01T00:00:00Z»
say $d.later(days => 2).later(:month); # same, as +True === 1
If the resultant time has value C<60> for seconds, yet no leap second
actually exists for that time, seconds will be set to C<59>:
say DateTime.new('2008-12-31T23:59:60Z').later: :1day;
# OUTPUT: «2009-01-01T23:59:59Z»
Negative offsets are allowed, though L<earlier|/routine/earlier> is more
idiomatic for that.
=head2 method earlier
Defined as:
method earlier(DateTime:D: *%unit)
Returns a DateTime object based on the current one, but with a time delta
towards the past applied. Unless the given unit is C<second> or C<seconds>, the
given value will be converted to an L<Int|/type/Int>. See L<#method later> for
usage.
my $d = DateTime.new(date => Date.new('2015-02-27'));
say $d.earlier(month => 1).earlier(:2days); # OUTPUT: «2015-01-25T00:00:00Z»
If the resultant time has value C<60> for seconds, yet no leap second
actually exists for that time, seconds will be set to C<59>:
say DateTime.new('2008-12-31T23:59:60Z').earlier: :1day;
# OUTPUT: «2008-12-30T23:59:59Z»
Negative offsets are allowed, though L<later|/routine/later> is more idiomatic for that.
=head2 method truncated-to
Defined as:
method truncated-to(DateTime:D: Cool $unit)
Returns a copy of the invocant, with everything smaller than the specified
unit truncated to the smallest possible value.
my $d = DateTime.new("2012-02-29T12:34:56.946314Z");
say $d.truncated-to('second'); # OUTPUT: «2012-02-29T12:34:56Z»
say $d.truncated-to('minute'); # OUTPUT: «2012-02-29T12:34:00Z»
say $d.truncated-to('hour'); # OUTPUT: «2012-02-29T12:00:00Z»
say $d.truncated-to('day'); # OUTPUT: «2012-02-29T00:00:00Z»
say $d.truncated-to('month'); # OUTPUT: «2012-02-01T00:00:00Z»
say $d.truncated-to('year'); # OUTPUT: «2012-01-01T00:00:00Z»
DateTimes with fractional seconds can be truncated to whole seconds with
C<.truncated-to('second')>.
=head2 method Date
Defined as:
multi method Date(DateTime:U --> Date:U)
multi method Date(DateTime:D --> Date:D)
Converts the invocant to L«C<Date>|/type/Date».
=for code
say DateTime.new("2012-02-29T12:34:56.946314Z").Date; # OUTPUT: «2012-02-29»
say DateTime.Date; # OUTPUT: «(Date)»
=head2 method DateTime
Defined as:
method DateTime(--> DateTime)
Returns the invocant.
say DateTime.new("2012-02-29T12:34:56.946314Z").DateTime;
# OUTPUT: «2012-02-29T12:34:56.946314Z»
say DateTime.DateTime;
# OUTPUT: «(DateTime)»
=head2 method utc
Defined as:
method utc(DateTime:D: --> DateTime:D)
Returns a DateTime object for the same time, but in time zone UTC.
say DateTime.new('2015-12-24T12:23:00+0200').utc;
# OUTPUT: «2015-12-24T10:23:00Z»
=head2 method in-timezone
Defined as:
method in-timezone(DateTime:D: Int(Cool) $timezone = 0 --> DateTime:D)
Returns a DateTime object for the same time, but in the specified C<$timezone>,
which is the offset B<in seconds> from
L<GMT|https://en.wikipedia.org/wiki/Greenwich_Mean_Time>.
say DateTime.new('2015-12-24T12:23:00Z').in-timezone(3600 + 1800); # OUTPUT: «2015-12-24T13:53:00+0130»
Per L<RFC 7164|https://tools.ietf.org/html/rfc7164#section-3>, leap seconds do
not respect local time and always occur at the end of the I<UTC> day:
say DateTime.new: '2017-01-01T00:59:60+01:00'
# OUTPUT: «2017-01-01T00:59:60+01:00»
=head2 method local
Defined as:
method local(DateTime:D: --> DateTime:D)
Returns a DateTime object for the same time, but in the local time zone
(L«C<$*TZ>|/language/variables#index-entry-%24*TZ»).
my $*TZ = -3600;
say DateTime.new('2015-12-24T12:23:00+0200').local; # OUTPUT: «2015-12-24T09:23:00-0100»
=head2 sub infix:<->
multi sub infix:<-> (DateTime:D, Duration:D --> DateTime:D)
multi sub infix:<-> (DateTime:D, DateTime:D --> Duration:D)
Takes a L«C<DateTime>|/type/DateTime» to subtract from and either a
L«C<Duration>|/type/Duration» or another C<DateTime> object. Returns a new
C<DateTime> object or the C<Duration> between the two dates, respectively. When
subtracting C<Duration>, time zone of the original C<DateTime> is preserved
in the returned C<DateTime> object.
say perl DateTime.new(:2016year) - DateTime.new(:2015year):;
# OUTPUT: «Duration.new(31536001.0)»
say DateTime.new(:2016year, :3600timezone) - Duration.new(31536001.0);
# OUTPUT: «2015-01-01T00:00:00+01:00»
=head2 sub infix:<+>
multi sub infix:<+> (DateTime:D, Duration:D --> DateTime:D)
multi sub infix:<+> (Duration:D, DateTime:D --> DateTime:D)
Takes a L«C<DateTime>|/type/DateTime» and increases it by the given
L«C<Duration>|/type/Duration», preserving the time zone.
say DateTime.new(:2015year) + Duration.new(31536001.0);
# OUTPUT: «2016-01-01T00:00:00Z»
say Duration.new(42) + DateTime.new(:2015year, :3600timezone);
# OUTPUT: «2015-01-01T00:00:42+01:00»
=head2 sub infix:«<=>»
multi sub infix:«<=>»(DateTime:D \a, DateTime:D \b --> Order:D)
Compares the equivalent instant, returns the Order.
say DateTime.now <=> DateTime.now; # OUTPUT: «Less»
=head2 sub infix:<cmp>
multi sub infix:<cmp>(DateTime:D \a, DateTime:D \b --> Order:D)
Compares the equivalent instant, returns the Order.
=head2 sub infix:«<»
multi sub infix:«<»(DateTime:D \a, DateTime:D \b --> Bool:D)
Compares the equivalent instant, returns a C<Bool>
=head2 sub infix:«>»
multi sub infix:«>»(DateTime:D \a, DateTime:D \b --> Bool:D)
Compares the equivalent instant, returns a C<Bool>
=head2 sub infix:«<=»
multi sub infix:«<=»(DateTime:D \a, DateTime:D \b --> Bool:D)
Compares the equivalent instant, returns a C<Bool>
=head2 sub infix:«>=»
multi sub infix:«>=»(DateTime:D \a, DateTime:D \b --> Bool:D)
Compares the equivalent instant, returns a C<Bool>
=head2 sub infix:«==»
multi sub infix:«==»(DateTime:D \a, DateTime:D \b --> Bool:D)
Compares the equivalent instant, returns a C<Bool>
=head2 sub infix:«!=»
multi sub infix:«!=»(DateTime:D \a, DateTime:D \b --> Bool:D)
Compares the equivalent instant, returns a C<Bool>
=end pod
# vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6