Debian Codegraph @

t/09changes.t

raw
1 use strict;
2 use warnings;
3
4 use lib 't/lib';
5 use T::RequireDateTime;
6
7 use DateTime::Duration;
8 use Test::More;
9 use Test::Fatal;
10
11 # The point of this group of tests is to try to check that DST changes
12 # are occurring at exactly the right time in various time zones. It's
13 # important to check both pre-generated spans, as well as spans that
14 # have to be generated on the fly.
15
16 # Rule AN 1996 max - Mar lastSun 2:00s 0 -
17 # Rule AN 2000 only - Aug lastSun 2:00s 1:00 -
18 # Rule AN 2001 max - Oct lastSun 2:00s 1:00 -
19 # Zone NAME GMTOFF RULES FORMAT [UNTIL]
20 # Zone Australia/Sydney 10:04:52 - LMT 1895 Feb
21 # 10:00 Aus EST 1971
22 # 10:00 AN EST
23
24 {
25
26 # one minute before change to standard time
27 my $dt = DateTime->new(
28 year => 1997, month => 3, day => 29,
29 hour => 15, minute => 59,
30 time_zone => 'UTC'
31 );
32
33 $dt->set_time_zone('Australia/Sydney');
34
35 is( $dt->hour, 2, 'A/S 1997: hour should be 2' );
36
37 $dt->set_time_zone('UTC')
38 ->add( minutes => 1 )
39 ->set_time_zone('Australia/Sydney');
40
41 is( $dt->hour, 2, 'A/S 1997: hour should still be 2' );
42 }
43
44 # same tests without using UTC as intermediate
45 {
46
47 # Can't start at 1:59 or we get the _2nd_ 1:59 of that day (post-DST change)
48 my $dt = DateTime->new(
49 year => 1997, month => 3, day => 30,
50 hour => 1, minute => 59,
51 time_zone => 'Australia/Sydney'
52 );
53
54 $dt->add( hours => 1 );
55
56 is( $dt->hour, 2, 'A/S 1997: hour should be 2' );
57
58 $dt->add( minutes => 1 );
59
60 is( $dt->hour, 2, 'A/S 1997: hour should still be 2' );
61 }
62
63 {
64
65 # one minute before change to standard time
66 my $dt = DateTime->new(
67 year => 2002, month => 10, day => 26,
68 hour => 15, minute => 59,
69 time_zone => 'UTC'
70 );
71
72 $dt->set_time_zone('Australia/Sydney');
73
74 is( $dt->hour, 1, 'A/S 2002: hour should be 1' );
75
76 $dt->set_time_zone('UTC')
77 ->add( minutes => 1 )
78 ->set_time_zone('Australia/Sydney');
79
80 is( $dt->hour, 3, 'A/S 2002: hour should be 3' );
81
82 }
83
84 # same tests without using UTC as intermediate
85 {
86 my $dt = DateTime->new(
87 year => 2002, month => 10, day => 27,
88 hour => 1, minute => 59,
89 time_zone => 'Australia/Sydney'
90 );
91
92 is( $dt->hour, 1, 'A/S 2002: hour should be 1' );
93
94 $dt->add( minutes => 1 );
95
96 is( $dt->hour, 3, 'A/S 2002: hour should be 3' );
97 }
98
99 # do same tests with future dates so more data is generated
100 {
101
102 # Can't start at 1:59 or we get the _2nd_ 1:59 of that day (post-DST change)
103 my $dt = DateTime->new(
104 year => 2040, month => 4, day => 1,
105 hour => 1, minute => 59,
106 time_zone => 'Australia/Sydney'
107 );
108
109 $dt->add( hours => 1 );
110
111 is( $dt->hour, 2, 'A/S 2040: hour should be 2' );
112
113 $dt->add( minutes => 1 );
114
115 is( $dt->hour, 2, 'A/S 2040: hour should still be 2' );
116 }
117
118 {
119 my $dt = DateTime->new(
120 year => 2040, month => 10, day => 7,
121 hour => 1, minute => 59,
122 time_zone => 'Australia/Sydney'
123 );
124
125 is( $dt->hour, 1, 'A/S 2040: hour should be 1' );
126
127 $dt->add( minutes => 1 );
128
129 is( $dt->hour, 3, 'A/S 2040: hour should be 3' );
130 }
131
132 # Rule EU 1981 max - Mar lastSun 1:00u 1:00 S
133 # Rule EU 1996 max - Oct lastSun 1:00u 0 -
134 {
135
136 # one minute before change to standard time
137 my $dt = DateTime->new(
138 year => 1982, month => 3, day => 28,
139 hour => 0, minute => 59,
140 time_zone => 'UTC'
141 );
142
143 $dt->set_time_zone('Europe/Vienna');
144
145 is( $dt->hour, 1, 'E/V 1982: hour should be 1' );
146
147 $dt->set_time_zone('UTC')
148 ->add( minutes => 1 )
149 ->set_time_zone('Europe/Vienna');
150
151 is( $dt->hour, 3, 'E/V 1982: hour should be 3' );
152 }
153
154 # same tests without using UTC as intermediate
155 {
156
157 my $dt = DateTime->new(
158 year => 1982, month => 3, day => 28,
159 hour => 1, minute => 59,
160 time_zone => 'Europe/Vienna'
161 );
162
163 is( $dt->hour, 1, 'E/V 1982: hour should be 1' );
164
165 $dt->add( minutes => 1 );
166
167 is( $dt->hour, 3, 'E/V 1982: hour should be 3' );
168 }
169
170 {
171
172 # one minute before change to standard time
173 my $dt = DateTime->new(
174 year => 1997, month => 10, day => 26,
175 hour => 0, minute => 59,
176 time_zone => 'UTC'
177 );
178
179 $dt->set_time_zone('Europe/Vienna');
180
181 is( $dt->hour, 2, 'E/V 1997: hour should be 2' );
182
183 $dt->set_time_zone('UTC')
184 ->add( minutes => 1 )
185 ->set_time_zone('Europe/Vienna');
186
187 is( $dt->hour, 2, 'E/V 1997: hour should still be 2' );
188
189 }
190
191 # same tests without using UTC as intermediate
192 {
193
194 # can't be created directly because of overlap between changes
195 my $dt = DateTime->new(
196 year => 1997, month => 10, day => 26,
197 hour => 1, minute => 59,
198 time_zone => 'Europe/Vienna'
199 );
200
201 $dt->add( hours => 1 );
202
203 is( $dt->hour, 2, 'E/V 1997: hour should be 2' );
204
205 $dt->add( minutes => 1 );
206
207 is( $dt->hour, 2, 'E/V 1997: hour should still be 2' );
208 }
209
210 # future
211 {
212 my $dt = DateTime->new(
213 year => 2040, month => 3, day => 25,
214 hour => 1, minute => 59,
215 time_zone => 'Europe/Vienna'
216 );
217
218 is( $dt->hour, 1, 'E/V 2040: hour should be 1' );
219
220 $dt->add( minutes => 1 );
221
222 is( $dt->hour, 3, 'E/V 2040: hour should be 3' );
223 }
224
225 {
226 my $dt = DateTime->new(
227 year => 2040, month => 10, day => 28,
228 hour => 1, minute => 59,
229 time_zone => 'Europe/Vienna'
230 );
231
232 $dt->add( hours => 1 );
233
234 is( $dt->hour, 2, 'E/V 2040: hour should be 2' );
235
236 $dt->add( minutes => 1 );
237
238 is( $dt->hour, 2, 'E/V 2040: hour should still be 2' );
239 }
240
241 # Africa/Algiers has an observance that ends at 1977-10-21T00:00:00
242 # local time and a rule that starts at exactly the same time
243
244 # Rule Algeria 1977 only - May 6 0:00 1:00 S
245 # Rule Algeria 1977 only - Oct 21 0:00 0 -
246 #
247 # 0:00 Algeria WE%sT 1977 Oct 21
248 # 1:00 Algeria CE%sT 1979 Oct 26
249 {
250 my $dt = DateTime->new(
251 year => 1977, month => 10, day => 20,
252 hour => 23, minute => 59,
253 time_zone => 'Africa/Algiers'
254 );
255
256 is( $dt->time_zone_short_name, 'WEST', 'short name is WEST' );
257 is( $dt->is_dst, 1, 'is dst' );
258
259 # observance ends, new rule starts, net effect is same offset,
260 # different short name, no longer is DST
261 $dt->add( minutes => 1 );
262
263 is( $dt->time_zone_short_name, 'CET', 'short name is CET' );
264 is( $dt->is_dst, 0, 'is not dst' );
265 }
266
267 {
268 my $dt = DateTime->new(
269 year => 2000, month => 10, day => 5,
270 hour => 15, time_zone => 'America/Chicago',
271 );
272 is( $dt->hour, 15, 'hour is 15' );
273 is( $dt->offset, -18000, 'offset is -18000' );
274 is( $dt->is_dst, 1, 'is dst' );
275
276 $dt->set_time_zone('America/New_York');
277 is( $dt->offset, -14400, 'offset is -14400' );
278 is( $dt->is_dst, 1, 'is dst' );
279 is(
280 $dt->hour, 16,
281 'America/New_York is exactly one hour later than America/Chicago - hour'
282 );
283 is(
284 $dt->minute, 0,
285 'America/New_York is exactly one hour later than America/Chicago - minute'
286 );
287 is(
288 $dt->second, 0,
289 'America/New_York is exactly one hour later than America/Chicago - second'
290 );
291 }
292
293 {
294
295 # this is the second of the two 01:59:59 times for that date
296 my $dt = DateTime->new(
297 year => 2003, month => 10, day => 26,
298 hour => 1, minute => 59, second => 59,
299 time_zone => 'America/Chicago',
300 );
301
302 is( $dt->offset, -21600, 'offset should be -21600' );
303 is( $dt->is_dst, 0, 'is not dst' );
304
305 $dt->subtract( hours => 1 );
306
307 is( $dt->offset, -18000, 'offset should be -18000' );
308 is( $dt->is_dst, 1, 'is not dst' );
309 is( $dt->hour, 1, 'crossing DST boundary does not change local hour' );
310 }
311
312 {
313 my $dt = DateTime->new(
314 year => 2003, month => 10, day => 26,
315 hour => 2, time_zone => 'America/Chicago',
316 );
317
318 is( $dt->offset, -21600, 'offset should be -21600' );
319 }
320
321 {
322 my $dt = DateTime->new(
323 year => 2003, month => 10, day => 26,
324 hour => 3, time_zone => 'America/Chicago',
325 );
326
327 is( $dt->offset, -21600, 'offset should be -21600' );
328 }
329
330 {
331 like(
332 exception {
333 DateTime->new(
334 year => 2003, month => 4, day => 6,
335 hour => 2, time_zone => 'America/Chicago',
336 );
337 },
338 qr/Invalid local time .+/,
339 'exception for invalid time'
340 );
341
342 like(
343 exception {
344 DateTime->new(
345 year => 2003, month => 4, day => 6,
346 hour => 2, minute => 59, second => 59,
347 time_zone => 'America/Chicago',
348 );
349 },
350 qr/Invalid local time .+/,
351 'exception for invalid time'
352 );
353 }
354
355 {
356 is(
357 exception {
358 DateTime->new(
359 year => 2003, month => 4, day => 6,
360 hour => 1, minute => 59, second => 59,
361 time_zone => 'America/Chicago',
362 );
363 },
364 undef,
365 'no exception for valid time'
366 );
367
368 SKIP:
369 {
370 skip
371 'DateTime 0.29 has a date math bug that causes this test to fail',
372 1
373 if ( DateTime->VERSION >= 0.29 && DateTime->VERSION < 0.30 );
374
375 my $dt = DateTime->new(
376 year => 2003, month => 4, day => 5,
377 hour => 2,
378 time_zone => 'America/Chicago',
379 );
380
381 like(
382 exception { $dt->add( days => 1 ) },
383 qr/Invalid local time .+/,
384 'exception for invalid time produced via add'
385 );
386 }
387 }
388
389 {
390 my $dt = DateTime->new(
391 year => 2003, month => 4, day => 5,
392 hour => 2,
393 time_zone => 'America/Chicago',
394 );
395 is(
396 exception { $dt->add( hours => 24 ) },
397 undef,
398 'add 24 hours should work even if add 1 day does not'
399 );
400
401 is( $dt->hour, 3, 'hour should no be 3' );
402 }
403
404 {
405 my $dt = DateTime->new(
406 year => 2003, month => 4, day => 6,
407 hour => 3, time_zone => 'America/Chicago',
408 );
409
410 is( $dt->hour, 3, 'hour should be 3' );
411 is( $dt->offset, -18000, 'offset should be -18000' );
412
413 $dt->subtract( seconds => 1 );
414
415 is( $dt->hour, 1, 'hour should be 1' );
416 is( $dt->offset, -21600, 'offset should be -21600' );
417 }
418
419 {
420 my $dt = DateTime->new(
421 year => 2003, month => 4, day => 6,
422 hour => 3, time_zone => 'floating',
423 );
424 $dt->set_time_zone('America/Chicago');
425
426 is( $dt->hour, 3, 'hour should be 3 after switching from floating TZ' );
427 is(
428 $dt->offset, -18000,
429 'tz offset should be -18000'
430 );
431 }
432
433 {
434 my $dt = DateTime->new(
435 year => 2003, month => 4, day => 6,
436 hour => 3, time_zone => 'America/Chicago',
437 );
438 $dt->set_time_zone('floating');
439
440 is( $dt->hour, 3, 'hour should be 3 after switching to floating TZ' );
441 is(
442 $dt->local_rd_as_seconds - $dt->utc_rd_as_seconds, 0,
443 'tz offset should be 0'
444 );
445 }
446
447 {
448 like(
449 exception {
450 DateTime->new(
451 year => 2040, month => 3, day => 11,
452 hour => 2, minute => 59, second => 59,
453 time_zone => 'America/Chicago',
454 );
455 },
456 qr/Invalid local time .+/,
457 'exception for invalid time'
458 );
459 }
460
461 {
462 my $dt = DateTime->new(
463 year => 2001, month => 10, day => 28,
464 hour => 0, minute => 59,
465 time_zone => 'UTC'
466 );
467
468 $dt->set_time_zone('Europe/Vienna');
469
470 is( $dt->hour, 2, 'hour should be 2 in vienna at 00:59:00 UTC' );
471
472 $dt->set_time_zone('UTC')
473 ->add( minutes => 1 )
474 ->set_time_zone('Europe/Vienna');
475
476 is( $dt->hour, 2, 'hour should be 2 in vienna at 01:00:00 UTC' );
477 }
478
479 {
480
481 # Doing this triggered a recursion bug in earlier versions of
482 # DateTime::TimeZone.
483 local $ENV{TZ} = 'America/Chicago';
484
485 my $local_tz = DateTime::TimeZone->new( name => 'America/Chicago' );
486 my $utc_tz = DateTime::TimeZone->new( name => 'UTC' );
487
488 my $dt = DateTime->new( year => 2050, time_zone => $local_tz );
489
490 my $sixm = DateTime::Duration->new( months => 6 );
491 foreach (
492 [ 2050, 7, 1, 1, 'CDT' ],
493 [ 2051, 1, 1, 0, 'CST' ],
494 [ 2051, 7, 1, 1, 'CDT' ],
495 [ 2052, 1, 1, 0, 'CST' ],
496 [ 2052, 7, 1, 1, 'CDT' ],
497 [ 2053, 1, 1, 0, 'CST' ],
498 [ 2053, 7, 1, 1, 'CDT' ],
499 [ 2054, 1, 1, 0, 'CST' ],
500 [ 2054, 7, 1, 1, 'CDT' ],
501 [ 2055, 1, 1, 0, 'CST' ],
502 [ 2055, 7, 1, 1, 'CDT' ],
503 [ 2056, 1, 1, 0, 'CST' ],
504 [ 2056, 7, 1, 1, 'CDT' ],
505 [ 2057, 1, 1, 0, 'CST' ],
506 [ 2057, 7, 1, 1, 'CDT' ],
507 [ 2058, 1, 1, 0, 'CST' ],
508 [ 2058, 7, 1, 1, 'CDT' ],
509 [ 2059, 1, 1, 0, 'CST' ],
510 [ 2059, 7, 1, 1, 'CDT' ],
511 [ 2060, 1, 1, 0, 'CST' ],
512 [ 2060, 7, 1, 1, 'CDT' ],
513 ) {
514 $dt->set_time_zone($utc_tz);
515
516 $dt->add_duration($sixm);
517
518 $dt->set_time_zone($local_tz);
519
520 $_->[1] = sprintf( '%02d', $_->[1] );
521
522 my $expect = join q{ }, @{$_};
523
524 is(
525 $dt->strftime('%Y %m%e%k %Z'), $expect,
526 "datetime is $expect"
527 );
528 }
529 }
530
531 {
532 my $local_tz = DateTime::TimeZone->new( name => 'America/New_York' );
533 my $utc_tz = DateTime::TimeZone->new( name => 'UTC' );
534
535 my $dt = DateTime->new( year => 2060, time_zone => $local_tz );
536
537 my $neg_sixm = DateTime::Duration->new( months => -6 );
538 foreach (
539 [ 2059, 7, 1, 1, 'EDT' ],
540 [ 2059, 1, 1, 0, 'EST' ],
541 [ 2058, 7, 1, 1, 'EDT' ],
542 [ 2058, 1, 1, 0, 'EST' ],
543 [ 2057, 7, 1, 1, 'EDT' ],
544 [ 2057, 1, 1, 0, 'EST' ],
545 [ 2056, 7, 1, 1, 'EDT' ],
546 [ 2056, 1, 1, 0, 'EST' ],
547 [ 2055, 7, 1, 1, 'EDT' ],
548 [ 2055, 1, 1, 0, 'EST' ],
549 [ 2054, 7, 1, 1, 'EDT' ],
550 [ 2054, 1, 1, 0, 'EST' ],
551 [ 2053, 7, 1, 1, 'EDT' ],
552 [ 2053, 1, 1, 0, 'EST' ],
553 [ 2052, 7, 1, 1, 'EDT' ],
554 [ 2052, 1, 1, 0, 'EST' ],
555 [ 2051, 7, 1, 1, 'EDT' ],
556 [ 2051, 1, 1, 0, 'EST' ],
557 [ 2050, 7, 1, 1, 'EDT' ],
558 [ 2050, 1, 1, 0, 'EST' ],
559 ) {
560 $dt->set_time_zone($utc_tz);
561
562 $dt->add_duration($neg_sixm);
563
564 $dt->set_time_zone($local_tz);
565
566 $_->[1] = sprintf( '%02d', $_->[1] );
567
568 my $expect = join q{ }, @{$_};
569
570 is(
571 $dt->strftime('%Y %m%e%k %Z'), $expect,
572 "datetime is $expect"
573 );
574 }
575 }
576
577 done_testing();