Another missing hunk for MakeMaker from .org.
[p5sagit/p5-mst-13.2.git] / lib / Time / Local.pm
index a8f2e49..c38d07c 100644 (file)
@@ -1,15 +1,16 @@
 package Time::Local;
-use 5.006;
+
 require Exporter;
 use Carp;
 use Config;
 use strict;
 use integer;
 
-our $VERSION    = '1.04';
-our @ISA       = qw( Exporter );
-our @EXPORT    = qw( timegm timelocal );
-our @EXPORT_OK = qw( timegm_nocheck timelocal_nocheck );
+use vars qw( $VERSION @ISA @EXPORT @EXPORT_OK );
+$VERSION    = '1.07';
+@ISA   = qw( Exporter );
+@EXPORT        = qw( timegm timelocal );
+@EXPORT_OK     = qw( timegm_nocheck timelocal_nocheck );
 
 my @MonthDays = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
 
@@ -19,25 +20,36 @@ my $Breakpoint   = ($ThisYear + 50) % 100;
 my $NextCentury  = $ThisYear - $ThisYear % 100;
    $NextCentury += 100 if $Breakpoint < 50;
 my $Century      = $NextCentury - 100;
+my $SecOff       = 0;
 
 my (%Options, %Cheat);
 
+my $MaxInt = ((1<<(8 * $Config{intsize} - 2))-1)*2 + 1;
+my $MaxDay = int(($MaxInt-43200)/86400)-1;
+
 # Determine the EPOC day for this machine
 my $Epoc = 0;
 if ($^O eq 'vos') {
 # work around posix-977 -- VOS doesn't handle dates in
 # the range 1970-1980.
   $Epoc = _daygm((0, 0, 0, 1, 0, 70, 4, 0));
-} else {
+}
+elsif ($^O eq 'MacOS') {
+  no integer;
+
+  $MaxDay *=2 if $^O eq 'MacOS';  # time_t unsigned ... quick hack?
+  # MacOS time() is seconds since 1 Jan 1904, localtime
+  # so we need to calculate an offset to apply later
+  $Epoc = 693901;
+  $SecOff = timelocal(localtime(0)) - timelocal(gmtime(0));
+  $Epoc += _daygm(gmtime(0));
+}
+else {
   $Epoc = _daygm(gmtime(0));
 }
 
 %Cheat=(); # clear the cache as epoc has changed
 
-my $MaxInt = ((1<<(8 * $Config{intsize} - 2))-1)*2 + 1;
-my $MaxDay = int(($MaxInt-43200)/86400)-1;
-
-
 sub _daygm {
     $_[3] + ($Cheat{pack("ss",@_[4,5])} ||= do {
        my $month = ($_[4] + 10) % 12;
@@ -48,7 +60,11 @@ sub _daygm {
 
 
 sub _timegm {
-    $_[0]  +  60 * $_[1]  +  3600 * $_[2]  +  86400 * &_daygm;
+    my $sec = $SecOff + $_[0]  +  60 * $_[1]  +  3600 * $_[2];
+
+    no integer;
+
+    $sec +  86400 * &_daygm;
 }
 
 
@@ -86,7 +102,11 @@ sub timegm {
        croak "Cannot handle date ($sec, $min, $hour, $mday, $month, $year)";
     }
 
-    $sec + 60*$min + 3600*$hour + 86400*$days;
+    $sec += $SecOff + 60*$min + 3600*$hour;
+
+    no integer;
+
+    $sec + 86400*$days;
 }
 
 
@@ -97,6 +117,7 @@ sub timegm_nocheck {
 
 
 sub timelocal {
+    no integer;
     my $ref_t = &timegm;
     my $loc_t = _timegm(localtime($ref_t));
 
@@ -112,7 +133,18 @@ sub timelocal {
        or return $loc_t;
 
     # Adjust for DST change
-    $loc_t + $dst_off;
+    $loc_t += $dst_off;
+
+    # for a negative offset from GMT, and if the original date
+    # was a non-extent gap in a forward DST jump, we should
+    # now have the wrong answer - undo the DST adjust;
+
+    return $loc_t if $zone_off <= 0;
+
+    my ($s,$m,$h) = localtime($loc_t);
+    $loc_t -= $dst_off if $s != $_[0] || $m != $_[1] || $h != $_[2];
+
+    $loc_t;
 }
 
 
@@ -138,8 +170,11 @@ Time::Local - efficiently compute time from local and GMT time
 
 These routines are the inverse of built-in perl functions localtime()
 and gmtime().  They accept a date as a six-element array, and return
-the corresponding time(2) value in seconds since the Epoch (Midnight,
-January 1, 1970).  This value can be positive or negative.
+the corresponding time(2) value in seconds since the system epoch
+(Midnight, January 1, 1970 UTC on Unix, for example).  This value can
+be positive or negative, though POSIX only requires support for
+positive values, so dates before the system's epoch may not work on
+all operating systems.
 
 It is worth drawing particular attention to the expected ranges for
 the values provided.  The value for the day of the month is the actual day
@@ -231,5 +266,24 @@ The whole scheme for interpreting two-digit years can be considered a bug.
 
 The proclivity to croak() is probably a bug.
 
+=head1 SUPPORT
+
+Support for this module is provided via the perl5-porters@perl.org
+email list.  See http://lists.perl.org/ for more details.
+
+Please submit bugs using the RT system at bugs.perl.org, the perlbug
+script, or as a last resort, to the perl5-porters@perl.org list.
+
+=head1 AUTHOR
+
+This module is based on a Perl 4 library, timelocal.pl, that was
+included with Perl 4.036, and was most likely written by Tom
+Christiansen.
+
+The current version was written by Graham Barr.
+
+It is now being maintained separately from the Perl core by Dave
+Rolsky, <autarch@urth.org>.
+
 =cut