From: Rafael Garcia-Suarez Date: Thu, 10 Feb 2005 10:58:26 +0000 (+0000) Subject: Upgrade to Time::Local 1.11 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=5847cf895c6e325eb16d32163270ffec382f535b;p=p5sagit%2Fp5-mst-13.2.git Upgrade to Time::Local 1.11 p4raw-id: //depot/perl@23956 --- diff --git a/lib/Time/Local.pm b/lib/Time/Local.pm index 73407c7..912f17d 100644 --- a/lib/Time/Local.pm +++ b/lib/Time/Local.pm @@ -7,7 +7,7 @@ use strict; use integer; use vars qw( $VERSION @ISA @EXPORT @EXPORT_OK ); -$VERSION = '1.10'; +$VERSION = '1.11'; $VERSION = eval $VERSION; @ISA = qw( Exporter ); @EXPORT = qw( timegm timelocal ); @@ -33,13 +33,18 @@ if ($^O eq 'MacOS') { } else { $MaxInt = ((1 << (8 * $Config{intsize} - 2))-1)*2 + 1; $MinInt = -$MaxInt - 1; + + # On Win32 (and others?) time_t appears to be signed, but negative + # epochs still don't work. - XXX - this is experimental + $MinInt = 0 + unless defined ((localtime(-1))[0]); } $Max{Day} = ($MaxInt >> 1) / 43200; -$Min{Day} = ($MinInt)? -($Max{Day}+1) : 0; +$Min{Day} = $MinInt ? -($Max{Day} + 1) : 0; -$Max{Sec} = $MaxInt - 86400 * $Max{Day}; -$Min{Sec} = $MinInt - 86400 * $Min{Day}; +$Max{Sec} = $MaxInt - 86400 * $Max{Day}; +$Min{Sec} = $MinInt - 86400 * $Min{Day}; # Determine the EPOC day for this machine my $Epoc = 0; @@ -111,6 +116,8 @@ sub timegm { croak "Month '$month' out of range 0..11" if $month > 11 or $month < 0; my $md = $MonthDays[$month]; +# ++$md if $month == 1 and $year % 4 == 0 and +# ($year % 100 != 0 or ($year + 1900) % 400 == 0); ++$md unless $month != 1 or $year % 4 or !($year % 400); croak "Day '$mday' out of range 1..$md" if $mday > $md or $mday < 1; @@ -252,8 +259,8 @@ values, the following conventions are followed: =item * Years greater than 999 are interpreted as being the actual year, -rather than the offset from 1900. Thus, 1963 would indicate the year -Martin Luther King won the Nobel prize, not the year 3863. +rather than the offset from 1900. Thus, 1964 would indicate the year +Martin Luther King won the Nobel prize, not the year 3864. =item * diff --git a/lib/Time/Local.t b/lib/Time/Local.t index 4401d3d..30a6d14 100755 --- a/lib/Time/Local.t +++ b/lib/Time/Local.t @@ -23,6 +23,7 @@ my @time = [1999, 12, 31, 23, 59, 59], [2000, 1, 1, 00, 00, 00], [2010, 10, 12, 14, 13, 12], + # leap day [2020, 2, 29, 12, 59, 59], [2030, 7, 4, 17, 07, 06], # The following test fails on a surprising number of systems @@ -31,51 +32,93 @@ my @time = # [2038, 1, 17, 23, 59, 59], # last full day in any tz ); +my @bad_time = + ( + # month too large + [1995, 13, 01, 01, 01, 01], + # day too large + [1995, 02, 30, 01, 01, 01], + # hour too large + [1995, 02, 10, 25, 01, 01], + # minute too large + [1995, 02, 10, 01, 60, 01], + # second too large + [1995, 02, 10, 01, 01, 60], + ); + +my @neg_time = + ( + # test negative epochs for systems that handle it + [ 1969, 12, 31, 16, 59, 59 ], + [ 1950, 04, 12, 9, 30, 31 ], + ); + +my $neg_epoch_ok = defined ((localtime(-3600))[0]) ? 1 : 0; + # use vmsish 'time' makes for oddness around the Unix epoch if ($^O eq 'VMS') { $time[0][2]++ } -my $tests = (@time * 12) + 6; +my $tests = (@time * 12); +$tests += @neg_time * 12; +$tests += @bad_time; +$tests += 8; $tests += 2 if $ENV{PERL_CORE}; $tests += 5 if $ENV{MAINTAINER}; plan tests => $tests; -for (@time) { +for (@time, @neg_time) { my($year, $mon, $mday, $hour, $min, $sec) = @$_; $year -= 1900; $mon--; if ($^O eq 'vos' && $year == 70) { skip(1, "skipping 1970 test on VOS.\n") for 1..6; + } elsif ($year < 70 && ! $neg_epoch_ok) { + skip(1, "skipping negative epoch.\n") for 1..6; } else { - my $time = timelocal($sec,$min,$hour,$mday,$mon,$year); + my $year_in = $year < 70 ? $year + 1900 : $year; + my $time = timelocal($sec,$min,$hour,$mday,$mon,$year_in); my($s,$m,$h,$D,$M,$Y) = localtime($time); - ok($s, $sec, 'second'); - ok($m, $min, 'minute'); - ok($h, $hour, 'hour'); - ok($D, $mday, 'day'); - ok($M, $mon, 'month'); - ok($Y, $year, 'year'); + ok($s, $sec, 'timelocal second'); + ok($m, $min, 'timelocal minute'); + ok($h, $hour, 'timelocal hour'); + ok($D, $mday, 'timelocal day'); + ok($M, $mon, 'timelocal month'); + ok($Y, $year, 'timelocal year'); } if ($^O eq 'vos' && $year == 70) { skip(1, "skipping 1970 test on VOS.\n") for 1..6; + } elsif ($year < 70 && ! $neg_epoch_ok) { + skip(1, "skipping negative epoch.\n") for 1..6; } else { - my $time = timegm($sec,$min,$hour,$mday,$mon,$year); + my $year_in = $year < 70 ? $year + 1900 : $year; + my $time = timegm($sec,$min,$hour,$mday,$mon,$year_in); my($s,$m,$h,$D,$M,$Y) = gmtime($time); - ok($s, $sec, 'second'); - ok($m, $min, 'minute'); - ok($h, $hour, 'hour'); - ok($D, $mday, 'day'); - ok($M, $mon, 'month'); - ok($Y, $year, 'year'); + ok($s, $sec, 'timegm second'); + ok($m, $min, 'timegm minute'); + ok($h, $hour, 'timegm hour'); + ok($D, $mday, 'timegm day'); + ok($M, $mon, 'timegm month'); + ok($Y, $year, 'timegm year'); } } +for (@bad_time) { + my($year, $mon, $mday, $hour, $min, $sec) = @$_; + $year -= 1900; + $mon--; + + eval { timegm($sec,$min,$hour,$mday,$mon,$year) }; + + ok($@, qr/.*out of range.*/, 'invalid time caused an error'); +} + ok(timelocal(0,0,1,1,0,90) - timelocal(0,0,0,1,0,90), 3600, 'one hour difference between two calls to timelocal'); @@ -98,6 +141,16 @@ ok(timegm(0,0,0, 1, 2, 80) - timegm(0,0,0, 1, 0, 80), 60 * 24 * 3600, ok($hour == 2 || $hour == 3, 1, 'hour should be 2 or 3'); } +if ($neg_epoch_ok) { + eval { timegm(0,0,0,29,1,1900) }; + ok($@, qr/Day '29' out of range 1\.\.28/); + + eval { timegm(0,0,0,29,1,1904) }; + ok($@, ''); +} else { + skip(1, "skipping negative epoch.\n") for 1..2; +} + # round trip was broken for edge cases if ($^O eq "aix" && $Config{osvers} =~ m/^4\.3\./) { skip( 1, "No fix expected for edge case test for $_ on AIX 4.3") for qw( timegm timelocal );