6 @INC = ('.', '../lib');
16 # Set up time values to test
19 #year,mon,day,hour,min,sec
20 [1970, 1, 2, 00, 00, 00],
21 [1980, 2, 28, 12, 00, 00],
22 [1980, 2, 29, 12, 00, 00],
23 [1999, 12, 31, 23, 59, 59],
24 [2000, 1, 1, 00, 00, 00],
25 [2010, 10, 12, 14, 13, 12],
27 [2020, 2, 29, 12, 59, 59],
28 [2030, 7, 4, 17, 07, 06],
29 # The following test fails on a surprising number of systems
30 # so it is commented out. The end of the Epoch for a 32-bit signed
31 # implementation of time_t should be Jan 19, 2038 03:14:07 UTC.
32 # [2038, 1, 17, 23, 59, 59], # last full day in any tz
38 [1995, 13, 01, 01, 01, 01],
40 [1995, 02, 30, 01, 01, 01],
42 [1995, 02, 10, 25, 01, 01],
44 [1995, 02, 10, 01, 60, 01],
46 [1995, 02, 10, 01, 01, 60],
51 # test negative epochs for systems that handle it
52 [ 1969, 12, 31, 16, 59, 59 ],
53 [ 1950, 04, 12, 9, 30, 31 ],
66 # Use 3 days before the start of the epoch because with Borland on
67 # Win32 it will work for -3600 _if_ your time zone is +01:00 (or
69 my $neg_epoch_ok = defined ((localtime(-259200))[0]) ? 1 : 0;
71 # use vmsish 'time' makes for oddness around the Unix epoch
74 $neg_epoch_ok = 0; # time_t is unsigned
77 my $epoch_is_64 = eval { $Config{ivsize} == 8 && ( gmtime 2**40 )[5] == 34912 };
79 my $tests = (@time * 12);
80 $tests += @neg_time * 12;
87 for (@time, @neg_time) {
88 my($year, $mon, $mday, $hour, $min, $sec) = @$_;
93 skip '1970 test on VOS fails.', 12
94 if $^O eq 'vos' && $year == 70;
95 skip 'this platform does not support negative epochs.', 12
96 if $year < 70 && ! $neg_epoch_ok;
99 my $year_in = $year < 70 ? $year + 1900 : $year;
100 my $time = timelocal($sec,$min,$hour,$mday,$mon,$year_in);
102 my($s,$m,$h,$D,$M,$Y) = localtime($time);
104 is($s, $sec, "timelocal second for @$_");
105 is($m, $min, "timelocal minute for @$_");
106 is($h, $hour, "timelocal hour for @$_");
107 is($D, $mday, "timelocal day for @$_");
108 is($M, $mon, "timelocal month for @$_");
109 is($Y, $year, "timelocal year for @$_");
113 my $year_in = $year < 70 ? $year + 1900 : $year;
114 my $time = timegm($sec,$min,$hour,$mday,$mon,$year_in);
116 my($s,$m,$h,$D,$M,$Y) = gmtime($time);
118 is($s, $sec, "timegm second for @$_");
119 is($m, $min, "timegm minute for @$_");
120 is($h, $hour, "timegm hour for @$_");
121 is($D, $mday, "timegm day for @$_");
122 is($M, $mon, "timegm month for @$_");
123 is($Y, $year, "timegm year for @$_");
129 my($year, $mon, $mday, $hour, $min, $sec) = @$_;
133 eval { timegm($sec,$min,$hour,$mday,$mon,$year) };
135 like($@, qr/.*out of range.*/, 'invalid time caused an error');
139 is(timelocal(0,0,1,1,0,90) - timelocal(0,0,0,1,0,90), 3600,
140 'one hour difference between two calls to timelocal');
142 is(timelocal(1,2,3,1,0,100) - timelocal(1,2,3,31,11,99), 24 * 3600,
143 'one day difference between two calls to timelocal');
145 # Diff beween Jan 1, 1980 and Mar 1, 1980 = (31 + 29 = 60 days)
146 is(timegm(0,0,0, 1, 2, 80) - timegm(0,0,0, 1, 0, 80), 60 * 24 * 3600,
147 '60 day difference between two calls to timegm');
151 # At a DST transition, the clock skips forward, eg from 01:59:59 to
152 # 03:00:00. In this case, 02:00:00 is an invalid time, and should be
153 # treated like 03:00:00 rather than 01:00:00 - negative zone offsets used
156 my $hour = (localtime(timelocal(0, 0, 2, 7, 3, 102)))[2];
157 # testers in US/Pacific should get 3,
158 # other testers should get 2
159 ok($hour == 2 || $hour == 3, 'hour should be 2 or 3');
163 my ( $year, $is_leap_year ) = @$p;
165 my $string = $is_leap_year ? 'is' : 'is not';
166 is( Time::Local::_is_leap_year($year), $is_leap_year,
167 "$year $string a leap year" );
172 skip 'this platform does not support negative epochs.', 6
173 unless $neg_epoch_ok;
175 eval { timegm(0,0,0,29,1,1900) };
176 like($@, qr/Day '29' out of range 1\.\.28/,
177 'does not accept leap day in 1900');
179 eval { timegm(0,0,0,29,1,200) };
180 like($@, qr/Day '29' out of range 1\.\.28/,
181 'does not accept leap day in 2100 (year passed as 200)');
183 eval { timegm(0,0,0,29,1,0) };
184 is($@, '', 'no error with leap day of 2000 (year passed as 0)');
186 eval { timegm(0,0,0,29,1,1904) };
187 is($@, '', 'no error with leap day of 1904');
189 eval { timegm(0,0,0,29,1,4) };
190 is($@, '', 'no error with leap day of 2004 (year passed as 4)');
192 eval { timegm(0,0,0,29,1,96) };
193 is($@, '', 'no error with leap day of 1996 (year passed as 96)');
198 skip 'These tests require a system with 64-bit time_t.', 3
201 is( timegm( 8, 14, 3, 19, 0, ( 1900 + 138 ) ), 2**31,
202 'can call timegm for 2**31 epoch seconds' );
203 is( timegm( 16, 28, 6, 7, 1, ( 1900 + 206 ) ), 2**32,
204 'can call timegm for 2**32 epoch seconds (on a 64-bit system)' );
205 is( timegm( 16, 36, 0, 20, 1, ( 34912 + 1900 ) ), 2**40,
206 'can call timegm for 2**40 epoch seconds (on a 64-bit system)' );
211 skip 'These tests only run for the package maintainer.', 8
212 unless $ENV{MAINTAINER};
216 local $ENV{TZ} = 'Europe/Vienna';
219 # 2001-10-28 02:30:00 - could be either summer or standard time,
220 # prefer earlier of the two, in this case summer
221 my $time = timelocal(0, 30, 2, 28, 9, 101);
222 is($time, 1004229000,
223 'timelocal prefers earlier epoch in the presence of a DST change');
225 local $ENV{TZ} = 'America/Chicago';
228 # Same local time in America/Chicago. There is a transition here
230 $time = timelocal(0, 30, 1, 28, 9, 101);
231 is($time, 1004250600,
232 'timelocal prefers earlier epoch in the presence of a DST change');
234 $time = timelocal(0, 30, 2, 1, 3, 101);
236 'timelocal for non-existent time gives you the time one hour later');
238 local $ENV{TZ} = 'Australia/Sydney';
240 # 2001-03-25 02:30:00 in Australia/Sydney. This is the transition
241 # _to_ summer time. The southern hemisphere transitions are
242 # opposite those of the northern.
243 $time = timelocal(0, 30, 2, 25, 2, 101);
245 'timelocal prefers earlier epoch in the presence of a DST change');
247 $time = timelocal(0, 30, 2, 28, 9, 101);
248 is($time, 1004200200,
249 'timelocal for non-existent time gives you the time one hour later');
251 local $ENV{TZ} = 'Europe/London';
253 $time = timelocal( localtime(1111917720) );
254 is($time, 1111917720,
255 'timelocal for round trip bug on date of DST change for Europe/London');
257 # There is no 1:00 AM on this date, as it leaps forward to
258 # 2:00 on the DST change - this should return 2:00 per the
260 is( ( localtime( timelocal( 0, 0, 1, 27, 2, 2005 ) ) )[2], 2,
261 'hour is 2 when given 1:00 AM on Europe/London date change' );
263 is( ( localtime( timelocal( 0, 0, 2, 27, 2, 2005 ) ) )[2], 2,
264 'hour is 2 when given 2:00 AM on Europe/London date change' );
269 skip 'These tests are only run when $ENV{PERL_CORE} is true.', 2
270 unless $ENV{PERL_CORE};
274 require 'timelocal.pl';
276 # need to get ok() from main package
277 ::is(timegm(0,0,0,1,0,80), main::timegm(0,0,0,1,0,80),
278 'timegm in timelocal.pl');
280 ::is(timelocal(1,2,3,4,5,88), main::timelocal(1,2,3,4,5,88),
281 'timelocal in timelocal.pl');