File: 09changes.t

package info (click to toggle)
libdatetime-timezone-perl 1%3A2.47-1%2B2023d
  • links: PTS, VCS
  • area: main
  • in suites: bullseye-updates
  • size: 97,508 kB
  • sloc: perl: 2,688; sh: 36; makefile: 10
file content (572 lines) | stat: -rw-r--r-- 14,771 bytes parent folder | download | duplicates (3)
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
use strict;
use warnings;

use lib 't/lib';
use T::RequireDateTime;

use DateTime::Duration;
use Test::More;
use Test::Fatal;

# The point of this group of tests is to try to check that DST changes
# are occuring at exactly the right time in various time zones.  It's
# important to check both pre-generated spans, as well as spans that
# have to be generated on the fly.

# Rule	AN	1996	max	-	Mar	lastSun	2:00s	0	-
# Rule	AN	2000	only	-	Aug	lastSun	2:00s	1:00	-
# Rule	AN	2001	max	-	Oct	lastSun	2:00s	1:00	-
# Zone	NAME		GMTOFF	RULES	FORMAT	[UNTIL]
# Zone Australia/Sydney	10:04:52 -	LMT	1895 Feb
# 			10:00	Aus	EST	1971
# 			10:00	AN	EST

{

    # one minute before change to standard time
    my $dt = DateTime->new(
        year      => 1997, month  => 3, day => 29,
        hour      => 15,   minute => 59,
        time_zone => 'UTC'
    );

    $dt->set_time_zone('Australia/Sydney');

    is( $dt->hour, 2, 'A/S 1997: hour should be 2' );

    $dt->set_time_zone('UTC')->add( minutes => 1 )
        ->set_time_zone('Australia/Sydney');

    is( $dt->hour, 2, 'A/S 1997: hour should still be 2' );
}

# same tests without using UTC as intermediate
{

    # Can't start at 1:59 or we get the _2nd_ 1:59 of that day (post-DST change)
    my $dt = DateTime->new(
        year      => 1997, month  => 3, day => 30,
        hour      => 1,    minute => 59,
        time_zone => 'Australia/Sydney'
    );

    $dt->add( hours => 1 );

    is( $dt->hour, 2, 'A/S 1997: hour should be 2' );

    $dt->add( minutes => 1 );

    is( $dt->hour, 2, 'A/S 1997: hour should still be 2' );
}

{

    # one minute before change to standard time
    my $dt = DateTime->new(
        year      => 2002, month  => 10, day => 26,
        hour      => 15,   minute => 59,
        time_zone => 'UTC'
    );

    $dt->set_time_zone('Australia/Sydney');

    is( $dt->hour, 1, 'A/S 2002: hour should be 1' );

    $dt->set_time_zone('UTC')->add( minutes => 1 )
        ->set_time_zone('Australia/Sydney');

    is( $dt->hour, 3, 'A/S 2002: hour should be 3' );

}

# same tests without using UTC as intermediate
{
    my $dt = DateTime->new(
        year      => 2002, month  => 10, day => 27,
        hour      => 1,    minute => 59,
        time_zone => 'Australia/Sydney'
    );

    is( $dt->hour, 1, 'A/S 2002: hour should be 1' );

    $dt->add( minutes => 1 );

    is( $dt->hour, 3, 'A/S 2002: hour should be 3' );
}

# do same tests with future dates so more data is generated
{

    # Can't start at 1:59 or we get the _2nd_ 1:59 of that day (post-DST change)
    my $dt = DateTime->new(
        year      => 2040, month  => 4, day => 1,
        hour      => 1,    minute => 59,
        time_zone => 'Australia/Sydney'
    );

    $dt->add( hours => 1 );

    is( $dt->hour, 2, 'A/S 2040: hour should be 2' );

    $dt->add( minutes => 1 );

    is( $dt->hour, 2, 'A/S 2040: hour should still be 2' );
}

{
    my $dt = DateTime->new(
        year      => 2040, month  => 10, day => 7,
        hour      => 1,    minute => 59,
        time_zone => 'Australia/Sydney'
    );

    is( $dt->hour, 1, 'A/S 2040: hour should be 1' );

    $dt->add( minutes => 1 );

    is( $dt->hour, 3, 'A/S 2040: hour should be 3' );
}

