Move Archive::Tar from lib/ to ext/
[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],
a272e669 29 [2038, 1, 17, 23, 59, 59], # last full day in any tz
30
31 # more than 2**31 time_t
32 [2258, 8, 11, 1, 49, 17],
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;
8ff19c00 83$tests += 21;
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
4c91ace1 92 # Test timelocal()
93 {
94 my $year_in = $year < 70 ? $year + 1900 : $year;
95 my $time = timelocal($sec,$min,$hour,$mday,$mon,$year_in);
96
97 my($s,$m,$h,$D,$M,$Y) = localtime($time);
98
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 @$_");
a272e669 105 }
e6f8b432 106
4c91ace1 107
108 # Test timegm()
a272e669 109 {
110 my $year_in = $year < 70 ? $year + 1900 : $year;
111 my $time = timegm($sec,$min,$hour,$mday,$mon,$year_in);
e6f8b432 112
a272e669 113 my($s,$m,$h,$D,$M,$Y) = gmtime($time);
e6f8b432 114
a272e669 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 @$_");
1a3850a5 121 }
1a3850a5 122}
123
4c91ace1 124
5847cf89 125for (@bad_time) {
126 my($year, $mon, $mday, $hour, $min, $sec) = @$_;
127 $year -= 1900;
128 $mon--;
129
130 eval { timegm($sec,$min,$hour,$mday,$mon,$year) };
131
e6f8b432 132 like($@, qr/.*out of range.*/, 'invalid time caused an error');
5847cf89 133}
134
e6f8b432 135{
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');
1a3850a5 138
e6f8b432 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');
1a3850a5 141
e6f8b432 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');
145}
1a3850a5 146
13ef5feb 147# bugid #19393
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
151# to do the latter
13ef5feb 152{
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
e6f8b432 156 ok($hour == 2 || $hour == 3, 'hour should be 2 or 3');
823a6996 157}
158
d15eb09c 159for my $p (@years) {
160 my ( $year, $is_leap_year ) = @$p;
161
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" );
165}
166
e6f8b432 167{
99ffb1cb 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');
171
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)');
175
176 eval { timegm(0,0,0,29,1,0) };
177 is($@, '', 'no error with leap day of 2000 (year passed as 0)');
178
5847cf89 179 eval { timegm(0,0,0,29,1,1904) };
e6f8b432 180 is($@, '', 'no error with leap day of 1904');
99ffb1cb 181
182 eval { timegm(0,0,0,29,1,4) };
183 is($@, '', 'no error with leap day of 2004 (year passed as 4)');
184
185 eval { timegm(0,0,0,29,1,96) };
186 is($@, '', 'no error with leap day of 1996 (year passed as 96)');
5847cf89 187}
188
65d4ed58 189SKIP:
190{
191 skip 'These tests require a system with 64-bit time_t.', 3
192 unless $epoch_is_64;
193
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)' );
200}
201
202SKIP:
203{
204 skip 'These tests only run for the package maintainer.', 8
205 unless $ENV{MAINTAINER};
206
e6f8b432 207 require POSIX;
208
209 local $ENV{TZ} = 'Europe/Vienna';
210 POSIX::tzset();
211
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');
217
218 local $ENV{TZ} = 'America/Chicago';
219 POSIX::tzset();
220
221 # Same local time in America/Chicago. There is a transition here
222 # as well.
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');
226
227 $time = timelocal(0, 30, 2, 1, 3, 101);
228 is($time, 986113800,
229 'timelocal for non-existent time gives you the time one hour later');
230
231 local $ENV{TZ} = 'Australia/Sydney';
232 POSIX::tzset();
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);
237 is($time, 985447800,
238 'timelocal prefers earlier epoch in the presence of a DST change');
239
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');
243
244 local $ENV{TZ} = 'Europe/London';
245 POSIX::tzset();
246 $time = timelocal( localtime(1111917720) );
247 is($time, 1111917720,
248 'timelocal for round trip bug on date of DST change for Europe/London');
249
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
252 # docs.
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' );
255
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' );
13ef5feb 258}