Upgrade to Time::Local 1.1901.
[p5sagit/p5-mst-13.2.git] / lib / Time / Local.t
CommitLineData
1a3850a5 1#!./perl
2
3BEGIN {
1c41b6a4 4 if ($ENV{PERL_CORE}){
5 chdir('t') if -d 't';
6 @INC = ('.', '../lib');
7 }
1a3850a5 8}
9
823a6996 10use strict;
11
65d4ed58 12use Config;
e6f8b432 13use Test::More;
1a3850a5 14use Time::Local;
15
16# Set up time values to test
823a6996 17my @time =
1a3850a5 18 (
823a6996 19 #year,mon,day,hour,min,sec
9b599b2a 20 [1970, 1, 2, 00, 00, 00],
1a3850a5 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],
5847cf89 26 # leap day
78063c05 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
1a3850a5 33 );
34
5847cf89 35my @bad_time =
36 (
37 # month too large
38 [1995, 13, 01, 01, 01, 01],
39 # day too large
40 [1995, 02, 30, 01, 01, 01],
41 # hour too large
42 [1995, 02, 10, 25, 01, 01],
43 # minute too large
44 [1995, 02, 10, 01, 60, 01],
45 # second too large
46 [1995, 02, 10, 01, 01, 60],
47 );
48
49my @neg_time =
50 (
51 # test negative epochs for systems that handle it
52 [ 1969, 12, 31, 16, 59, 59 ],
53 [ 1950, 04, 12, 9, 30, 31 ],
54 );
55
d15eb09c 56# Leap year tests
57my @years =
58 (
59 [ 1900 => 0 ],
60 [ 1947 => 0 ],
61 [ 1996 => 1 ],
62 [ 2000 => 1 ],
63 [ 2100 => 0 ],
64 );
65
93a04732 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
68# greater).
d15eb09c 69my $neg_epoch_ok = defined ((localtime(-259200))[0]) ? 1 : 0;
5847cf89 70
61bb5906 71# use vmsish 'time' makes for oddness around the Unix epoch
8f230aaa 72if ($^O eq 'VMS') {
5c415a7a 73 $time[0][2]++;
74 $neg_epoch_ok = 0; # time_t is unsigned
75}
61bb5906 76
65d4ed58 77my $epoch_is_64 = eval { $Config{ivsize} == 8 && ( gmtime 2**40 )[5] == 34912 };
78
5847cf89 79my $tests = (@time * 12);
80$tests += @neg_time * 12;
81$tests += @bad_time;
d15eb09c 82$tests += @years;
65d4ed58 83$tests += 23;
1c41b6a4 84
823a6996 85plan tests => $tests;
1a3850a5 86
5847cf89 87for (@time, @neg_time) {
1a3850a5 88 my($year, $mon, $mday, $hour, $min, $sec) = @$_;
89 $year -= 1900;
823a6996 90 $mon--;
91
e6f8b432 92 SKIP: {
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;
97
98 {
99 my $year_in = $year < 70 ? $year + 1900 : $year;
100 my $time = timelocal($sec,$min,$hour,$mday,$mon,$year_in);
101
102 my($s,$m,$h,$D,$M,$Y) = localtime($time);
103
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 @$_");
110 }
111
112 {
113 my $year_in = $year < 70 ? $year + 1900 : $year;
114 my $time = timegm($sec,$min,$hour,$mday,$mon,$year_in);
115
116 my($s,$m,$h,$D,$M,$Y) = gmtime($time);
117
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 @$_");
124 }
1a3850a5 125 }
1a3850a5 126}
127
5847cf89 128for (@bad_time) {
129 my($year, $mon, $mday, $hour, $min, $sec) = @$_;
130 $year -= 1900;
131 $mon--;
132
133 eval { timegm($sec,$min,$hour,$mday,$mon,$year) };
134
e6f8b432 135 like($@, qr/.*out of range.*/, 'invalid time caused an error');
5847cf89 136}
137
e6f8b432 138{
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');
1a3850a5 141
e6f8b432 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');
1a3850a5 144
e6f8b432 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');
148}
1a3850a5 149
13ef5feb 150# bugid #19393
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
154# to do the latter
13ef5feb 155{
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
e6f8b432 159 ok($hour == 2 || $hour == 3, 'hour should be 2 or 3');
823a6996 160}
161
d15eb09c 162for my $p (@years) {
163 my ( $year, $is_leap_year ) = @$p;
164
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" );
168}
169
e6f8b432 170SKIP:
171{
c5310178 172 skip 'this platform does not support negative epochs.', 6
e6f8b432 173 unless $neg_epoch_ok;
174
99ffb1cb 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');
178
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)');
182
183 eval { timegm(0,0,0,29,1,0) };
184 is($@, '', 'no error with leap day of 2000 (year passed as 0)');
185
5847cf89 186 eval { timegm(0,0,0,29,1,1904) };
e6f8b432 187 is($@, '', 'no error with leap day of 1904');
99ffb1cb 188
189 eval { timegm(0,0,0,29,1,4) };
190 is($@, '', 'no error with leap day of 2004 (year passed as 4)');
191
192 eval { timegm(0,0,0,29,1,96) };
193 is($@, '', 'no error with leap day of 1996 (year passed as 96)');
5847cf89 194}
195
65d4ed58 196SKIP:
197{
198 skip 'These tests require a system with 64-bit time_t.', 3
199 unless $epoch_is_64;
200
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)' );
207}
208
209SKIP:
210{
211 skip 'These tests only run for the package maintainer.', 8
212 unless $ENV{MAINTAINER};
213
e6f8b432 214 require POSIX;
215
216 local $ENV{TZ} = 'Europe/Vienna';
217 POSIX::tzset();
218
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');
224
225 local $ENV{TZ} = 'America/Chicago';
226 POSIX::tzset();
227
228 # Same local time in America/Chicago. There is a transition here
229 # as well.
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');
233
234 $time = timelocal(0, 30, 2, 1, 3, 101);
235 is($time, 986113800,
236 'timelocal for non-existent time gives you the time one hour later');
237
238 local $ENV{TZ} = 'Australia/Sydney';
239 POSIX::tzset();
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);
244 is($time, 985447800,
245 'timelocal prefers earlier epoch in the presence of a DST change');
246
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');
250
251 local $ENV{TZ} = 'Europe/London';
252 POSIX::tzset();
253 $time = timelocal( localtime(1111917720) );
254 is($time, 1111917720,
255 'timelocal for round trip bug on date of DST change for Europe/London');
256
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
259 # docs.
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' );
262
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' );
13ef5feb 265}
266
65d4ed58 267SKIP:
268{
269 skip 'These tests are only run when $ENV{PERL_CORE} is true.', 2
270 unless $ENV{PERL_CORE};
271
272 {
273 package test;
274 require 'timelocal.pl';
4ab0373f 275
65d4ed58 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');
1a3850a5 279
65d4ed58 280 ::is(timelocal(1,2,3,4,5,88), main::timelocal(1,2,3,4,5,88),
281 'timelocal in timelocal.pl');
282 }
1c41b6a4 283}