File: 02basic.t

package info (click to toggle)
libdatetime-timezone-perl 1%3A1.75-2%2B2018e
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-updates
  • size: 191,260 kB
  • sloc: perl: 2,767; sh: 42; makefile: 14
file content (199 lines) | stat: -rw-r--r-- 5,641 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
use strict;
use warnings;

use File::Spec;
use Test::More;

use lib File::Spec->catdir( File::Spec->curdir, 't' );

BEGIN { require 'check_datetime_version.pl' }

use DateTime::TimeZone;

my @names = DateTime::TimeZone::all_names();

my $is_maintainer = -d '.hg' || $ENV{RELEASE_TESTING} ? 1 : 0;

foreach my $name (@names) {
    my $tz = DateTime::TimeZone->new( name => $name );
    isa_ok( $tz, 'DateTime::TimeZone' );

    is( $tz->name, $name, 'check ->name' );

    is( $tz->is_floating, 0, 'should not be floating' );
    is( $tz->is_utc,      0, 'should not be UTC' )
        unless $name eq 'UTC';

    # adding these tests makes the test suite take a _long_ time to
    # finish, and it uses up lots of memory too.
    if ($is_maintainer) {
        my $dt;
        eval { $dt = DateTime->now( time_zone => $name ) };
        is( $@, '', "Can call DateTime->now with $name" );
        eval { $dt->add( years => 50 ) };
        is( $@, '', "Can add 50 years with $name" );
        eval { $dt->subtract( years => 400 ) };
        is( $@, '', "Can subtract 400 years with $name" );
        eval {
            $dt = DateTime->new( year => 2000, month => 6, hour => 1,
                time_zone => $name );
        };
        is( $dt->hour, 1, 'make sure that local time is always respected' );
        eval {
            $dt = DateTime->new( year => 2000, month => 12, hour => 1,
                time_zone => $name );
        };
        is( $dt->hour, 1, 'make sure that local time is always respected' );
    }
}

foreach my $name ( '0', 'Z', 'UTC' ) {
    my $tz = DateTime::TimeZone->new( name => $name );
    isa_ok( $tz, 'DateTime::TimeZone' );

    is( $tz->name, 'UTC', 'name should be UTC' );

    is( $tz->is_floating, 0, 'should not be floating' );
    is( $tz->is_utc,      1, 'should be UTC' );
}

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

# These tests are odd since we're feeding UTC times into the time zone
# object, which isn't what happens in real usage.  But doing this
# minimizes how much of DateTime.pm needs to work for these tests.
{
    my $dt = DateTime->new(
        year      => 2001,
        month     => 9,
        day       => 10,
        time_zone => 'UTC',
    );
    is( $tz->offset_for_datetime($dt), -18000, 'offset should be -18000' );
    is( $tz->short_name_for_datetime($dt), 'CDT', 'name should be CDT' );
}

{
    my $dt = DateTime->new(
        year      => 2001,
        month     => 10,
        day       => 29,
        time_zone => 'UTC',
    );
    is( $tz->offset_for_datetime($dt), -21600, 'offset should be -21600' );
    is( $tz->short_name_for_datetime($dt), 'CST', 'name should be CST' );
}

{

    # check that generation works properly
    my $dt = DateTime->new(
        year      => 2200,
        month     => 11,
        day       => 2,
        time_zone => 'UTC',
    );
    is( $tz->offset_for_datetime($dt), -18000,
        'generated offset should be -1800' );
    is( $tz->short_name_for_datetime($dt), 'CDT',
        'generated name should be CDT' );
}

{

    # check that generation works properly
    my $dt = DateTime->new(
        year      => 2200,
        month     => 11,
        day       => 3,
        time_zone => 'UTC',
    );
    is( $tz->offset_for_datetime($dt), -21600,
        'generated offset should be -21600' );
    is( $tz->short_name_for_datetime($dt), 'CST',
        'generated name should be CST' );
}

{

    # bug when creating new datetime for year just after time zone's
    # max year
    my $tz = DateTime::TimeZone->new( name => 'America/Los_Angeles' );

    my $dt = eval {
        DateTime->new(
            year      => $tz->{max_year} + 1,
            month     => 5,
            day       => 20,
            time_zone => $tz
        );
    };
    ok( $dt, 'was able to create datetime object' );
}

{
    my $dt = DateTime->new(
        year      => 1944,
        month     => 10,
        day       => 29,
        time_zone => 'UTC',
    );
    is( $tz->offset_for_datetime($dt), -18000, 'offset should be -18000' );
    is( $tz->short_name_for_datetime($dt), 'CWT', 'name should be CWT' );
}

{
    my $dt = DateTime->new(
        year      => 1936,
        month     => 3,
        day       => 2,
        time_zone => 'UTC',
    );

    is( $tz->offset_for_datetime($dt), -18000, 'offset should be -18000' );
    is( $tz->short_name_for_datetime($dt), 'EST', 'name should be EST' );
}

{
    my $dt = DateTime->new(
        year      => 1883,
        month     => 1,
        day       => 29,
        time_zone => 'UTC',
    );

    is( $tz->offset_for_datetime($dt), -21036, 'offset should be -21036' );
    is( $tz->short_name_for_datetime($dt), 'LMT', 'name should be LMT' );
}

{
    {

        package TestHack;

        sub new { bless {} }

        # UTC RD secs == 63518486401
        sub utc_rd_values { ( 735167, 57601 ) }
    }

    # This is to check a bug in DT::TZ::_span_for_datetime, where it
    # was always looking at the LOCAL_END of the current max_span.
    #
    # Australia/Sydney's max_span (before generation) has a LOCAL_END
    # of 63518522400 and UTC_END of 63518486400.  The values above
    # create a utc_rd_seconds value that is after the UTC_END but
    # before the LOCAL_END.
    my $dt = DateTime->from_object( object => TestHack->new );

    eval { $dt->set_time_zone('UTC')->set_time_zone('Australia/Sydney') };
    ok( !$@,         'should be able to set time zone' );
    ok( $dt->is_dst, 'is_dst should be true' );
}

{
    my $tz = DateTime::TimeZone->new( name => '-0100' );
    ok( !$tz->is_olson, 'is_olson is false for offset only time zone' );
}

done_testing();