Update some docs to explain that Perl no longer has a 2038 bug.
[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
e6f8b432 12use Test::More;
1a3850a5 13use Time::Local;
14
15# Set up time values to test
823a6996 16my @time =
1a3850a5 17 (
823a6996 18 #year,mon,day,hour,min,sec
9b599b2a 19 [1970, 1, 2, 00, 00, 00],
1a3850a5 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],
5847cf89 25 # leap day
78063c05 26 [2020, 2, 29, 12, 59, 59],
27 [2030, 7, 4, 17, 07, 06],
a272e669 28 [2038, 1, 17, 23, 59, 59], # last full day in any tz
29
30 # more than 2**31 time_t
31 [2258, 8, 11, 1, 49, 17],
1a3850a5 32 );
33
5847cf89 34my @bad_time =
35 (
36 # month too large
37 [1995, 13, 01, 01, 01, 01],
38 # day too large
39 [1995, 02, 30, 01, 01, 01],
40 # hour too large
41 [1995, 02, 10, 25, 01, 01],
42 # minute too large
43 [1995, 02, 10, 01, 60, 01],
44 # second too large
45 [1995, 02, 10, 01, 01, 60],
46 );
47
48my @neg_time =
49 (
50 # test negative epochs for systems that handle it
51 [ 1969, 12, 31, 16, 59, 59 ],
52 [ 1950, 04, 12, 9, 30, 31 ],
53 );
54
d15eb09c 55# Leap year tests
56my @years =
57 (
58 [ 1900 => 0 ],
59 [ 1947 => 0 ],
60 [ 1996 => 1 ],
61 [ 2000 => 1 ],
62 [ 2100 => 0 ],
63 );
64
93a04732 65# Use 3 days before the start of the epoch because with Borland on
66# Win32 it will work for -3600 _if_ your time zone is +01:00 (or
67# greater).
d15eb09c 68my $neg_epoch_ok = defined ((localtime(-259200))[0]) ? 1 : 0;
5847cf89 69
61bb5906 70# use vmsish 'time' makes for oddness around the Unix epoch
8f230aaa 71if ($^O eq 'VMS') {
5c415a7a 72 $time[0][2]++;
73 $neg_epoch_ok = 0; # time_t is unsigned
74}
61bb5906 75
5847cf89 76my $tests = (@time * 12);
77$tests += @neg_time * 12;
78$tests += @bad_time;
d15eb09c 79$tests += @years;
99ffb1cb 80$tests += 10;
1c41b6a4 81$tests += 2 if $ENV{PERL_CORE};
e6f8b432 82$tests += 8 if $ENV{MAINTAINER};
1c41b6a4 83
823a6996 84plan tests => $tests;
1a3850a5 85
5847cf89 86for (@time, @neg_time) {
1a3850a5 87 my($year, $mon, $mday, $hour, $min, $sec) = @$_;
88 $year -= 1900;
823a6996 89 $mon--;
90
a272e669 91 SKIP: {
e6f8b432 92 skip '1970 test on VOS fails.', 12
93 if $^O eq 'vos' && $year == 70;
94 skip 'this platform does not support negative epochs.', 12
95 if $year < 70 && ! $neg_epoch_ok;
96
97 {
98 my $year_in = $year < 70 ? $year + 1900 : $year;
99 my $time = timelocal($sec,$min,$hour,$mday,$mon,$year_in);
100
101 my($s,$m,$h,$D,$M,$Y) = localtime($time);
102
103 is($s, $sec, "timelocal second for @$_");
104 is($m, $min, "timelocal minute for @$_");
105 is($h, $hour, "timelocal hour for @$_");
106 is($D, $mday, "timelocal day for @$_");
107 is($M, $mon, "timelocal month for @$_");
108 is($Y, $year, "timelocal year for @$_");
109 }
a272e669 110 }
e6f8b432 111
a272e669 112 # Perl has its own gmtime()
113 {
114 my $year_in = $year < 70 ? $year + 1900 : $year;
115 my $time = timegm($sec,$min,$hour,$mday,$mon,$year_in);
e6f8b432 116
a272e669 117 my($s,$m,$h,$D,$M,$Y) = gmtime($time);
e6f8b432 118
a272e669 119 is($s, $sec, "timegm second for @$_");
120 is($m, $min, "timegm minute for @$_");
121 is($h, $hour, "timegm hour for @$_");
122 is($D, $mday, "timegm day for @$_");
123 is($M, $mon, "timegm month for @$_");
124 is($Y, $year, "timegm year for @$_");
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 170{
99ffb1cb 171 eval { timegm(0,0,0,29,1,1900) };
172 like($@, qr/Day '29' out of range 1\.\.28/,
173 'does not accept leap day in 1900');
174
175 eval { timegm(0,0,0,29,1,200) };
176 like($@, qr/Day '29' out of range 1\.\.28/,
177 'does not accept leap day in 2100 (year passed as 200)');
178
179 eval { timegm(0,0,0,29,1,0) };
180 is($@, '', 'no error with leap day of 2000 (year passed as 0)');
181
5847cf89 182 eval { timegm(0,0,0,29,1,1904) };
e6f8b432 183 is($@, '', 'no error with leap day of 1904');
99ffb1cb 184
185 eval { timegm(0,0,0,29,1,4) };
186 is($@, '', 'no error with leap day of 2004 (year passed as 4)');
187
188 eval { timegm(0,0,0,29,1,96) };
189 is($@, '', 'no error with leap day of 1996 (year passed as 96)');
5847cf89 190}
191
823a6996 192if ($ENV{MAINTAINER}) {
e6f8b432 193 require POSIX;
194
195 local $ENV{TZ} = 'Europe/Vienna';
196 POSIX::tzset();
197
198 # 2001-10-28 02:30:00 - could be either summer or standard time,
199 # prefer earlier of the two, in this case summer
200 my $time = timelocal(0, 30, 2, 28, 9, 101);
201 is($time, 1004229000,
202 'timelocal prefers earlier epoch in the presence of a DST change');
203
204 local $ENV{TZ} = 'America/Chicago';
205 POSIX::tzset();
206
207 # Same local time in America/Chicago. There is a transition here
208 # as well.
209 $time = timelocal(0, 30, 1, 28, 9, 101);
210 is($time, 1004250600,
211 'timelocal prefers earlier epoch in the presence of a DST change');
212
213 $time = timelocal(0, 30, 2, 1, 3, 101);
214 is($time, 986113800,
215 'timelocal for non-existent time gives you the time one hour later');
216
217 local $ENV{TZ} = 'Australia/Sydney';
218 POSIX::tzset();
219 # 2001-03-25 02:30:00 in Australia/Sydney. This is the transition
220 # _to_ summer time. The southern hemisphere transitions are
221 # opposite those of the northern.
222 $time = timelocal(0, 30, 2, 25, 2, 101);
223 is($time, 985447800,
224 'timelocal prefers earlier epoch in the presence of a DST change');
225
226 $time = timelocal(0, 30, 2, 28, 9, 101);
227 is($time, 1004200200,
228 'timelocal for non-existent time gives you the time one hour later');
229
230 local $ENV{TZ} = 'Europe/London';
231 POSIX::tzset();
232 $time = timelocal( localtime(1111917720) );
233 is($time, 1111917720,
234 'timelocal for round trip bug on date of DST change for Europe/London');
235
236 # There is no 1:00 AM on this date, as it leaps forward to
237 # 2:00 on the DST change - this should return 2:00 per the
238 # docs.
239 is( ( localtime( timelocal( 0, 0, 1, 27, 2, 2005 ) ) )[2], 2,
240 'hour is 2 when given 1:00 AM on Europe/London date change' );
241
242 is( ( localtime( timelocal( 0, 0, 2, 27, 2, 2005 ) ) )[2], 2,
243 'hour is 2 when given 2:00 AM on Europe/London date change' );
13ef5feb 244}
245
1c41b6a4 246if ($ENV{PERL_CORE}) {
1c41b6a4 247 package test;
248 require 'timelocal.pl';
4ab0373f 249
250 # need to get ok() from main package
e6f8b432 251 ::is(timegm(0,0,0,1,0,80), main::timegm(0,0,0,1,0,80),
823a6996 252 'timegm in timelocal.pl');
1a3850a5 253
e6f8b432 254 ::is(timelocal(1,2,3,4,5,88), main::timelocal(1,2,3,4,5,88),
823a6996 255 'timelocal in timelocal.pl');
1c41b6a4 256}