Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 493 lines (429 sloc) 24.361 kB
7d2cc0e [spec/S32-temporal/DateTime.t] renamed from Temporal.t, add 2 strftim…
mberends authored
1 use v6;
2 use Test;
3
ce9b56d [S32] Dropped mention of quarters from Temporal.pod. I don't think qu…
Kodi authored
4 plan *;
4214998 [t/spec] Cleaned up DateTime.t a bit. (I hadn't meant to commit it yet.)
Kodi authored
5
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
6 sub dt(*%args) { DateTime.new(year => 1984, |%args) }
7d2cc0e [spec/S32-temporal/DateTime.t] renamed from Temporal.t, add 2 strftim…
mberends authored
7
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
8 sub ymd($year, $month, $day) { dt :$year, :$month, :$day }
9
10 sub ds(Str $s) { DateTime.new($s) }
11
12 sub tz($tz) { ds "2005-02-04T15:25:00$tz" }
13
14 sub show-dt($dt) {
15 join ' ', floor($dt.second), $dt.minute, $dt.hour,
16 $dt.day, $dt.month, $dt.year, $dt.day-of-week
17 }
7d2cc0e [spec/S32-temporal/DateTime.t] renamed from Temporal.t, add 2 strftim…
mberends authored
18
19 # An independent calculation to cross check the Temporal algorithms.
20 sub test-gmtime( Num $t is copy ) {
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
21 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday);
7d2cc0e [spec/S32-temporal/DateTime.t] renamed from Temporal.t, add 2 strftim…
mberends authored
22 $t = floor($t);
23 $sec = $t % 60; $t div= 60; # $t is now epoch minutes
24 $min = $t % 60; $t div= 60; # $t is now epoch hours
25 $hour = $t % 24; $t div= 24; # $t is now epoch days
26 # Not a sophisticated or fast algorithm, just an understandable one
27 # only valid from 1970-01-01 until 2100-02-28
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
28 $wday = ($t+3) % 7; # 1970-01-01 was a Thursday
29 # Monday is $wday 0, unlike Perl 5.
7d2cc0e [spec/S32-temporal/DateTime.t] renamed from Temporal.t, add 2 strftim…
mberends authored
30 $year = 70; # (Unix epoch 0) == (Gregorian 1970) == (Perl year 70)
31 loop ( $yday = 365; $t >= $yday; $year++ ) {
32 $t -= $yday; # count off full years of 365 or 366 days
33 $yday = (($year+1) % 4 == 0) ?? 366 !! 365;
34 }
35 $yday = $t;
36 # Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
37 my @days = 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31;
38 @days[1] = ($year % 4 == 0) ?? 29 !! 28; # calibrate February
39 loop ( $mon = 0; $t >= @days[$mon]; $mon++ ) {
40 $t -= @days[$mon]; # count off full months of whatever days
41 }
42 $mday = $t + 1;
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
43 return ($sec, $min, $hour, $mday, $mon + 1, $year + 1900, $wday + 1);
44 } # 0 1 2 3 4 5 6
4214998 [t/spec] Cleaned up DateTime.t a bit. (I hadn't meant to commit it yet.)
Kodi authored
45
ce9b56d [S32] Dropped mention of quarters from Temporal.pod. I don't think qu…
Kodi authored
46 # --------------------------------------------------------------------
ef575e2 [t/spec/S32-temporal/DateTime] Corrected and extended. Rakudo should …
Kodi authored
47 # Input validation
ce9b56d [S32] Dropped mention of quarters from Temporal.pod. I don't think qu…
Kodi authored
48 # --------------------------------------------------------------------
ef575e2 [t/spec/S32-temporal/DateTime] Corrected and extended. Rakudo should …
Kodi authored
49
50 # L<S32::Temporal/'C<DateTime>'/outside of the ranges specified>
51
52 lives_ok { dt month => 1 }, 'DateTime accepts January';
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
53 dies_ok { dt month => 0 }, 'DateTime rejects month 0';
54 dies_ok { dt month => -1 }, 'DateTime rejects month -1';
ef575e2 [t/spec/S32-temporal/DateTime] Corrected and extended. Rakudo should …
Kodi authored
55 lives_ok { dt month => 12 }, 'DateTime accepts December';
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
56 dies_ok { dt month => 13 }, 'DateTime rejects month 13';
ef575e2 [t/spec/S32-temporal/DateTime] Corrected and extended. Rakudo should …
Kodi authored
57 lives_ok { dt month => 1, day => 31 }, 'DateTime accepts January 31';
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
58 dies_ok { dt month => 1, day => 32 }, 'DateTime rejects January 32';
ef575e2 [t/spec/S32-temporal/DateTime] Corrected and extended. Rakudo should …
Kodi authored
59 lives_ok { dt month => 6, day => 30 }, 'DateTime accepts June 30';
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
60 dies_ok { dt month => 6, day => 31 }, 'DateTime rejects June 31';
61 dies_ok { dt month => 2, day => 30 }, 'DateTime rejects February 30';
ef575e2 [t/spec/S32-temporal/DateTime] Corrected and extended. Rakudo should …
Kodi authored
62 lives_ok { ymd 1996, 2, 29 }, 'DateTime accepts 29 Feb 1996';
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
63 dies_ok { ymd 1995, 2, 29 }, 'DateTime rejects 29 Feb 1995';
ef575e2 [t/spec/S32-temporal/DateTime] Corrected and extended. Rakudo should …
Kodi authored
64 lives_ok { ymd 2000, 2, 29 }, 'DateTime accepts 29 Feb 2000';
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
65 lives_ok { ds '2000-02-29T22:33:44Z' }, 'DateTime accepts 29 Feb 2000 (ISO)';
66 dies_ok { ymd 1900, 2, 29 }, 'DateTime rejects 29 Feb 1900';
67 dies_ok { ds '1900-02-29T22:33:44Z' }, 'DateTime rejects 29 Feb 1900 (ISO)';
ef575e2 [t/spec/S32-temporal/DateTime] Corrected and extended. Rakudo should …
Kodi authored
68 lives_ok { dt hour => 0 }, 'DateTime accepts hour 0';
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
69 dies_ok { dt hour => -1 }, 'DateTime rejects hour 0';
ef575e2 [t/spec/S32-temporal/DateTime] Corrected and extended. Rakudo should …
Kodi authored
70 lives_ok { dt hour => 23 }, 'DateTime accepts hour 23';
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
71 dies_ok { dt hour => 24 }, 'DateTime rejects hour 24';
ef575e2 [t/spec/S32-temporal/DateTime] Corrected and extended. Rakudo should …
Kodi authored
72 lives_ok { dt minute => 0 }, 'DateTime accepts minute 0';
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
73 dies_ok { dt minute => -1 }, 'DateTime rejects minute -1';
ef575e2 [t/spec/S32-temporal/DateTime] Corrected and extended. Rakudo should …
Kodi authored
74 lives_ok { dt minute => 59 }, 'DateTime accepts minute 59';
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
75 lives_ok { ds '1999-01-01T00:59:22Z' }, 'DateTime accepts minute 59 (ISO)';
ef575e2 [t/spec/S32-temporal/DateTime] Corrected and extended. Rakudo should …
Kodi authored
76 lives_ok { dt date => Date.new(1999, 1, 1), minute => 59 }, 'DateTime accepts minute 59 (with Date)';
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
77 dies_ok { dt minute => 60 }, 'DateTime rejects minute 60';
78 dies_ok { ds '1999-01-01T00:60:22Z' }, 'DateTime rejects minute 60 (ISO)';
79 dies_ok { dt date => Date.new(1999, 1, 1), minute => 60 }, 'DateTime rejects minute 60 (with Date)';
ef575e2 [t/spec/S32-temporal/DateTime] Corrected and extended. Rakudo should …
Kodi authored
80 lives_ok { dt second => 0 }, 'DateTime accepts second 0';
81 lives_ok { dt second => 1/2 }, 'DateTime accepts second 1/2';
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
82 dies_ok { dt second => -1 }, 'DateTime rejects second -1';
83 dies_ok { dt second => -1/2 }, 'DateTime rejects second -1/2';
ef575e2 [t/spec/S32-temporal/DateTime] Corrected and extended. Rakudo should …
Kodi authored
84 lives_ok { dt second => 60 }, 'DateTime accepts second 60';
85 lives_ok { dt second => 61 }, 'DateTime accepts second 61';
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
86 lives_ok { ds '1999-01-01T12:10:61Z' }, 'DateTime accepts second 61 (ISO)';
ef575e2 [t/spec/S32-temporal/DateTime] Corrected and extended. Rakudo should …
Kodi authored
87 lives_ok { dt second => 61.5 }, 'DateTime accepts second 61.5';
88 lives_ok { dt second => 61.99 }, 'DateTime accepts second 61.99';
89 lives_ok { dt date => Date.new(1999, 1, 1), second => 61.99 }, 'DateTime accepts second 61.99 (with Date)';
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
90 dies_ok { dt second => 62 }, 'DateTime rejects second 62';
91 dies_ok { ds '1999-01-01T12:10:62Z' }, 'DateTime rejects second 62 (ISO)';
92 dies_ok { dt date => Date.new(1999, 1, 1), second => 62 }, 'DateTime rejects second 62 (with Date)';
ef575e2 [t/spec/S32-temporal/DateTime] Corrected and extended. Rakudo should …
Kodi authored
93
94 # L<S32::Temporal/'"Set" methods'/'Just as with the C<new> method, validation'>
95
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
96 {
97 my $dt = dt year => 2007;
98 lives_ok { $dt.set(year => 2000, month => 2, day => 29) }, 'DateTime accepts 29 Feb 2000 (.set)';
99 dies_ok { $dt.set(year => 1900, month => 2, day => 29) }, 'DateTime rejects 29 Feb 1900 (.set)';
100 lives_ok { $dt.set(minute => 59) }, 'DateTime accepts minute 59 (.set)';
101 dies_ok { $dt.set(minute => 60) }, 'DateTime rejects minute 60 (.set)';
102 lives_ok { $dt.set(second => 61.5) }, 'DateTime accepts second 61.5 (.set)';
103 dies_ok { $dt.set(second => 62) }, 'DateTime rejects second 62 (.set)';
104 }
105
106 # TODO: DateTime.new(Instant $i, …)
ef575e2 [t/spec/S32-temporal/DateTime] Corrected and extended. Rakudo should …
Kodi authored
107
ce9b56d [S32] Dropped mention of quarters from Temporal.pod. I don't think qu…
Kodi authored
108 # --------------------------------------------------------------------
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
109 # L<S32::Temporal/'C<DateTime>'/DateTime.new(time)>
ce9b56d [S32] Dropped mention of quarters from Temporal.pod. I don't think qu…
Kodi authored
110 # --------------------------------------------------------------------
ef575e2 [t/spec/S32-temporal/DateTime] Corrected and extended. Rakudo should …
Kodi authored
111
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
112 is show-dt(DateTime.new(0)), '0 0 0 1 1 1970 4', 'DateTime at beginning of Unix epoch';
113 is show-dt(DateTime.new(946684799)), '59 59 23 31 12 1999 5', 'from POSIX at 1999-12-31T23:59:59Z';
114 # last second of previous millennium, FSVO 'millennium'.
115 is show-dt(DateTime.new(946684800)), '0 0 0 1 1 2000 6', 'from POSIX at 2000-01-01T00:00:00Z';
116 # one second later, sing Auld Lang Syne.
117
118 # compare dates for a series of times earlier and later than "now", so
119 # that every test run will use different values
120 {
121 my $t = floor time;
122 my $t1 = $t;
123 my $t2 = $t;
124 # the offset changes all time components and causes overflow/underflow
125 my $offset = ((((7*31+1)*24+10)*60+21)*60+21);
126 for 1, 2, 3 {
127 $t1 -= $offset;
128 my $dt = DateTime.new($t1);
129 is show-dt($dt), join(' ', test-gmtime $t1), "crosscheck $dt";
130 $t2 += $offset;
131 $dt = DateTime.new($t2);
132 is show-dt($dt), join(' ', test-gmtime $t2), "crosscheck $dt";
133 }
134 }
135
136 {
137 my $dt = DateTime.new(946684799,
138 timezone => -(5*60*60 + 55*60),
139 formatter => { .day ~ '/' ~ .month ~ '/' ~ .year ~ ' ' ~
140 .second ~ 's' ~ .minute ~ 'm' ~ .hour ~ 'h' });
141 is ~$dt, '31/12/1999 59s4m18h', 'DateTime.new(Numeric) with time zone and formatter';
142 }
143
144 # --------------------------------------------------------------------
145 # L<S32::Temporal/'C<DateTime>'/A shorter way to send in date>
146 # DateTime.new(Str)
147 # --------------------------------------------------------------------
148
149 is ds('2009-12-31T22:33:44Z'), '2009-12-31T22:33:44Z', 'round-tripping ISO 8601 (Z)';
150 is ds('2009-12-31T22:33:44+0000'), '2009-12-31T22:33:44Z', 'round-tripping ISO 8601 (+0000 to Z)';
151 is ds('2009-12-31T22:33:44+1100'), '2009-12-31T22:33:44+1100', 'round-tripping ISO 8601 (+1100)';
152 is ds('2009-12-31T22:33:44'), '2009-12-31T22:33:44Z', 'DateTime.new(Str) defaults to UTC';
153 is DateTime.new('2009-12-31T22:33:44',
154 timezone => 12*60*60 + 34*60),
155 '2009-12-31T22:33:44+1234', 'DateTime.new(Str) with :timezone';
156 is DateTime.new('2009-12-31T22:33:44',
157 formatter => { ($^dt.hour % 12) ~ 'ish' } ),
158 '10ish', 'DateTime.new(Str) with formatter';
159
160 # --------------------------------------------------------------------
161 # L<S32::Temporal/'C<DateTime>'/'one additional constructor: C<now>'>
162 # --------------------------------------------------------------------
163
164 {
165 my $t = floor time;
166 1 while floor time == $t; # loop until the next second
167 $t = time;
168 my $dt1 = DateTime.new($t);
169 my $dt2 = DateTime.now; # $dt1 and $dt2 might differ very occasionally
170 is show-dt($dt1), show-dt($dt2), 'DateTime.now uses current time';
171
172 $t = floor time;
173 1 while floor time == $t;
174 $t = time;
175 $dt1 = DateTime.new($t);
176 $dt2 = DateTime.now(
177 timezone => 22*60*60,
178 formatter => { ~($^x.hour) });
179 is ~$dt2, ~(($dt1.hour + 22) % 24), 'DateTime.now with time zone and formatter';
180 }
181
182 # --------------------------------------------------------------------
183 # L<S32::Temporal/'"Get" methods'/'the synonym C<day-of-month>'>
184 # --------------------------------------------------------------------
185
186 is dt(day => 18).day-of-month, 18, 'DateTime.day can be spelled as DateTime.day-of-month';
187
188 # TODO: DateTime.Instant
189
190 # --------------------------------------------------------------------
191 # L<S32::Temporal/'"Get" methods'/'the method C<posix>'>
192 # --------------------------------------------------------------------
193
194 {
195 is dt(year => 1970).posix, 0, 'DateTime.posix (1970-01-01T00:00:00Z)';
196 my $dt = dt
197 year => 1970, month => 1, day => 1,
198 hour => 1, minute => 1, second => 1;
199 is $dt.posix, 3661, 'DateTime.posix (1970-01-01T01:01:01Z)';
200 $dt = dt
201 year => 1970, month => 1, day => 1,
202 hour => 1, minute => 1, second => 1,
203 timezone => -1*60*60 -1*60;
204 is $dt.posix, 7321, 'DateTime.posix (1970-01-01T01:01:01-0101)';
205 # round-trip test for the current time
206 my $t = floor time;
207 my @t = test-gmtime $t;
208 $dt = dt
209 year => @t[5], month => @t[4], day => @t[3],
210 hour => @t[2], minute => @t[1], second => @t[0];
211 is $dt.posix, $t, "at $dt, POSIX is {$dt.posix}";
212 }
ef575e2 [t/spec/S32-temporal/DateTime] Corrected and extended. Rakudo should …
Kodi authored
213
ce9b56d [S32] Dropped mention of quarters from Temporal.pod. I don't think qu…
Kodi authored
214 # --------------------------------------------------------------------
ef575e2 [t/spec/S32-temporal/DateTime] Corrected and extended. Rakudo should …
Kodi authored
215 # L<S32::Temporal/'"Get" methods'/'The method C<week>'>
ce9b56d [S32] Dropped mention of quarters from Temporal.pod. I don't think qu…
Kodi authored
216 # --------------------------------------------------------------------
ef575e2 [t/spec/S32-temporal/DateTime] Corrected and extended. Rakudo should …
Kodi authored
217
218 is ymd(1977, 8, 20).week.join(' '), '1977 33', 'DateTime.week (1977-8-20)';
219 is ymd(1977, 8, 20).week-year, 1977, 'DateTime.week (1977-8-20)';
220 is ymd(1977, 8, 20).week-number, 33, 'DateTime.week-number (1977-8-20)';
221 is ymd(1987, 12, 18).week.join(' '), '1987 51', 'DateTime.week (1987-12-18)';
222 is ymd(2020, 5, 4).week.join(' '), '2020 19', 'DateTime.week (2020-5-4)';
223
224 # From http://en.wikipedia.org/w/index.php?title=ISO_week_date&oldid=370553706#Examples
225
226 is ymd(2005, 01, 01).week.join(' '), '2004 53', 'DateTime.week (2005-01-01)';
227 is ymd(2005, 01, 02).week.join(' '), '2004 53', 'DateTime.week (2005-01-02)';
228 is ymd(2005, 12, 31).week.join(' '), '2005 52', 'DateTime.week (2005-12-31)';
229 is ymd(2007, 01, 01).week.join(' '), '2007 1', 'DateTime.week (2007-01-01)';
230 is ymd(2007, 12, 30).week.join(' '), '2007 52', 'DateTime.week (2007-12-30)';
231 is ymd(2007, 12, 30).week-year, 2007, 'DateTime.week (2007-12-30)';
232 is ymd(2007, 12, 30).week-number, 52, 'DateTime.week-number (2007-12-30)';
233 is ymd(2007, 12, 31).week.join(' '), '2008 1', 'DateTime.week (2007-12-31)';
234 is ymd(2008, 01, 01).week.join(' '), '2008 1', 'DateTime.week (2008-01-01)';
235 is ymd(2008, 12, 29).week.join(' '), '2009 1', 'DateTime.week (2008-12-29)';
236 is ymd(2008, 12, 31).week.join(' '), '2009 1', 'DateTime.week (2008-12-31)';
237 is ymd(2009, 01, 01).week.join(' '), '2009 1', 'DateTime.week (2009-01-01)';
238 is ymd(2009, 12, 31).week.join(' '), '2009 53', 'DateTime.week (2009-12-31)';
239 is ymd(2010, 01, 03).week.join(' '), '2009 53', 'DateTime.week (2010-01-03)';
240 is ymd(2010, 01, 03).week-year, 2009, 'DateTime.week-year (2010-01-03)';
241 is ymd(2010, 01, 03).week-number, 53, 'DateTime.week-number (2010-01-03)';
242
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
243 # day-of-week is tested each time show-dt is called.
244
ce9b56d [S32] Dropped mention of quarters from Temporal.pod. I don't think qu…
Kodi authored
245 # --------------------------------------------------------------------
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
246 # L<S32::Temporal/'"Get" methods'/'The C<weekday-of-month> method'>
ce9b56d [S32] Dropped mention of quarters from Temporal.pod. I don't think qu…
Kodi authored
247 # --------------------------------------------------------------------
ef575e2 [t/spec/S32-temporal/DateTime] Corrected and extended. Rakudo should …
Kodi authored
248
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
249 is ymd(1982, 2, 1).weekday-of-month, 1, 'DateTime.weekday-of-month (1982-02-01)';
250 is ymd(1982, 2, 7).weekday-of-month, 1, 'DateTime.weekday-of-month (1982-02-07)';
251 is ymd(1982, 2, 8).weekday-of-month, 2, 'DateTime.weekday-of-month (1982-02-08)';
252 is ymd(1982, 2, 18).weekday-of-month, 3, 'DateTime.weekday-of-month (1982-02-18)';
253 is ymd(1982, 2, 28).weekday-of-month, 4, 'DateTime.weekday-of-month (1982-02-28)';
254 is ymd(1982, 4, 4).weekday-of-month, 1, 'DateTime.weekday-of-month (1982-04-04)';
255 is ymd(1982, 4, 7).weekday-of-month, 1, 'DateTime.weekday-of-month (1982-04-07)';
256 is ymd(1982, 4, 8).weekday-of-month, 2, 'DateTime.weekday-of-month (1982-04-08)';
257 is ymd(1982, 4, 13).weekday-of-month, 2, 'DateTime.weekday-of-month (1982-04-13)';
258 is ymd(1982, 4, 30).weekday-of-month, 5, 'DateTime.weekday-of-month (1982-04-30)';
259
260 # --------------------------------------------------------------------
261 # L<S32::Temporal/'"Get" methods'/'The C<day-of-year> method'>
262 # --------------------------------------------------------------------
263
264 is ymd(1975, 1, 1).day-of-year, 1, 'DateTime.day-of-year (1975-01-01)';
265 is ymd(1977, 5, 5).day-of-year, 125, 'DateTime.day-of-year (1977-05-05)';
266 is ymd(1983, 11, 27).day-of-year, 331, 'DateTime.day-of-year (1983-11-27)';
267 is ymd(1999, 2, 28).day-of-year, 59, 'DateTime.day-of-year (1999-02-28)';
268 is ymd(1999, 3, 1).day-of-year, 60, 'DateTime.day-of-year (1999-03-01)';
269 is ymd(1999, 12, 31).day-of-year, 365, 'DateTime.day-of-year (1999-12-31)';
270 is ymd(2000, 2, 28).day-of-year, 59, 'DateTime.day-of-year (2000-02-28)';
271 is ymd(2000, 2, 29).day-of-year, 60, 'DateTime.day-of-year (2000-02-29)';
272 is ymd(2000, 3, 1).day-of-year, 61, 'DateTime.day-of-year (2000-03-01)';
273 is ymd(2000, 12, 31).day-of-year, 366, 'DateTime.day-of-year (2000-12-31)';
274
275 # --------------------------------------------------------------------
276 # L<S32::Temporal/'"Get" methods'/'The method C<whole-second>'>
277 # --------------------------------------------------------------------
278
279 is dt(second => 22).whole-second, 22, 'DateTime.whole-second (22)';
280 is dt(second => 22.1).whole-second, 22, 'DateTime.whole-second (22.1)';
281 is dt(second => 15.9).whole-second, 15, 'DateTime.whole-second (15.9)';
282 is dt(second => 0).whole-second, 0, 'DateTime.whole-second (0)';
283 is dt(second => 0.9).whole-second, 0, 'DateTime.whole-second (0.9)';
284 is dt(second => 60).whole-second, 60, 'DateTime.whole-second (60)';
285 is dt(second => 60.5).whole-second, 60, 'DateTime.whole-second (60.5)';
286
287 # --------------------------------------------------------------------
288 # L<S32::Temporal/'"Get" methods'/'The C<Date> method'>
289 # --------------------------------------------------------------------
290
291 {
292 my $dt = ymd 2010, 6, 4;
293 my $date;
294 lives_ok { $date = $dt.Date(); }, 'DateTime.Date';
295 isa_ok $date, Date, 'Date object is correct class';
296 is $date.year, 2010, 'Date year';
297 is $date.month, 6, 'Date month';
298 is $date.day, 4, 'Date day';
299 }
300
301 # --------------------------------------------------------------------
302 # L<S32::Temporal/'"Get" methods'/'The method C<offset>'>
303 # --------------------------------------------------------------------
304
305 is tz( 'Z').offset, 0, 'DateTime.offset (Z)';
306 is tz('+0000').offset, 0, 'DateTime.offset (+0000)';
307 is tz('-0000').offset, 0, 'DateTime.offset (-0000)';
308 is tz('+0015').offset, 900, 'DateTime.offset (+0015)';
309 is tz('-0015').offset, -900, 'DateTime.offset (-0015)';
310 is tz('+0700').offset, 25200, 'DateTime.offset (+0700)';
311 is tz('-0700').offset, -25200, 'DateTime.offset (-0700)';
312 is tz('+1433').offset, 52380, 'DateTime.offset (+1433)';
313 is tz('-1433').offset, -52380, 'DateTime.offset (-1433)';
314 is dt(timezone => 3661).offset, 3661, 'DateTime.offset (1 hour, 1 minute, 1 second)';
315
316 {
317 my $tz = sub ($dt, $to-utc) {
318 $to-utc
319 ?? $dt.year == 2001 ?? 1 !! 2
320 !! 3
321 };
322 my $dt = dt year => 2001, timezone => $tz;
323 is $dt.offset, 1, 'DateTime.offset (function, 1)';
324 $dt = dt year => 2000, timezone => $tz;
325 is $dt.offset, 2, 'DateTime.offset (function, 2)';
326 }
ef575e2 [t/spec/S32-temporal/DateTime] Corrected and extended. Rakudo should …
Kodi authored
327
ce9b56d [S32] Dropped mention of quarters from Temporal.pod. I don't think qu…
Kodi authored
328 # --------------------------------------------------------------------
329 # L<S32::Temporal/'"Set" methods'/'The C<truncate> method'>
330 # --------------------------------------------------------------------
331
332 {
333 my $moon-landing = dt # Although the seconds part is fictional.
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
334 year => 1969, month => 7, day => 20,
ce9b56d [S32] Dropped mention of quarters from Temporal.pod. I don't think qu…
Kodi authored
335 hour => 8, minute => 17, second => 32.4;
336
337 my $dt = $moon-landing.clone; $dt.truncate(to => 'second');
338 is $dt.second, 32, 'DateTime.truncate(to => "second")';
339 $dt = $moon-landing.clone; $dt.truncate(to => 'minute');
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
340 is ~$dt, '1969-07-20T08:17:00Z', 'DateTime.truncate(to => "minute")';
ce9b56d [S32] Dropped mention of quarters from Temporal.pod. I don't think qu…
Kodi authored
341 $dt = $moon-landing.clone; $dt.truncate(to => 'hour');
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
342 is ~$dt, '1969-07-20T08:00:00Z', 'DateTime.truncate(to => "hour")';
ce9b56d [S32] Dropped mention of quarters from Temporal.pod. I don't think qu…
Kodi authored
343 $dt = $moon-landing.clone; $dt.truncate(to => 'day');
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
344 is ~$dt, '1969-07-20T00:00:00Z', 'DateTime.truncate(to => "day")';
ce9b56d [S32] Dropped mention of quarters from Temporal.pod. I don't think qu…
Kodi authored
345 $dt = $moon-landing.clone; $dt.truncate(to => 'month');
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
346 is ~$dt, '1969-07-01T00:00:00Z', 'DateTime.truncate(to => "month")';
ce9b56d [S32] Dropped mention of quarters from Temporal.pod. I don't think qu…
Kodi authored
347 $dt = $moon-landing.clone; $dt.truncate(to => 'year');
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
348 is ~$dt, '1969-01-01T00:00:00Z', 'DateTime.truncate(to => "year")';
ce9b56d [S32] Dropped mention of quarters from Temporal.pod. I don't think qu…
Kodi authored
349
350 $dt = ymd 1999, 1, 18; $dt.truncate(to => 'week');
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
351 is ~$dt, '1999-01-18T00:00:00Z', 'DateTime.truncate(to => "week") (no change in day)';
ce9b56d [S32] Dropped mention of quarters from Temporal.pod. I don't think qu…
Kodi authored
352 $dt = ymd 1999, 1, 19; $dt.truncate(to => 'week');
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
353 is ~$dt, '1999-01-18T00:00:00Z', 'DateTime.truncate(to => "week") (short jump)';
ce9b56d [S32] Dropped mention of quarters from Temporal.pod. I don't think qu…
Kodi authored
354 $dt = ymd 1999, 1, 17; $dt.truncate(to => 'week');
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
355 is ~$dt, '1999-01-11T00:00:00Z', 'DateTime.truncate(to => "week") (long jump)';
ce9b56d [S32] Dropped mention of quarters from Temporal.pod. I don't think qu…
Kodi authored
356 $dt = ymd 1999, 4, 2; $dt.truncate(to => 'week');
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
357 is ~$dt, '1999-03-29T00:00:00Z', 'DateTime.truncate(to => "week") (changing month)';
ce9b56d [S32] Dropped mention of quarters from Temporal.pod. I don't think qu…
Kodi authored
358 $dt = ymd 1999, 1, 3; $dt.truncate(to => 'week');
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
359 is ~$dt, '1998-12-28T00:00:00Z', 'DateTime.truncate(to => "week") (changing year)';
ce9b56d [S32] Dropped mention of quarters from Temporal.pod. I don't think qu…
Kodi authored
360 $dt = ymd 2000, 3, 1; $dt.truncate(to => 'week');
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
361 is ~$dt, '2000-02-28T00:00:00Z', 'DateTime.truncate(to => "week") (skipping over Feb 29)';
ce9b56d [S32] Dropped mention of quarters from Temporal.pod. I don't think qu…
Kodi authored
362 $dt = ymd 1988, 3, 3; $dt.truncate(to => 'week');
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
363 is ~$dt, '1988-02-29T00:00:00Z', 'DateTime.truncate(to => "week") (landing on Feb 29)';
ce9b56d [S32] Dropped mention of quarters from Temporal.pod. I don't think qu…
Kodi authored
364 }
365
366 # --------------------------------------------------------------------
367 # L<S32::Temporal/'"Set" methods'/adjust the time zone>
368 # --------------------------------------------------------------------
369
370 # We use $dt.set(timezone => FOO) instead of $dt.timezone = FOO
371 # because Rakudo hasn't yet implemented the latter.
372
373 {
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
374 sub set-tz($dt, $hours, $minutes=0, $seconds=0) {
375 $dt.set(timezone => $hours*60*60 + $minutes*60 + $seconds);
376 }
377 sub hms($dt) {
378 $dt.hour ~ ',' ~ $dt.minute ~ ',' ~ $dt.second
379 }
380
381 my $dt = tz '+0200'; set-tz($dt, 4);
382 is ~$dt, '2005-02-04T17:25:00+0400', 'Changing time zones (adding hours)';
383 $dt = tz '+0000'; set-tz($dt, -1);
384 is ~$dt, '2005-02-04T14:25:00-0100', 'Changing time zones (subtracting hours)';
385 $dt = tz '-0100'; set-tz($dt, 0);
386 is ~$dt, '2005-02-04T16:25:00Z', 'Changing time zones (-0100 to UTC)';
387 $dt = tz '+0100'; set-tz($dt, -1);
388 is ~$dt, '2005-02-04T13:25:00-0100', 'Changing time zones (+ hours to - hours)';
389 $dt = tz '-0200'; set-tz($dt, -5);
390 is ~$dt, '2005-02-04T12:25:00-0500', 'Changing time zones (decreasing negative hours)';
391 $dt = tz '+0000'; set-tz($dt, 0, -13);
392 is ~$dt, '2005-02-04T15:12:00-0013', 'Changing time zones (negative minutes)';
393 $dt = tz '+0000'; set-tz($dt, 0, 0, -5);
394 is hms($dt), '15,24,55', 'Changing time zones (negative seconds)';
395 $dt = tz '+0000'; set-tz($dt, 0, -27);
396 is ~$dt, '2005-02-04T14:58:00-0027', 'Changing time zones (hour rollover 1)';
397 $dt = tz '+0000'; set-tz($dt, 0, 44);
398 is ~$dt, '2005-02-04T16:09:00+0044', 'Changing time zones (hour rollover 2)';
399 $dt = tz '+0311'; set-tz($dt, -2, -27);
400 is ~$dt, '2005-02-04T09:47:00-0227', 'Changing time zones (hours and minutes)';
401 $dt = tz '+0311'; set-tz($dt, -2, -27, -19);
402 is hms($dt), '9,46,41', 'Changing time zones (hours, minutes, and seconds)';
403 $dt = tz '+0000'; set-tz($dt, -18, -55);
404 is ~$dt, '2005-02-03T20:30:00-1855', 'Changing time zones (one-day rollover)';
405 $dt = tz '-1611'; set-tz($dt, 16, 55);
406 is ~$dt, '2005-02-06T00:31:00+1655', 'Changing time zones (two-day rollover)';
407 $dt = ds '2005-01-01T02:22:00+0300'; set-tz($dt, 0, 35);
408 is ~$dt, '2004-12-31T23:57:00+0035', 'Changing time zones (year rollover)';
409
410 $dt = dt year => 2005, month => 1, day => 3,
411 hour => 2, minute => 22, second => 4,
412 timezone => 13;
413 $dt.set(timezone => -529402);
414 # A difference from UTC of 6 days, 3 hours, 3 minutes, and
415 # 22 seconds.
416 is show-dt($dt), '29 18 23 27 12 2004 1', 'Changing time zones (big rollover)';
417
418 # DateTime doesn't implement DST, but it ought to make
419 # implementing DST possible. We test that here.
420
421 sub nyc-tz($dt, $to-utc) { # America/New_York
422 3600 * (us2007dst($dt, $to-utc ?? 2 !! 7) ?? -4 !! -5)
423 }
424
425 sub lax-tz($dt, $to-utc) { # America/Los_Angeles
426 3600 * (us2007dst($dt, $to-utc ?? 2 !! 10) ?? -7 !! -8)
427 }
428
429 sub us2007dst($dt, $critical-hour) {
430 $dt.month > 3
431 || $dt.month == 3
432 && ($dt.day > 11
433 || $dt.day == 11 && $dt.hour >= $critical-hour)
434 and $dt.month < 11
435 || $dt.month == 11
436 && ($dt.day < 4
437 || $dt.day == 4 && $dt.hour < $critical-hour)
438 }
439
440 sub nyc-dt($year, $month, $day, $hour, $minute) {
441 dt :$year, :$month, :$day, :$hour, :$minute,
442 timezone => &nyc-tz
443 }
444
445 $dt = ds '2007-01-02T02:22:00Z'; $dt.set(timezone => &nyc-tz);
446 is ~$dt, '2007-01-01T21:22:00-0500', 'Changing time zones (UTC to NYC, outside of DST)';
447 $dt = ds '2007-08-01T02:22:00Z'; $dt.set(timezone => &nyc-tz);
448 is ~$dt, '2007-07-31T22:22:00-0400', 'Changing time zones (UTC to NYC, during DST)';
449 $dt = ds '2007-03-11T06:55:00Z'; $dt.set(timezone => &nyc-tz);
450 is ~$dt, '2007-03-11T01:55:00-0500', 'Changing time zones (UTC to NYC, just before DST)';
451 $dt = ds '2007-03-11T07:02:00Z'; $dt.set(timezone => &nyc-tz);
452 is ~$dt, '2007-03-11T03:02:00-0400', 'Changing time zones (UTC to NYC, just after DST)';
453 $dt = ds '2007-03-11T09:58:00+0303'; $dt.set(timezone => &nyc-tz);
454 is ~$dt, '2007-03-11T01:55:00-0500', 'Changing time zones (+0303 to NYC, just before DST)';
455 $dt = ds '2007-03-10T14:50:00-1612'; $dt.set(timezone => &nyc-tz);
456 is ~$dt, '2007-03-11T03:02:00-0400', 'Changing time zones (-1612 to NYC, just after DST)';
457 $dt = nyc-dt 2007, 1, 1, 21, 22; $dt.set(timezone => 0);
458 is ~$dt, '2007-01-02T02:22:00Z', 'Changing time zones (NYC to UTC, outside of DST)';
459 $dt = nyc-dt 2007, 7, 31, 22, 22; $dt.set(timezone => 0);
460 is ~$dt, '2007-08-01T02:22:00Z', 'Changing time zones (NYC to UTC, during DST)';
461 $dt = nyc-dt 2007, 3, 11, 1, 55; $dt.set(timezone => 0);
462 is ~$dt, '2007-03-11T06:55:00Z', 'Changing time zones (NYC to UTC, just before DST)';
463 $dt = nyc-dt 2007, 3, 11, 3, 2; set-tz($dt, -16, -12);
464 is ~$dt, '2007-03-10T14:50:00-1612', 'Changing time zones (NYC to -1612, just after DST)';
465 $dt = nyc-dt 2007, 3, 11, 1, 55; $dt.set(timezone => &lax-tz);
466 is ~$dt, '2007-03-10T22:55:00-0800', 'Changing time zones (NYC to LAX, just before NYC DST)';
467 $dt = nyc-dt 2007, 3, 11, 3, 2; $dt.set(timezone => &lax-tz);
468 is ~$dt, '2007-03-10T23:02:00-0800', 'Changing time zones (NYC to LAX, just after NYC DST)';
469 $dt = nyc-dt 2007, 3, 11, 6, 2; $dt.set(timezone => &lax-tz);
470 is ~$dt, '2007-03-11T03:02:00-0700', 'Changing time zones (NYC to LAX, just after LAX DST)';
ce9b56d [S32] Dropped mention of quarters from Temporal.pod. I don't think qu…
Kodi authored
471 }
472
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
473 # --------------------------------------------------------------------
474 # L<S32::Temporal/'"Set" methods'/'C<set> and C<truncate> return'>
475 # ...the calling object
476 # --------------------------------------------------------------------
ce9b56d [S32] Dropped mention of quarters from Temporal.pod. I don't think qu…
Kodi authored
477
f03f623 [t/spec] Overhauled DateTime.t. Rakudo can pass all of these with my …
Kodi authored
478 {
479 my $dt = ymd 1998, 4, 2;
480 my $same-dt = $dt.set(year => 1997);
481 $same-dt.set(month => 3);
482 is ~$dt, '1997-03-02T00:00:00Z', 'DateTime.set returns the calling object';
483
484 $dt = ymd 1998, 1, 30;
485 $same-dt = $dt.truncate(to => 'month');
486 $same-dt.truncate(to => 'week');
487 is $dt.year, 1997, 'DateTime.truncate returns the calling object';
488 }
ef575e2 [t/spec/S32-temporal/DateTime] Corrected and extended. Rakudo should …
Kodi authored
489
490 done_testing;
491
7d2cc0e [spec/S32-temporal/DateTime.t] renamed from Temporal.t, add 2 strftim…
mberends authored
492 # vim: ft=perl6
Something went wrong with that request. Please try again.