# Rule	EU	1981	max	-	Mar	lastSun	 1:00u	1:00	S
# Rule	EU	1996	max	-	Oct	lastSun	 1:00u	0	-
{

    # one minute before change to standard time
    my $dt = DateTime->new(
        year      => 1982, month  => 3, day => 28,
        hour      => 0,    minute => 59,
        time_zone => 'UTC'
    );

    $dt->set_time_zone('Europe/Vienna');

    is( $dt->hour, 1, 'E/V 1982: hour should be 1' );

    $dt->set_time_zone('UTC')->add( minutes => 1 )
        ->set_time_zone('Europe/Vienna');

    is( $dt->hour, 3, 'E/V 1982: hour should be 3' );
}

# same tests without using UTC as intermediate
{

    my $dt = DateTime->new(
        year      => 1982, month  => 3, day => 28,
        hour      => 1,    minute => 59,
        time_zone => 'Europe/Vienna'
    );

    is( $dt->hour, 1, 'E/V 1982: hour should be 1' );

    $dt->add( minutes => 1 );

    is( $dt->hour, 3, 'E/V 1982: hour should be 3' );
}

{

    # one minute before change to standard time
    my $dt = DateTime->new(
        year      => 1997, month  => 10, day => 26,
        hour      => 0,    minute => 59,
        time_zone => 'UTC'
    );

    $dt->set_time_zone('Europe/Vienna');

    is( $dt->hour, 2, 'E/V 1997: hour should be 2' );

    $dt->set_time_zone('UTC')->add( minutes => 1 )
        ->set_time_zone('Europe/Vienna');

    is( $dt->hour, 2, 'E/V 1997: hour should still be 2' );

}

# same tests without using UTC as intermediate
{

    # can't be created directly because of overlap between changes
    my $dt = DateTime->new(
        year      => 1997, month  => 10, day => 26,
        hour      => 1,    minute => 59,
        time_zone => 'Europe/Vienna'
    );

    $dt->add( hours => 1 );

    is( $dt->hour, 2, 'E/V 1997: hour should be 2' );

    $dt->add( minutes => 1 );

    is( $dt->hour, 2, 'E/V 1997: hour should still be 2' );
}

# future
{
    my $dt = DateTime->new(
        year      => 2040, month  => 3, day => 25,
        hour      => 1,    minute => 59,
        time_zone => 'Europe/Vienna'
    );

    is( $dt->hour, 1, 'E/V 2040: hour should be 1' );

    $dt->add( minutes => 1 );

    is( $dt->hour, 3, 'E/V 2040: hour should be 3' );
}

{
    my $dt = DateTime->new(
        year      => 2040, month  => 10, day => 28,
        hour      => 1,    minute => 59,
        time_zone => 'Europe/Vienna'
    );

    $dt->add( hours => 1 );

    is( $dt->hour, 2, 'E/V 2040: hour should be 2' );

    $dt->add( minutes => 1 );

    is( $dt->hour, 2, 'E/V 2040: hour should still be 2' );
}

# Africa/Algiers has an observance that ends at 1977-10-21T00:00:00
# local time and a rule that starts at exactly the same time

# Rule	Algeria	1977	only	-	May	 6	 0:00	1:00	S
# Rule	Algeria	1977	only	-	Oct	21	 0:00	0	-
#
# 			0:00	Algeria	WE%sT	1977 Oct 21
# 			1:00	Algeria	CE%sT	1979 Oct 26
{
    my $dt = DateTime->new(
        year      => 1977, month  => 10, day => 20,
        hour      => 23,   minute => 59,
        time_zone => 'Africa/Algiers'
    );

    is( $dt->time_zone_short_name, 'WEST', 'short name is WEST' );
    is( $dt->is_dst,               1,      'is dst' );

    # observance ends, new rule starts, net effect is same offset,
    # different short name, no longer is DST
    $dt->add( minutes => 1 );

    is( $dt->time_zone_short_name, 'CET', 'short name is CET' );
    is( $dt->is_dst,               0,     'is not dst' );
}

{
    my $dt = DateTime->new(
        year => 2000, month     => 10, day => 5,
        hour => 15,   time_zone => 'America/Chicago',
    );
    is( $dt->hour,   15,     'hour is 15' );
    is( $dt->offset, -18000, 'offset is -18000' );
    is( $dt->is_dst, 1,      'is dst' );

    $dt->set_time_zone('America/New_York');
    is( $dt->offset, -14400, 'offset is -14400' );
    is( $dt->is_dst, 1,      'is dst' );
    is(
        $dt->hour, 16,
        'America/New_York is exactly one hour later than America/Chicago - hour'
    );
    is(
        $dt->minute, 0,
        'America/New_York is exactly one hour later than America/Chicago - minute'
    );
    is(
        $dt->second, 0,
        'America/New_York is exactly one hour later than America/Chicago - second'
    );
}

