1#!./perl 2 3BEGIN { 4 if ($ENV{PERL_CORE}){ 5 chdir('t') if -d 't'; 6 @INC = ('.', '../lib'); 7 } 8} 9 10use strict; 11 12use Config; 13use Test; 14use Time::Local; 15 16# Set up time values to test 17my @time = 18 ( 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], 26 # leap day 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 33 ); 34 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 56# Use 3 days before the start of the epoch because with Borland on 57# Win32 it will work for -3600 _if_ your time zone is +01:00 (or 58# greater). 59my $neg_epoch_ok = defined ((localtime(-259200))[0]) ? 1 : 0; 60 61# use vmsish 'time' makes for oddness around the Unix epoch 62if ($^O eq 'VMS') { 63 $time[0][2]++; 64 $neg_epoch_ok = 0; # time_t is unsigned 65} 66 67my $tests = (@time * 12); 68$tests += @neg_time * 12; 69$tests += @bad_time; 70$tests += 8; 71$tests += 2 if $ENV{PERL_CORE}; 72$tests += 5 if $ENV{MAINTAINER}; 73 74plan tests => $tests; 75 76for (@time, @neg_time) { 77 my($year, $mon, $mday, $hour, $min, $sec) = @$_; 78 $year -= 1900; 79 $mon--; 80 81 if ($^O eq 'vos' && $year == 70) { 82 skip(1, "skipping 1970 test on VOS.\n") for 1..6; 83 } elsif ($year < 70 && ! $neg_epoch_ok) { 84 skip(1, "skipping negative epoch.\n") for 1..6; 85 } else { 86 my $year_in = $year < 70 ? $year + 1900 : $year; 87 my $time = timelocal($sec,$min,$hour,$mday,$mon,$year_in); 88 89 my($s,$m,$h,$D,$M,$Y) = localtime($time); 90 91 ok($s, $sec, 'timelocal second'); 92 ok($m, $min, 'timelocal minute'); 93 ok($h, $hour, 'timelocal hour'); 94 ok($D, $mday, 'timelocal day'); 95 ok($M, $mon, 'timelocal month'); 96 ok($Y, $year, 'timelocal year'); 97 } 98 99 if ($^O eq 'vos' && $year == 70) { 100 skip(1, "skipping 1970 test on VOS.\n") for 1..6; 101 } elsif ($year < 70 && ! $neg_epoch_ok) { 102 skip(1, "skipping negative epoch.\n") for 1..6; 103 } else { 104 my $year_in = $year < 70 ? $year + 1900 : $year; 105 my $time = timegm($sec,$min,$hour,$mday,$mon,$year_in); 106 107 my($s,$m,$h,$D,$M,$Y) = gmtime($time); 108 109 ok($s, $sec, 'timegm second'); 110 ok($m, $min, 'timegm minute'); 111 ok($h, $hour, 'timegm hour'); 112 ok($D, $mday, 'timegm day'); 113 ok($M, $mon, 'timegm month'); 114 ok($Y, $year, 'timegm year'); 115 } 116} 117 118for (@bad_time) { 119 my($year, $mon, $mday, $hour, $min, $sec) = @$_; 120 $year -= 1900; 121 $mon--; 122 123 eval { timegm($sec,$min,$hour,$mday,$mon,$year) }; 124 125 ok($@, qr/.*out of range.*/, 'invalid time caused an error'); 126} 127 128ok(timelocal(0,0,1,1,0,90) - timelocal(0,0,0,1,0,90), 3600, 129 'one hour difference between two calls to timelocal'); 130 131ok(timelocal(1,2,3,1,0,100) - timelocal(1,2,3,31,11,99), 24 * 3600, 132 'one day difference between two calls to timelocal'); 133 134# Diff beween Jan 1, 1980 and Mar 1, 1980 = (31 + 29 = 60 days) 135ok(timegm(0,0,0, 1, 2, 80) - timegm(0,0,0, 1, 0, 80), 60 * 24 * 3600, 136 '60 day difference between two calls to timegm'); 137 138# bugid #19393 139# At a DST transition, the clock skips forward, eg from 01:59:59 to 140# 03:00:00. In this case, 02:00:00 is an invalid time, and should be 141# treated like 03:00:00 rather than 01:00:00 - negative zone offsets used 142# to do the latter 143{ 144 my $hour = (localtime(timelocal(0, 0, 2, 7, 3, 102)))[2]; 145 # testers in US/Pacific should get 3, 146 # other testers should get 2 147 ok($hour == 2 || $hour == 3, 1, 'hour should be 2 or 3'); 148} 149 150if ($neg_epoch_ok) { 151 eval { timegm(0,0,0,29,1,1900) }; 152 ok($@, qr/Day '29' out of range 1\.\.28/); 153 154 eval { timegm(0,0,0,29,1,1904) }; 155 ok($@, ''); 156} else { 157 skip(1, "skipping negative epoch.\n") for 1..2; 158} 159 160# round trip was broken for edge cases 161if ($^O eq "aix" && $Config{osvers} =~ m/^4\.3\./) { 162 skip( 1, "No fix expected for edge case test for $_ on AIX 4.3") for qw( timegm timelocal ); 163} else { 164 ok(sprintf('%x', timegm(gmtime(0x7fffffff))), sprintf('%x', 0x7fffffff), 165 '0x7fffffff round trip through gmtime then timegm'); 166 167 ok(sprintf('%x', timelocal(localtime(0x7fffffff))), sprintf('%x', 0x7fffffff), 168 '0x7fffffff round trip through localtime then timelocal'); 169} 170 171if ($ENV{MAINTAINER}) { 172 eval { require POSIX; POSIX::tzset() }; 173 if ($@) { 174 skip( 1, "Cannot call POSIX::tzset() on this platform\n" ) for 1..3; 175 } 176 else { 177 local $ENV{TZ} = 'Europe/Vienna'; 178 POSIX::tzset(); 179 180 # 2001-10-28 02:30:00 - could be either summer or standard time, 181 # prefer earlier of the two, in this case summer 182 my $time = timelocal(0, 30, 2, 28, 9, 101); 183 ok($time, 1004229000, 184 'timelocal prefers earlier epoch in the presence of a DST change'); 185 186 local $ENV{TZ} = 'America/Chicago'; 187 POSIX::tzset(); 188 189 # Same local time in America/Chicago. There is a transition 190 # here as well. 191 $time = timelocal(0, 30, 1, 28, 9, 101); 192 ok($time, 1004250600, 193 'timelocal prefers earlier epoch in the presence of a DST change'); 194 195 $time = timelocal(0, 30, 2, 1, 3, 101); 196 ok($time, 986113800, 197 'timelocal for non-existent time gives you the time one hour later'); 198 199 local $ENV{TZ} = 'Australia/Sydney'; 200 POSIX::tzset(); 201 202 # 2001-03-25 02:30:00 in Australia/Sydney. This is the transition 203 # _to_ summer time. The southern hemisphere transitions are 204 # opposite those of the northern. 205 $time = timelocal(0, 30, 2, 25, 2, 101); 206 ok($time, 985447800, 207 'timelocal prefers earlier epoch in the presence of a DST change'); 208 209 $time = timelocal(0, 30, 2, 28, 9, 101); 210 ok($time, 1004200200, 211 'timelocal for non-existent time gives you the time one hour later'); 212 } 213} 214 215if ($ENV{PERL_CORE}) { 216 package test; 217 require 'timelocal.pl'; 218 219 # need to get ok() from main package 220 ::ok(timegm(0,0,0,1,0,80), main::timegm(0,0,0,1,0,80), 221 'timegm in timelocal.pl'); 222 223 ::ok(timelocal(1,2,3,4,5,88), main::timelocal(1,2,3,4,5,88), 224 'timelocal in timelocal.pl'); 225} 226