1#!./perl 2 3use strict; 4use warnings; 5 6use Config; 7use Test::More 0.88; 8use Time::Local; 9 10# Set up time values to test 11my @time = 12 ( 13 #year,mon,day,hour,min,sec 14 [1970, 1, 2, 00, 00, 00], 15 [1980, 2, 28, 12, 00, 00], 16 [1980, 2, 29, 12, 00, 00], 17 [1999, 12, 31, 23, 59, 59], 18 [2000, 1, 1, 00, 00, 00], 19 [2010, 10, 12, 14, 13, 12], 20 # leap day 21 [2020, 2, 29, 12, 59, 59], 22 [2030, 7, 4, 17, 07, 06], 23 24# The following test fails on a surprising number of systems 25# so it is commented out. The end of the Epoch for a 32-bit signed 26# implementation of time_t should be Jan 19, 2038 03:14:07 UTC. 27# [2038, 1, 17, 23, 59, 59], # last full day in any tz 28 29 [2010, 10, 12, 14, 13, 12.1], 30 [2010, 10, 12, 14, 13, 59.1], 31 ); 32 33# more than 2**31 time_t - requires a 64bit safe localtime/gmtime 34push @time, [2258, 8, 11, 1, 49, 17] 35 if $] >= 5.012000; 36 37my @bad_time = 38 ( 39 # month too large 40 [1995, 13, 01, 01, 01, 01], 41 # day too large 42 [1995, 02, 30, 01, 01, 01], 43 # hour too large 44 [1995, 02, 10, 25, 01, 01], 45 # minute too large 46 [1995, 02, 10, 01, 60, 01], 47 # second too large 48 [1995, 02, 10, 01, 01, 60], 49 ); 50 51my @neg_time = 52 ( 53 # test negative epochs for systems that handle it 54 [ 1969, 12, 31, 16, 59, 59 ], 55 [ 1950, 04, 12, 9, 30, 31 ], 56 ); 57 58# Leap year tests 59my @years = 60 ( 61 [ 1900 => 0 ], 62 [ 1947 => 0 ], 63 [ 1996 => 1 ], 64 [ 2000 => 1 ], 65 [ 2100 => 0 ], 66 ); 67 68# Use 3 days before the start of the epoch because with Borland on 69# Win32 it will work for -3600 _if_ your time zone is +01:00 (or 70# greater). 71my $neg_epoch_ok = defined ((localtime(-259200))[0]) ? 1 : 0; 72 73# use vmsish 'time' makes for oddness around the Unix epoch 74if ($^O eq 'VMS') { 75 $time[0][2]++; 76 $neg_epoch_ok = 0; # time_t is unsigned 77} 78 79my $epoch_is_64 = eval { $Config{ivsize} == 8 && ( gmtime 2**40 )[5] == 34912 }; 80 81for (@time, @neg_time) { 82 my($year, $mon, $mday, $hour, $min, $sec) = @$_; 83 $year -= 1900; 84 $mon--; 85 86 SKIP: { 87 skip '1970 test on VOS fails.', 12 88 if $^O eq 'vos' && $year == 70; 89 skip 'this platform does not support negative epochs.', 12 90 if $year < 70 && ! $neg_epoch_ok; 91 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, int($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 @$_"); 105 } 106 107 108 # Test timegm() 109 { 110 my $year_in = $year < 70 ? $year + 1900 : $year; 111 my $time = timegm($sec,$min,$hour,$mday,$mon,$year_in); 112 113 my($s,$m,$h,$D,$M,$Y) = gmtime($time); 114 115 is($s, int($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 @$_"); 121 } 122 } 123} 124 125 126for (@bad_time) { 127 my($year, $mon, $mday, $hour, $min, $sec) = @$_; 128 $year -= 1900; 129 $mon--; 130 131 eval { timegm($sec,$min,$hour,$mday,$mon,$year) }; 132 133 like($@, qr/.*out of range.*/, 'invalid time caused an error'); 134} 135 136{ 137 is(timelocal(0,0,1,1,0,90) - timelocal(0,0,0,1,0,90), 3600, 138 'one hour difference between two calls to timelocal'); 139 140 is(timelocal(1,2,3,1,0,100) - timelocal(1,2,3,31,11,99), 24 * 3600, 141 'one day difference between two calls to timelocal'); 142 143 # Diff beween Jan 1, 1980 and Mar 1, 1980 = (31 + 29 = 60 days) 144 is(timegm(0,0,0, 1, 2, 80) - timegm(0,0,0, 1, 0, 80), 60 * 24 * 3600, 145 '60 day difference between two calls to timegm'); 146} 147 148# bugid #19393 149# At a DST transition, the clock skips forward, eg from 01:59:59 to 150# 03:00:00. In this case, 02:00:00 is an invalid time, and should be 151# treated like 03:00:00 rather than 01:00:00 - negative zone offsets used 152# to do the latter 153{ 154 my $hour = (localtime(timelocal(0, 0, 2, 7, 3, 102)))[2]; 155 # testers in US/Pacific should get 3, 156 # other testers should get 2 157 ok($hour == 2 || $hour == 3, 'hour should be 2 or 3'); 158} 159 160for my $p (@years) { 161 my ( $year, $is_leap_year ) = @$p; 162 163 my $string = $is_leap_year ? 'is' : 'is not'; 164 is( Time::Local::_is_leap_year($year), $is_leap_year, 165 "$year $string a leap year" ); 166} 167 168SKIP: 169{ 170 skip 'this platform does not support negative epochs.', 6 171 unless $neg_epoch_ok; 172 173 eval { timegm(0,0,0,29,1,1900) }; 174 like($@, qr/Day '29' out of range 1\.\.28/, 175 'does not accept leap day in 1900'); 176 177 eval { timegm(0,0,0,29,1,200) }; 178 like($@, qr/Day '29' out of range 1\.\.28/, 179 'does not accept leap day in 2100 (year passed as 200)'); 180 181 eval { timegm(0,0,0,29,1,0) }; 182 is($@, '', 'no error with leap day of 2000 (year passed as 0)'); 183 184 eval { timegm(0,0,0,29,1,1904) }; 185 is($@, '', 'no error with leap day of 1904'); 186 187 eval { timegm(0,0,0,29,1,4) }; 188 is($@, '', 'no error with leap day of 2004 (year passed as 4)'); 189 190 eval { timegm(0,0,0,29,1,96) }; 191 is($@, '', 'no error with leap day of 1996 (year passed as 96)'); 192} 193 194SKIP: 195{ 196 skip 'These tests require a system with 64-bit time_t.', 3 197 unless $epoch_is_64; 198 199 is( timegm( 8, 14, 3, 19, 0, ( 1900 + 138 ) ), 2**31, 200 'can call timegm for 2**31 epoch seconds' ); 201 is( timegm( 16, 28, 6, 7, 1, ( 1900 + 206 ) ), 2**32, 202 'can call timegm for 2**32 epoch seconds (on a 64-bit system)' ); 203 is( timegm( 16, 36, 0, 20, 1, ( 34912 + 1900 ) ), 2**40, 204 'can call timegm for 2**40 epoch seconds (on a 64-bit system)' ); 205} 206 207SKIP: 208{ 209 skip 'These tests only run for the package maintainer.', 8 210 unless $ENV{MAINTAINER}; 211 212 require POSIX; 213 214 local $ENV{TZ} = 'Europe/Vienna'; 215 POSIX::tzset(); 216 217 # 2001-10-28 02:30:00 - could be either summer or standard time, 218 # prefer earlier of the two, in this case summer 219 my $time = timelocal(0, 30, 2, 28, 9, 101); 220 is($time, 1004229000, 221 'timelocal prefers earlier epoch in the presence of a DST change'); 222 223 local $ENV{TZ} = 'America/Chicago'; 224 POSIX::tzset(); 225 226 # Same local time in America/Chicago. There is a transition here 227 # as well. 228 $time = timelocal(0, 30, 1, 28, 9, 101); 229 is($time, 1004250600, 230 'timelocal prefers earlier epoch in the presence of a DST change'); 231 232 $time = timelocal(0, 30, 2, 1, 3, 101); 233 is($time, 986113800, 234 'timelocal for non-existent time gives you the time one hour later'); 235 236 local $ENV{TZ} = 'Australia/Sydney'; 237 POSIX::tzset(); 238 # 2001-03-25 02:30:00 in Australia/Sydney. This is the transition 239 # _to_ summer time. The southern hemisphere transitions are 240 # opposite those of the northern. 241 $time = timelocal(0, 30, 2, 25, 2, 101); 242 is($time, 985447800, 243 'timelocal prefers earlier epoch in the presence of a DST change'); 244 245 $time = timelocal(0, 30, 2, 28, 9, 101); 246 is($time, 1004200200, 247 'timelocal for non-existent time gives you the time one hour later'); 248 249 local $ENV{TZ} = 'Europe/London'; 250 POSIX::tzset(); 251 $time = timelocal( localtime(1111917720) ); 252 is($time, 1111917720, 253 'timelocal for round trip bug on date of DST change for Europe/London'); 254 255 # There is no 1:00 AM on this date, as it leaps forward to 256 # 2:00 on the DST change - this should return 2:00 per the 257 # docs. 258 is( ( localtime( timelocal( 0, 0, 1, 27, 2, 2005 ) ) )[2], 2, 259 'hour is 2 when given 1:00 AM on Europe/London date change' ); 260 261 is( ( localtime( timelocal( 0, 0, 2, 27, 2, 2005 ) ) )[2], 2, 262 'hour is 2 when given 2:00 AM on Europe/London date change' ); 263} 264 265done_testing(); 266