{

    # this is the second of the two 01:59:59 times for that date
    my $dt = DateTime->new(
        year      => 2003, month  => 10, day    => 26,
        hour      => 1,    minute => 59, second => 59,
        time_zone => 'America/Chicago',
    );

    is( $dt->offset, -21600, 'offset should be -21600' );
    is( $dt->is_dst, 0,      'is not dst' );

    $dt->subtract( hours => 1 );

    is( $dt->offset, -18000, 'offset should be -18000' );
    is( $dt->is_dst, 1,      'is not dst' );
    is( $dt->hour,   1, 'crossing DST boundary does not change local hour' );
}

{
    my $dt = DateTime->new(
        year => 2003, month     => 10, day => 26,
        hour => 2,    time_zone => 'America/Chicago',
    );

    is( $dt->offset, -21600, 'offset should be -21600' );
}

{
    my $dt = DateTime->new(
        year => 2003, month     => 10, day => 26,
        hour => 3,    time_zone => 'America/Chicago',
    );

    is( $dt->offset, -21600, 'offset should be -21600' );
}

{
    like(
        exception {
            DateTime->new(
                year => 2003, month     => 4, day => 6,
                hour => 2,    time_zone => 'America/Chicago',
            );
        },
        qr/Invalid local time .+/,
        'exception for invalid time'
    );

    like(
        exception {
            DateTime->new(
                year      => 2003, month  => 4,  day    => 6,
                hour      => 2,    minute => 59, second => 59,
                time_zone => 'America/Chicago',
            );
        },
        qr/Invalid local time .+/,
        'exception for invalid time'
    );
}

{
    is(
        exception {
            DateTime->new(
                year      => 2003, month  => 4,  day    => 6,
                hour      => 1,    minute => 59, second => 59,
                time_zone => 'America/Chicago',
            );
        },
        undef,
        'no exception for valid time'
    );

SKIP:
    {
        skip
            'DateTime 0.29 has a date math bug that causes this test to fail',
            1
            if ( DateTime->VERSION >= 0.29 && DateTime->VERSION < 0.30 );

        my $dt = DateTime->new(
            year      => 2003, month => 4, day => 5,
            hour      => 2,
            time_zone => 'America/Chicago',
        );

        like(
            exception { $dt->add( days => 1 ) },
            qr/Invalid local time .+/,
            'exception for invalid time produced via add'
        );
    }
}

{
    my $dt = DateTime->new(
        year      => 2003, month => 4, day => 5,
        hour      => 2,
        time_zone => 'America/Chicago',
    );
    is(
        exception { $dt->add( hours => 24 ) },
        undef,
        'add 24 hours should work even if add 1 day does not'
    );

    is( $dt->hour, 3, 'hour should no be 3' );
}

{
    my $dt = DateTime->new(
        year => 2003, month     => 4, day => 6,
        hour => 3,    time_zone => 'America/Chicago',
    );

    is( $dt->hour,   3,      'hour should be 3' );
    is( $dt->offset, -18000, 'offset should be -18000' );

    $dt->subtract( seconds => 1 );

    is( $dt->hour,   1,      'hour should be 1' );
    is( $dt->offset, -21600, 'offset should be -21600' );
}

{
    my $dt = DateTime->new(
        year => 2003, month     => 4, day => 6,
        hour => 3,    time_zone => 'floating',
    );
    $dt->set_time_zone('America/Chicago');

    is( $dt->hour, 3, 'hour should be 3 after switching from floating TZ' );
    is(
        $dt->offset, -18000,
        'tz offset should be -18000'
    );
}

{
    my $dt = DateTime->new(
        year => 2003, month     => 4, day => 6,
        hour => 3,    time_zone => 'America/Chicago',
    );
    $dt->set_time_zone('floating');

    is( $dt->hour, 3, 'hour should be 3 after switching to floating TZ' );
    is(
        $dt->local_rd_as_seconds - $dt->utc_rd_as_seconds, 0,
        'tz offset should be 0'
    );
}

{
    like(
        exception {
            DateTime->new(
                year      => 2040, month  => 3,  day    => 11,
                hour      => 2,    minute => 59, second => 59,
                time_zone => 'America/Chicago',
            );
        },
        qr/Invalid local time .+/,
        'exception for invalid time'
    );
}

