use integer;
use vars qw( $VERSION @ISA @EXPORT @EXPORT_OK );
-$VERSION = '1.07';
+$VERSION = '1.07_94';
+$VERSION = eval $VERSION;
@ISA = qw( Exporter );
@EXPORT = qw( timegm timelocal );
@EXPORT_OK = qw( timegm_nocheck timelocal_nocheck );
my $Century = $NextCentury - 100;
my $SecOff = 0;
-my (%Options, %Cheat);
+my (%Options, %Cheat, %Min, %Max);
+my ($MinInt, $MaxInt);
-my $MaxInt = ((1<<(8 * $Config{intsize} - 2))-1)*2 + 1;
-my $MaxDay = int(($MaxInt-43200)/86400)-1;
+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;
+}
+
+$Max{Day} = ($MaxInt >> 1) / 43200;
+$Min{Day} = ($MinInt)? -($Max{Day}+1) : 0;
+
+$Max{Sec} = $MaxInt - 86400 * $Max{Day};
+$Min{Sec} = $MinInt - 86400 * $Min{Day};
# Determine the EPOC day for this machine
my $Epoc = 0;
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;
}
+sub _zoneadjust {
+ my ($day, $sec, $time) = @_;
+
+ $sec = $sec + _timegm(localtime($time)) - $time;
+ if ($sec >= 86400) { $day++; $sec -= 86400; }
+ if ($sec < 0) { $day--; $sec += 86400; }
+
+ ($day, $sec);
+}
+
+
sub timegm {
my ($sec,$min,$hour,$mday,$month,$year) = @_;
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 $days = _daygm(undef, undef, undef, $mday, $month, $year);
-
- unless ($Options{no_range_check} or abs($days) < $MaxDay) {
+ my $xsec = $sec + $SecOff + 60*$min + 3600*$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 += $SecOff + 60*$min + 3600*$hour;
-
no integer;
- $sec + 86400*$days;
+ $xsec + 86400 * $days;
}
sub timelocal {
- no integer;
+ # 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));
+
+ # Calculate first guess with a one-day delta to avoid localtime overflow
+ my $delta = ($_[5] < 100)? 86400 : -86400;
+ my $loc_t = _timegm(localtime( $ref_t + $delta )) - $delta;
# 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 -= 3600 if ($delta > 0 && $ref_t >= 3600);
+
# Adjust for timezone
$loc_t = $ref_t + $zone_off;
# Adjust for DST change
$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;
- 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];
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 UTC 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 UTC, B<or> 2001-10-28
+01:30:00 UTC.
+
+When given an ambiguous local time, the timelocal() function should
+always return the epoch for the I<earlier> of the two possible UTC
+times.
+
+=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 perl5-porters@perl.org