File: 02basic.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 (245 lines) | stat: -rw-r--r-- 6,199 bytes parent folder | download | duplicates (4)
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
use strict;
use warnings;

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

use Test::More;
use Test::Fatal;

use DateTime::TimeZone;
use Try::Tiny;

my @names = DateTime::TimeZone::all_names();
cmp_ok(
    scalar @names, '>', 100,
    'at least 100 zones'
);

my %links = DateTime::TimeZone->links();
cmp_ok(
    scalar keys %links, '>', 100,
    'at least 100 links'
);

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

foreach my $name (@names) {
    my $resolved_name = $links{$name} || $name;

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

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

    is( $tz->is_floating, 0, 'should not be floating' );
    is( $tz->is_utc,      0, 'should not be UTC' )
        unless $resolved_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;
        is(
            exception { $dt = DateTime->now( time_zone => $name ) },
            undef,
            "Can call DateTime->now with $name"
        );

        is(
            exception { $dt->add( years => 50 ) },
            undef,
            "Can add 50 years with $name"
        );

        is(
            exception { $dt->subtract( years => 400 ) },
            undef,
            "Can subtract 400 years with $name"
        );

        try {
            $dt = DateTime->new(
                year      => 2000, month => 6, hour => 1,
                time_zone => $name
            );
        };
        is( $dt->hour, 1, 'make sure that local time is always respected' );

        try {
            $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'
    );
}

{
    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 {}, shift }

        # 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 );

    is(
        exception {
            $dt->set_time_zone('UTC')->set_time_zone('Australia/Sydney')
        },
        undef,
        'should be able to set time zone without error'
    );

    ok( $dt->is_dst, 'is_dst should be true' );
}

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

{

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

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

done_testing();