{
    my $dt = DateTime->new(
        year      => 2001, month  => 10, day => 28,
        hour      => 0,    minute => 59,
        time_zone => 'UTC'
    );

    $dt->set_time_zone('Europe/Vienna');

    is( $dt->hour, 2, 'hour should be 2 in vienna at 00:59:00 UTC' );

    $dt->set_time_zone('UTC')->add( minutes => 1 )
        ->set_time_zone('Europe/Vienna');

    is( $dt->hour, 2, 'hour should be 2 in vienna at 01:00:00 UTC' );
}

{

    # Doing this triggered a recursion bug in earlier versions of
    # DateTime::TimeZone.
    local $ENV{TZ} = 'America/Chicago';

    my $local_tz = DateTime::TimeZone->new( name => 'America/Chicago' );
    my $utc_tz   = DateTime::TimeZone->new( name => 'UTC' );

    my $dt = DateTime->new( year => 2050, time_zone => $local_tz );

    my $sixm = DateTime::Duration->new( months => 6 );
    foreach (
        [ 2050, 7, 1, 1, 'CDT' ],
        [ 2051, 1, 1, 0, 'CST' ],
        [ 2051, 7, 1, 1, 'CDT' ],
        [ 2052, 1, 1, 0, 'CST' ],
        [ 2052, 7, 1, 1, 'CDT' ],
        [ 2053, 1, 1, 0, 'CST' ],
        [ 2053, 7, 1, 1, 'CDT' ],
        [ 2054, 1, 1, 0, 'CST' ],
        [ 2054, 7, 1, 1, 'CDT' ],
        [ 2055, 1, 1, 0, 'CST' ],
        [ 2055, 7, 1, 1, 'CDT' ],
        [ 2056, 1, 1, 0, 'CST' ],
        [ 2056, 7, 1, 1, 'CDT' ],
        [ 2057, 1, 1, 0, 'CST' ],
        [ 2057, 7, 1, 1, 'CDT' ],
        [ 2058, 1, 1, 0, 'CST' ],
        [ 2058, 7, 1, 1, 'CDT' ],
        [ 2059, 1, 1, 0, 'CST' ],
        [ 2059, 7, 1, 1, 'CDT' ],
        [ 2060, 1, 1, 0, 'CST' ],
        [ 2060, 7, 1, 1, 'CDT' ],
    ) {
        $dt->set_time_zone($utc_tz);

        $dt->add_duration($sixm);

        $dt->set_time_zone($local_tz);

        $_->[1] = sprintf( '%02d', $_->[1] );

        my $expect = join q{ }, @{$_};

        is(
            $dt->strftime('%Y %m%e%k %Z'), $expect,
            "datetime is $expect"
        );
    }
}

{
    my $local_tz = DateTime::TimeZone->new( name => 'America/New_York' );
    my $utc_tz   = DateTime::TimeZone->new( name => 'UTC' );

    my $dt = DateTime->new( year => 2060, time_zone => $local_tz );

    my $neg_sixm = DateTime::Duration->new( months => -6 );
    foreach (
        [ 2059, 7, 1, 1, 'EDT' ],
        [ 2059, 1, 1, 0, 'EST' ],
        [ 2058, 7, 1, 1, 'EDT' ],
        [ 2058, 1, 1, 0, 'EST' ],
        [ 2057, 7, 1, 1, 'EDT' ],
        [ 2057, 1, 1, 0, 'EST' ],
        [ 2056, 7, 1, 1, 'EDT' ],
        [ 2056, 1, 1, 0, 'EST' ],
        [ 2055, 7, 1, 1, 'EDT' ],
        [ 2055, 1, 1, 0, 'EST' ],
        [ 2054, 7, 1, 1, 'EDT' ],
        [ 2054, 1, 1, 0, 'EST' ],
        [ 2053, 7, 1, 1, 'EDT' ],
        [ 2053, 1, 1, 0, 'EST' ],
        [ 2052, 7, 1, 1, 'EDT' ],
        [ 2052, 1, 1, 0, 'EST' ],
        [ 2051, 7, 1, 1, 'EDT' ],
        [ 2051, 1, 1, 0, 'EST' ],
        [ 2050, 7, 1, 1, 'EDT' ],
        [ 2050, 1, 1, 0, 'EST' ],
    ) {
        $dt->set_time_zone($utc_tz);

        $dt->add_duration($neg_sixm);

        $dt->set_time_zone($local_tz);

        $_->[1] = sprintf( '%02d', $_->[1] );

        my $expect = join q{ }, @{$_};

        is(
            $dt->strftime('%Y %m%e%k %Z'), $expect,
            "datetime is $expect"
        );
    }
}

done_testing();