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.12';
+$VERSION = eval $VERSION;
+@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);
my $NextCentury = $ThisYear - $ThisYear % 100;
$NextCentury += 100 if $Breakpoint < 50;
my $Century = $NextCentury - 100;
+my $SecOff = 0;
+
+my (%Options, %Cheat, %Min, %Max);
+my ($MinInt, $MaxInt);
+
+use constant ONE_HOUR => 3600;
+use constant ONE_DAY => 86400;
-my (%Options, %Cheat);
+if ($^O eq 'MacOS') {
+ # time_t is unsigned...
+ $MaxInt = (1 << (8 * $Config{intsize})) - 1;
+ $MinInt = 0;
+} 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;
+
+$Max{Sec} = $MaxInt - ONE_DAY * $Max{Day};
+$Min{Sec} = $MinInt - ONE_DAY * $Min{Day};
# Determine the EPOC day for this machine
my $Epoc = 0;
# 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;
+
+ # 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;
sub _timegm {
- $_[0] + 60 * $_[1] + 3600 * $_[2] + 86400 * &_daygm;
+ my $sec = $SecOff + $_[0] + 60 * $_[1] + ONE_HOUR * $_[2];
+
+ no integer;
+
+ $sec + ONE_DAY * &_daygm;
+}
+
+
+sub _zoneadjust {
+ my ($day, $sec, $time) = @_;
+
+ $sec = $sec + _timegm(localtime($time)) - $time;
+ if ($sec >= ONE_DAY) { $day++; $sec -= ONE_DAY; }
+ if ($sec < 0) { $day--; $sec += ONE_DAY; }
+
+ ($day, $sec);
}
unless ($Options{no_range_check}) {
if (abs($year) >= 0x7fff) {
$year += 1900;
- croak "Cannot handle date ($sec, $min, $hour, $mday, $month, $year)";
+ croak "Cannot handle date ($sec, $min, $hour, $mday, $month, *$year*)";
}
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;
}
my $days = _daygm(undef, undef, undef, $mday, $month, $year);
-
- unless ($Options{no_range_check} or abs($days) < $MaxDay) {
+ my $xsec = $sec + $SecOff + 60*$min + ONE_HOUR*$hour;
+
+ unless ($Options{no_range_check}
+ or ($days > $Min{Day} or $days == $Min{Day} and $xsec >= $Min{Sec})
+ and ($days < $Max{Day} or $days == $Max{Day} and $xsec <= $Max{Sec}))
+ {
+ warn "Day too small - $days > $Min{Day}\n" if $days < $Min{Day};
+ warn "Day too big - $days > $Max{Day}\n" if $days > $Max{Day};
+ warn "Sec too small - $days < $Min{Sec}\n" if $days < $Min{Sec};
+ warn "Sec too big - $days > $Max{Sec}\n" if $days > $Max{Sec};
$year += 1900;
croak "Cannot handle date ($sec, $min, $hour, $mday, $month, $year)";
}
- $sec + 60*$min + 3600*$hour + 86400*$days;
+ no integer;
+
+ $xsec + ONE_DAY * $days;
}
sub timelocal {
+ # Adjust Max/Min allowed times to fit local time zone and call timegm
+ local ($Max{Day}, $Max{Sec}) = _zoneadjust($Max{Day}, $Max{Sec}, $MaxInt);
+ local ($Min{Day}, $Min{Sec}) = _zoneadjust($Min{Day}, $Min{Sec}, $MinInt);
my $ref_t = &timegm;
+
my $loc_t = _timegm(localtime($ref_t));
# Is there a timezone offset from GMT or are we done
my $zone_off = $ref_t - $loc_t
or return $loc_t;
+ # This hack is needed to always pick the first matching time
+ # during a DST change when time would otherwise be ambiguous
+ $zone_off -= ONE_HOUR if $ref_t >= ONE_HOUR;
+
# Adjust for timezone
$loc_t = $ref_t + $zone_off;
or return $loc_t;
# Adjust for DST change
- $loc_t + $dst_off;
+ $loc_t += $dst_off;
+
+ return $loc_t if $dst_off >= 0;
+
+ # 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;
+ my ($s,$m,$h) = localtime($loc_t);
+ $loc_t -= $dst_off if $s != $_[0] || $m != $_[1] || $h != $_[2];
+
+ $loc_t;
}
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 GMT 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
=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 2863.
+rather than the offset from 1900. Thus, 1964 would indicate the year
+Martin Luther King won the Nobel prize, not the year 3864.
=item *
Both timelocal() and timegm() croak if given dates outside the supported
range.
+=head2 Ambiguous Local Times (DST)
+
+Because of DST changes, there are many time zones where the same local
+time occurs for two different GMT times on the same day. For example,
+in the "Europe/Paris" time zone, the local time of 2001-10-28 02:30:00
+can represent either 2001-10-28 00:30:00 GMT, B<or> 2001-10-28
+01:30:00 GMT.
+
+When given an ambiguous local time, the timelocal() function should
+always return the epoch for the I<earlier> of the two possible GMT
+times.
+
+=head2 Non-Existent Local Times (DST)
+
+When a DST change causes a locale clock to skip one hour forward,
+there will be an hour's worth of local times that don't exist. Again,
+for the "Europe/Paris" time zone, the local clock jumped from
+2001-03-25 01:59:59 to 2001-03-25 03:00:00.
+
+If the timelocal() function is given a non-existent local time, it
+will simply return an epoch value for the time one hour later.
+
+=head2 Negative Epoch Values
+
+Negative epoch (time_t) values are not officially supported by the
+POSIX standards, so this module's tests do not test them. On some
+systems, they are known not to work. These include MacOS (pre-OSX)
+and Win32.
+
+On systems which do support negative epoch values, this module should
+be able to cope with dates before the start of the epoch, down the
+minimum value of time_t for the system.
+
=head1 IMPLEMENTATION
These routines are quite efficient and yet are always guaranteed to agree
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 datetime@perl.org
+email list. See http://lists.perl.org/ for more details.
+
+Please submit bugs using the RT system at rt.cpan.org, or as a last
+resort, to the datetime@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