File: 47default-time-zone.t

package info (click to toggle)
libdatetime-perl 2%3A1.50-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,504 kB
  • sloc: perl: 2,964; makefile: 3
file content (90 lines) | stat: -rw-r--r-- 2,833 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
use strict;
use warnings;

use Test::More;

use DateTime;

{
    my $dt = DateTime->new( year => 2000, month => 2, day => 21 );
    is(
        $dt->time_zone->name, 'floating',
        'Time zones for new DateTime objects should default to floating'
    );
    is(
        DateTime->last_day_of_month( year => 2000, month => 2 )
            ->time_zone->name,
        'floating',
        'last_day_of_month time zone also should default to floating'
    );
    is(
        DateTime->from_day_of_year( year => 2000, day_of_year => 212 )
            ->time_zone->name,
        'floating',
        'from_day_of_year time zone also should default to floating'
    );
    is(
        DateTime->now->time_zone->name, 'UTC',
        '... except for constructors which assume UTC'
    );
    is(
        DateTime->from_epoch( epoch => time() )->time_zone->name, 'UTC',
        '... except for constructors which assume UTC'
    );
}

{
    my $dt1 = DateTime->new( year => 1970, hour => 1, nanosecond => 100 );
    my $dt2 = DateTime->from_object( object => $dt1 );
    is(
        $dt2->time_zone->name, 'floating',
        'Copying DateTime objects from other DateTime objects should retain the timezone'
    );
}

{
    my $dt = DateTime->new( year => 2000, month => 2, day => 21 );
    local $ENV{PERL_DATETIME_DEFAULT_TZ} = 'America/Los_Angeles';
    is(
        $dt->time_zone->name, 'floating',
        'Setting PERL_DATETIME_DEFAULT_TZ env should not impact existing objects'
    );
    $dt = DateTime->new( year => 2000, month => 2, day => 21 );
    is(
        $dt->time_zone->name, $ENV{PERL_DATETIME_DEFAULT_TZ},
        '... but new objects should no longer default to the floating time zone'
    );
    is(
        DateTime->last_day_of_month( year => 2000, month => 2 )
            ->time_zone->name,
        $ENV{PERL_DATETIME_DEFAULT_TZ},
        'last_day_of_month time zone also should default to floating'
    );
    is(
        DateTime->from_day_of_year( year => 2000, day_of_year => 212 )
            ->time_zone->name,
        $ENV{PERL_DATETIME_DEFAULT_TZ},
        'from_day_of_year time zone also should default to floating'
    );
    is(
        DateTime->now->time_zone->name, 'UTC',
        '... and constructors which assume UTC should remain unchanged'
    );

    my $dt1 = DateTime->new( year => 1970, hour => 1, nanosecond => 100 );
    my $dt2 = DateTime->from_object( object => $dt1 );
    is(
        $dt2->time_zone->name, $ENV{PERL_DATETIME_DEFAULT_TZ},
        'Copying DateTime objects from other DateTime objects should retain the timezone'
    );
}

{
    my $dt = DateTime->new( year => 2000, month => 2, day => 21 );
    is(
        $dt->time_zone->name, 'floating',
        'Default time zone should revert to "floating" when PERL_DATETIME_DEFAULT_TZ no longer set'
    );
}

done_testing();