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 [2038, 1, 17, 23, 59, 59], # last full day in any tz
31 # more than 2**31 time_t
32 [2258, 8, 11, 1, 49, 17],
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) = @$_;
94 my $year_in = $year < 70 ? $year + 1900 : $year;
95 my $time = timelocal($sec,$min,$hour,$mday,$mon,$year_in);
97 my($s,$m,$h,$D,$M,$Y) = localtime($time);
99 is($s, $sec, "timelocal second for @$_");
100 is($m, $min, "timelocal minute for @$_");
101 is($h, $hour, "timelocal hour for @$_");
102 is($D, $mday, "timelocal day for @$_");
103 is($M, $mon, "timelocal month for @$_");
104 is($Y, $year, "timelocal year for @$_");
110 my $year_in = $year < 70 ? $year + 1900 : $year;
111 my $time = timegm($sec,$min,$hour,$mday,$mon,$year_in);
113 my($s,$m,$h,$D,$M,$Y) = gmtime($time);
115 is($s, $sec, "timegm second for @$_");
116 is($m, $min, "timegm minute for @$_");
117 is($h, $hour, "timegm hour for @$_");
118 is($D, $mday, "timegm day for @$_");
119 is($M, $mon, "timegm month for @$_");
120 is($Y, $year, "timegm year for @$_");
126 my($year, $mon, $mday, $hour, $min, $sec) = @$_;
130 eval { timegm($sec,$min,$hour,$mday,$mon,$year) };
132 like($@, qr/.*out of range.*/, 'invalid time caused an error');
136 is(timelocal(0,0,1,1,0,90) - timelocal(0,0,0,1,0,90), 3600,
137 'one hour difference between two calls to timelocal');
139 is(timelocal(1,2,3,1,0,100) - timelocal(1,2,3,31,11,99), 24 * 3600,
140 'one day difference between two calls to timelocal');
142 # Diff beween Jan 1, 1980 and Mar 1, 1980 = (31 + 29 = 60 days)
143 is(timegm(0,0,0, 1, 2, 80) - timegm(0,0,0, 1, 0, 80), 60 * 24 * 3600,
144 '60 day difference between two calls to timegm');
148 # At a DST transition, the clock skips forward, eg from 01:59:59 to
149 # 03:00:00. In this case, 02:00:00 is an invalid time, and should be
150 # treated like 03:00:00 rather than 01:00:00 - negative zone offsets used
153 my $hour = (localtime(timelocal(0, 0, 2, 7, 3, 102)))[2];
154 # testers in US/Pacific should get 3,
155 # other testers should get 2
156 ok($hour == 2 || $hour == 3, 'hour should be 2 or 3');
160 my ( $year, $is_leap_year ) = @$p;
162 my $string = $is_leap_year ? 'is' : 'is not';
163 is( Time::Local::_is_leap_year($year), $is_leap_year,
164 "$year $string a leap year" );
168 eval { timegm(0,0,0,29,1,1900) };
169 like($@, qr/Day '29' out of range 1\.\.28/,
170 'does not accept leap day in 1900');
172 eval { timegm(0,0,0,29,1,200) };
173 like($@, qr/Day '29' out of range 1\.\.28/,
174 'does not accept leap day in 2100 (year passed as 200)');
176 eval { timegm(0,0,0,29,1,0) };
177 is($@, '', 'no error with leap day of 2000 (year passed as 0)');
179 eval { timegm(0,0,0,29,1,1904) };
180 is($@, '', 'no error with leap day of 1904');
182 eval { timegm(0,0,0,29,1,4) };
183 is($@, '', 'no error with leap day of 2004 (year passed as 4)');
185 eval { timegm(0,0,0,29,1,96) };
186 is($@, '', 'no error with leap day of 1996 (year passed as 96)');
191 skip 'These tests require a system with 64-bit time_t.', 3
194 is( timegm( 8, 14, 3, 19, 0, ( 1900 + 138 ) ), 2**31,
195 'can call timegm for 2**31 epoch seconds' );
196 is( timegm( 16, 28, 6, 7, 1, ( 1900 + 206 ) ), 2**32,
197 'can call timegm for 2**32 epoch seconds (on a 64-bit system)' );
198 is( timegm( 16, 36, 0, 20, 1, ( 34912 + 1900 ) ), 2**40,
199 'can call timegm for 2**40 epoch seconds (on a 64-bit system)' );
204 skip 'These tests only run for the package maintainer.', 8
205 unless $ENV{MAINTAINER};
209 local $ENV{TZ} = 'Europe/Vienna';
212 # 2001-10-28 02:30:00 - could be either summer or standard time,
213 # prefer earlier of the two, in this case summer
214 my $time = timelocal(0, 30, 2, 28, 9, 101);
215 is($time, 1004229000,
216 'timelocal prefers earlier epoch in the presence of a DST change');
218 local $ENV{TZ} = 'America/Chicago';
221 # Same local time in America/Chicago. There is a transition here
223 $time = timelocal(0, 30, 1, 28, 9, 101);
224 is($time, 1004250600,
225 'timelocal prefers earlier epoch in the presence of a DST change');
227 $time = timelocal(0, 30, 2, 1, 3, 101);
229 'timelocal for non-existent time gives you the time one hour later');
231 local $ENV{TZ} = 'Australia/Sydney';
233 # 2001-03-25 02:30:00 in Australia/Sydney. This is the transition
234 # _to_ summer time. The southern hemisphere transitions are
235 # opposite those of the northern.
236 $time = timelocal(0, 30, 2, 25, 2, 101);
238 'timelocal prefers earlier epoch in the presence of a DST change');
240 $time = timelocal(0, 30, 2, 28, 9, 101);
241 is($time, 1004200200,
242 'timelocal for non-existent time gives you the time one hour later');
244 local $ENV{TZ} = 'Europe/London';
246 $time = timelocal( localtime(1111917720) );
247 is($time, 1111917720,
248 'timelocal for round trip bug on date of DST change for Europe/London');
250 # There is no 1:00 AM on this date, as it leaps forward to
251 # 2:00 on the DST change - this should return 2:00 per the
253 is( ( localtime( timelocal( 0, 0, 1, 27, 2, 2005 ) ) )[2], 2,
254 'hour is 2 when given 1:00 AM on Europe/London date change' );
256 is( ( localtime( timelocal( 0, 0, 2, 27, 2, 2005 ) ) )[2], 2,
257 'hour is 2 when given 2:00 AM on Europe/London date change' );