X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2Ftimelocal.pl;h=75f1ac1851aa62ff378d99f6e09cb138e66bc047;hb=f4ae0f5e14d1459d62e647fa88facfcf715d1fcf;hp=95b47e1ef9ee4adc717c2d9e0ea0b765b8d3bb75;hpb=e334a159a5616cab575044bafaf68f75b7bb3a16;p=p5sagit%2Fp5-mst-13.2.git diff --git a/lib/timelocal.pl b/lib/timelocal.pl index 95b47e1..75f1ac1 100644 --- a/lib/timelocal.pl +++ b/lib/timelocal.pl @@ -21,6 +21,9 @@ ;# the result of localtime(0) when the package is initialized. The daylight ;# savings offset is currently assumed to be one hour. +;# Both routines return -1 if the integer limit is hit. I.e. for dates +;# after the 1st of January, 2038 on most machines. + CONFIG: { package timelocal; @@ -37,6 +40,7 @@ CONFIG: { $HR = 60 * $MIN; $DAYS = 24 * $HR; $YearFix = ((gmtime(946684800))[5] == 100) ? 100 : 0; + 1; } sub timegm { @@ -45,6 +49,7 @@ sub timegm { local($[) = 0; $ym = pack(C2, @_[5,4]); $cheat = $cheat{$ym} || &cheat; + return -1 if $cheat<0; $cheat + $_[0] * $SEC + $_[1] * $MIN + $_[2] * $HR + ($_[3]-1) * $DAYS; } @@ -53,6 +58,7 @@ sub timelocal { local($[) = 0; $time = &main'timegm + $tzmin*$MIN; + return -1 if $cheat<0; @test = localtime($time); $time -= $HR if $test[2] != $_[2]; $time; @@ -63,17 +69,39 @@ package timelocal; sub cheat { $year = $_[5]; $month = $_[4]; - die "Month out of range 0..11 in ctime.pl\n" if $month > 11; + die "Month out of range 0..11 in timelocal.pl\n" + if $month > 11 || $month < 0; + die "Day out of range 1..31 in timelocal.pl\n" + if $_[3] > 31 || $_[3] < 1; + die "Hour out of range 0..23 in timelocal.pl\n" + if $_[2] > 23 || $_[2] < 0; + die "Minute out of range 0..59 in timelocal.pl\n" + if $_[1] > 59 || $_[1] < 0; + die "Second out of range 0..59 in timelocal.pl\n" + if $_[0] > 59 || $_[0] < 0; $guess = $^T; @g = gmtime($guess); $year += $YearFix if $year < $epoch[5]; + $lastguess = ""; while ($diff = $year - $g[5]) { $guess += $diff * (363 * $DAYS); @g = gmtime($guess); + if (($thisguess = "@g") eq $lastguess){ + return -1; #date beyond this machine's integer limit + } + $lastguess = $thisguess; } while ($diff = $month - $g[4]) { $guess += $diff * (27 * $DAYS); @g = gmtime($guess); + if (($thisguess = "@g") eq $lastguess){ + return -1; #date beyond this machine's integer limit + } + $lastguess = $thisguess; + } + @gfake = gmtime($guess-1); #still being sceptic + if ("@gfake" eq $lastguess){ + return -1; #date beyond this machine's integer limit } $g[3]--; $guess -= $g[0] * $SEC + $g[1] * $MIN + $g[2] * $HR + $g[3] * $DAYS;