6 @INC = ('.', '../lib');
15 # Set up time values to test
18 #year,mon,day,hour,min,sec
19 [1970, 1, 2, 00, 00, 00],
20 [1980, 2, 28, 12, 00, 00],
21 [1980, 2, 29, 12, 00, 00],
22 [1999, 12, 31, 23, 59, 59],
23 [2000, 1, 1, 00, 00, 00],
24 [2010, 10, 12, 14, 13, 12],
26 [2020, 2, 29, 12, 59, 59],
27 [2030, 7, 4, 17, 07, 06],
28 [2038, 1, 17, 23, 59, 59], # last full day in any tz
30 # more than 2**31 time_t
31 [2258, 8, 11, 1, 49, 17],
37 [1995, 13, 01, 01, 01, 01],
39 [1995, 02, 30, 01, 01, 01],
41 [1995, 02, 10, 25, 01, 01],
43 [1995, 02, 10, 01, 60, 01],
45 [1995, 02, 10, 01, 01, 60],
50 # test negative epochs for systems that handle it
51 [ 1969, 12, 31, 16, 59, 59 ],
52 [ 1950, 04, 12, 9, 30, 31 ],
65 my $tests = (@time * 12);
66 $tests += @neg_time * 12;
70 $tests += 2 if $ENV{PERL_CORE};
71 $tests += 8 if $ENV{MAINTAINER};
75 for (@time, @neg_time) {
76 my($year, $mon, $mday, $hour, $min, $sec) = @$_;
82 my $year_in = $year < 70 ? $year + 1900 : $year;
83 my $time = timelocal($sec,$min,$hour,$mday,$mon,$year_in);
85 my($s,$m,$h,$D,$M,$Y) = localtime($time);
87 is($s, $sec, "timelocal second for @$_");
88 is($m, $min, "timelocal minute for @$_");
89 is($h, $hour, "timelocal hour for @$_");
90 is($D, $mday, "timelocal day for @$_");
91 is($M, $mon, "timelocal month for @$_");
92 is($Y, $year, "timelocal year for @$_");
98 my $year_in = $year < 70 ? $year + 1900 : $year;
99 my $time = timegm($sec,$min,$hour,$mday,$mon,$year_in);
101 my($s,$m,$h,$D,$M,$Y) = gmtime($time);
103 is($s, $sec, "timegm second for @$_");
104 is($m, $min, "timegm minute for @$_");
105 is($h, $hour, "timegm hour for @$_");
106 is($D, $mday, "timegm day for @$_");
107 is($M, $mon, "timegm month for @$_");
108 is($Y, $year, "timegm year for @$_");
114 my($year, $mon, $mday, $hour, $min, $sec) = @$_;
118 eval { timegm($sec,$min,$hour,$mday,$mon,$year) };
120 like($@, qr/.*out of range.*/, 'invalid time caused an error');
124 is(timelocal(0,0,1,1,0,90) - timelocal(0,0,0,1,0,90), 3600,
125 'one hour difference between two calls to timelocal');
127 is(timelocal(1,2,3,1,0,100) - timelocal(1,2,3,31,11,99), 24 * 3600,
128 'one day difference between two calls to timelocal');
130 # Diff beween Jan 1, 1980 and Mar 1, 1980 = (31 + 29 = 60 days)
131 is(timegm(0,0,0, 1, 2, 80) - timegm(0,0,0, 1, 0, 80), 60 * 24 * 3600,
132 '60 day difference between two calls to timegm');
136 # At a DST transition, the clock skips forward, eg from 01:59:59 to
137 # 03:00:00. In this case, 02:00:00 is an invalid time, and should be
138 # treated like 03:00:00 rather than 01:00:00 - negative zone offsets used
141 my $hour = (localtime(timelocal(0, 0, 2, 7, 3, 102)))[2];
142 # testers in US/Pacific should get 3,
143 # other testers should get 2
144 ok($hour == 2 || $hour == 3, 'hour should be 2 or 3');
148 my ( $year, $is_leap_year ) = @$p;
150 my $string = $is_leap_year ? 'is' : 'is not';
151 is( Time::Local::_is_leap_year($year), $is_leap_year,
152 "$year $string a leap year" );
156 eval { timegm(0,0,0,29,1,1900) };
157 like($@, qr/Day '29' out of range 1\.\.28/,
158 'does not accept leap day in 1900');
160 eval { timegm(0,0,0,29,1,200) };
161 like($@, qr/Day '29' out of range 1\.\.28/,
162 'does not accept leap day in 2100 (year passed as 200)');
164 eval { timegm(0,0,0,29,1,0) };
165 is($@, '', 'no error with leap day of 2000 (year passed as 0)');
167 eval { timegm(0,0,0,29,1,1904) };
168 is($@, '', 'no error with leap day of 1904');
170 eval { timegm(0,0,0,29,1,4) };
171 is($@, '', 'no error with leap day of 2004 (year passed as 4)');
173 eval { timegm(0,0,0,29,1,96) };
174 is($@, '', 'no error with leap day of 1996 (year passed as 96)');
177 if ($ENV{MAINTAINER}) {
180 local $ENV{TZ} = 'Europe/Vienna';
183 # 2001-10-28 02:30:00 - could be either summer or standard time,
184 # prefer earlier of the two, in this case summer
185 my $time = timelocal(0, 30, 2, 28, 9, 101);
186 is($time, 1004229000,
187 'timelocal prefers earlier epoch in the presence of a DST change');
189 local $ENV{TZ} = 'America/Chicago';
192 # Same local time in America/Chicago. There is a transition here
194 $time = timelocal(0, 30, 1, 28, 9, 101);
195 is($time, 1004250600,
196 'timelocal prefers earlier epoch in the presence of a DST change');
198 $time = timelocal(0, 30, 2, 1, 3, 101);
200 'timelocal for non-existent time gives you the time one hour later');
202 local $ENV{TZ} = 'Australia/Sydney';
204 # 2001-03-25 02:30:00 in Australia/Sydney. This is the transition
205 # _to_ summer time. The southern hemisphere transitions are
206 # opposite those of the northern.
207 $time = timelocal(0, 30, 2, 25, 2, 101);
209 'timelocal prefers earlier epoch in the presence of a DST change');
211 $time = timelocal(0, 30, 2, 28, 9, 101);
212 is($time, 1004200200,
213 'timelocal for non-existent time gives you the time one hour later');
215 local $ENV{TZ} = 'Europe/London';
217 $time = timelocal( localtime(1111917720) );
218 is($time, 1111917720,
219 'timelocal for round trip bug on date of DST change for Europe/London');
221 # There is no 1:00 AM on this date, as it leaps forward to
222 # 2:00 on the DST change - this should return 2:00 per the
224 is( ( localtime( timelocal( 0, 0, 1, 27, 2, 2005 ) ) )[2], 2,
225 'hour is 2 when given 1:00 AM on Europe/London date change' );
227 is( ( localtime( timelocal( 0, 0, 2, 27, 2, 2005 ) ) )[2], 2,
228 'hour is 2 when given 2:00 AM on Europe/London date change' );
231 if ($ENV{PERL_CORE}) {
233 require 'timelocal.pl';
235 # need to get ok() from main package
236 ::is(timegm(0,0,0,1,0,80), main::timegm(0,0,0,1,0,80),
237 'timegm in timelocal.pl');
239 ::is(timelocal(1,2,3,4,5,88), main::timelocal(1,2,3,4,5,88),
240 'timelocal in timelocal.pl');