Disable the edge case tests for timegm and timelocal on
[p5sagit/p5-mst-13.2.git] / lib / Time / Local.pm
CommitLineData
a0d0e21e 1package Time::Local;
1c41b6a4 2
a0d0e21e 3require Exporter;
4use Carp;
e7ec2331 5use Config;
b75c8c73 6use strict;
326557bd 7use integer;
a0d0e21e 8
1c41b6a4 9use vars qw( $VERSION @ISA @EXPORT @EXPORT_OK );
823a6996 10$VERSION = '1.07_94';
11$VERSION = eval $VERSION;
1c41b6a4 12@ISA = qw( Exporter );
13@EXPORT = qw( timegm timelocal );
14@EXPORT_OK = qw( timegm_nocheck timelocal_nocheck );
a0d0e21e 15
326557bd 16my @MonthDays = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
17
06ef4121 18# Determine breakpoint for rolling century
326557bd 19my $ThisYear = (localtime())[5];
20my $Breakpoint = ($ThisYear + 50) % 100;
21my $NextCentury = $ThisYear - $ThisYear % 100;
22 $NextCentury += 100 if $Breakpoint < 50;
23my $Century = $NextCentury - 100;
67627c52 24my $SecOff = 0;
326557bd 25
823a6996 26my (%Options, %Cheat, %Min, %Max);
27my ($MinInt, $MaxInt);
326557bd 28
823a6996 29if ($^O eq 'MacOS') {
30 # time_t is unsigned...
31 $MaxInt = (1 << (8 * $Config{intsize})) - 1;
32 $MinInt = 0;
33} else {
34 $MaxInt = ((1 << (8 * $Config{intsize} - 2))-1)*2 + 1;
35 $MinInt = -$MaxInt - 1;
36}
37
38$Max{Day} = ($MaxInt >> 1) / 43200;
39$Min{Day} = ($MinInt)? -($Max{Day}+1) : 0;
40
41$Max{Sec} = $MaxInt - 86400 * $Max{Day};
42$Min{Sec} = $MinInt - 86400 * $Min{Day};
67627c52 43
326557bd 44# Determine the EPOC day for this machine
88db9e9a 45my $Epoc = 0;
46if ($^O eq 'vos') {
47# work around posix-977 -- VOS doesn't handle dates in
48# the range 1970-1980.
49 $Epoc = _daygm((0, 0, 0, 1, 0, 70, 4, 0));
67627c52 50}
51elsif ($^O eq 'MacOS') {
52 no integer;
53
67627c52 54 # MacOS time() is seconds since 1 Jan 1904, localtime
55 # so we need to calculate an offset to apply later
56 $Epoc = 693901;
57 $SecOff = timelocal(localtime(0)) - timelocal(gmtime(0));
58 $Epoc += _daygm(gmtime(0));
59}
60else {
88db9e9a 61 $Epoc = _daygm(gmtime(0));
62}
63
326557bd 64%Cheat=(); # clear the cache as epoc has changed
65
326557bd 66sub _daygm {
67 $_[3] + ($Cheat{pack("ss",@_[4,5])} ||= do {
68 my $month = ($_[4] + 10) % 12;
69 my $year = $_[5] + 1900 - $month/10;
70 365*$year + $year/4 - $year/100 + $year/400 + ($month*306 + 5)/10 - $Epoc
71 });
72}
73
74
75sub _timegm {
67627c52 76 my $sec = $SecOff + $_[0] + 60 * $_[1] + 3600 * $_[2];
77
78 no integer;
79
80 $sec + 86400 * &_daygm;
326557bd 81}
9bb8015a 82
e36f48eb 83
823a6996 84sub _zoneadjust {
85 my ($day, $sec, $time) = @_;
86
87 $sec = $sec + _timegm(localtime($time)) - $time;
88 if ($sec >= 86400) { $day++; $sec -= 86400; }
89 if ($sec < 0) { $day--; $sec += 86400; }
90
91 ($day, $sec);
92}
93
94
9bb8015a 95sub timegm {
326557bd 96 my ($sec,$min,$hour,$mday,$month,$year) = @_;
97
98 if ($year >= 1000) {
99 $year -= 1900;
100 }
101 elsif ($year < 100 and $year >= 0) {
102 $year += ($year > $Breakpoint) ? $Century : $NextCentury;
103 }
104
105 unless ($Options{no_range_check}) {
106 if (abs($year) >= 0x7fff) {
107 $year += 1900;
823a6996 108 croak "Cannot handle date ($sec, $min, $hour, $mday, $month, *$year*)";
326557bd 109 }
110
111 croak "Month '$month' out of range 0..11" if $month > 11 or $month < 0;
112
113 my $md = $MonthDays[$month];
114 ++$md unless $month != 1 or $year % 4 or !($year % 400);
115
116 croak "Day '$mday' out of range 1..$md" if $mday > $md or $mday < 1;
117 croak "Hour '$hour' out of range 0..23" if $hour > 23 or $hour < 0;
118 croak "Minute '$min' out of range 0..59" if $min > 59 or $min < 0;
119 croak "Second '$sec' out of range 0..59" if $sec > 59 or $sec < 0;
06ef4121 120 }
326557bd 121
122 my $days = _daygm(undef, undef, undef, $mday, $month, $year);
823a6996 123 my $xsec = $sec + $SecOff + 60*$min + 3600*$hour;
124
125 unless ($Options{no_range_check}
126 or ($days > $Min{Day} or $days == $Min{Day} and $xsec >= $Min{Sec})
127 and ($days < $Max{Day} or $days == $Max{Day} and $xsec <= $Max{Sec}))
128 {
129 warn "Day too small - $days > $Min{Day}\n" if $days < $Min{Day};
130 warn "Day too big - $days > $Max{Day}\n" if $days > $Max{Day};
131 warn "Sec too small - $days < $Min{Sec}\n" if $days < $Min{Sec};
132 warn "Sec too big - $days > $Max{Sec}\n" if $days > $Max{Sec};
326557bd 133 $year += 1900;
134 croak "Cannot handle date ($sec, $min, $hour, $mday, $month, $year)";
06ef4121 135 }
326557bd 136
67627c52 137 no integer;
138
823a6996 139 $xsec + 86400 * $days;
9bb8015a 140}
141
326557bd 142
e36f48eb 143sub timegm_nocheck {
b75c8c73 144 local $Options{no_range_check} = 1;
e36f48eb 145 &timegm;
146}
147
326557bd 148
9bb8015a 149sub timelocal {
823a6996 150 # Adjust Max/Min allowed times to fit local time zone and call timegm
151 local ($Max{Day}, $Max{Sec}) = _zoneadjust($Max{Day}, $Max{Sec}, $MaxInt);
152 local ($Min{Day}, $Min{Sec}) = _zoneadjust($Min{Day}, $Min{Sec}, $MinInt);
326557bd 153 my $ref_t = &timegm;
823a6996 154
155 # Calculate first guess with a one-day delta to avoid localtime overflow
156 my $delta = ($_[5] < 100)? 86400 : -86400;
157 my $loc_t = _timegm(localtime( $ref_t + $delta )) - $delta;
a0d0e21e 158
326557bd 159 # Is there a timezone offset from GMT or are we done
160 my $zone_off = $ref_t - $loc_t
161 or return $loc_t;
16bb4654 162
823a6996 163 # This hack is needed to always pick the first matching time
164 # during a DST change when time would otherwise be ambiguous
165 $zone_off -= 3600 if ($delta > 0 && $ref_t >= 3600);
166
326557bd 167 # Adjust for timezone
168 $loc_t = $ref_t + $zone_off;
16bb4654 169
326557bd 170 # Are we close to a DST change or are we done
171 my $dst_off = $ref_t - _timegm(localtime($loc_t))
172 or return $loc_t;
173
174 # Adjust for DST change
13ef5feb 175 $loc_t += $dst_off;
176
823a6996 177 return $loc_t if $dst_off >= 0;
178
13ef5feb 179 # for a negative offset from GMT, and if the original date
180 # was a non-extent gap in a forward DST jump, we should
181 # now have the wrong answer - undo the DST adjust;
182
13ef5feb 183 my ($s,$m,$h) = localtime($loc_t);
184 $loc_t -= $dst_off if $s != $_[0] || $m != $_[1] || $h != $_[2];
185
186 $loc_t;
a0d0e21e 187}
188
326557bd 189
e36f48eb 190sub timelocal_nocheck {
b75c8c73 191 local $Options{no_range_check} = 1;
e36f48eb 192 &timelocal;
193}
194
a0d0e21e 1951;
06ef4121 196
197__END__
198
199=head1 NAME
200
201Time::Local - efficiently compute time from local and GMT time
202
203=head1 SYNOPSIS
204
396e3838 205 $time = timelocal($sec,$min,$hour,$mday,$mon,$year);
206 $time = timegm($sec,$min,$hour,$mday,$mon,$year);
06ef4121 207
208=head1 DESCRIPTION
209
396e3838 210These routines are the inverse of built-in perl functions localtime()
06ef4121 211and gmtime(). They accept a date as a six-element array, and return
1c41b6a4 212the corresponding time(2) value in seconds since the system epoch
213(Midnight, January 1, 1970 UTC on Unix, for example). This value can
214be positive or negative, though POSIX only requires support for
215positive values, so dates before the system's epoch may not work on
216all operating systems.
06ef4121 217
218It is worth drawing particular attention to the expected ranges for
eee32007 219the values provided. The value for the day of the month is the actual day
220(ie 1..31), while the month is the number of months since January (0..11).
06ef4121 221This is consistent with the values returned from localtime() and gmtime().
222
e36f48eb 223The timelocal() and timegm() functions perform range checking on the
396e3838 224input $sec, $min, $hour, $mday, and $mon values by default. If you'd
e36f48eb 225rather they didn't, you can explicitly import the timelocal_nocheck()
226and timegm_nocheck() functions.
ac54365a 227
e36f48eb 228 use Time::Local 'timelocal_nocheck';
3cb6de81 229
a1f33342 230 {
a1f33342 231 # The 365th day of 1999
e36f48eb 232 print scalar localtime timelocal_nocheck 0,0,0,365,0,99;
ac54365a 233
a1f33342 234 # The twenty thousandth day since 1970
e36f48eb 235 print scalar localtime timelocal_nocheck 0,0,0,20000,0,70;
ac54365a 236
a1f33342 237 # And even the 10,000,000th second since 1999!
e36f48eb 238 print scalar localtime timelocal_nocheck 10000000,0,0,1,0,99;
a1f33342 239 }
ac54365a 240
e36f48eb 241Your mileage may vary when trying these with minutes and hours,
ac54365a 242and it doesn't work at all for months.
243
06ef4121 244Strictly speaking, the year should also be specified in a form consistent
245with localtime(), i.e. the offset from 1900.
246In order to make the interpretation of the year easier for humans,
247however, who are more accustomed to seeing years as two-digit or four-digit
248values, the following conventions are followed:
249
250=over 4
251
252=item *
253
254Years greater than 999 are interpreted as being the actual year,
255rather than the offset from 1900. Thus, 1963 would indicate the year
90ca0aaa 256Martin Luther King won the Nobel prize, not the year 2863.
06ef4121 257
258=item *
259
260Years in the range 100..999 are interpreted as offset from 1900,
261so that 112 indicates 2012. This rule also applies to years less than zero
262(but see note below regarding date range).
263
264=item *
265
266Years in the range 0..99 are interpreted as shorthand for years in the
267rolling "current century," defined as 50 years on either side of the current
268year. Thus, today, in 1999, 0 would refer to 2000, and 45 to 2045,
269but 55 would refer to 1955. Twenty years from now, 55 would instead refer
270to 2055. This is messy, but matches the way people currently think about
271two digit dates. Whenever possible, use an absolute four digit year instead.
272
273=back
274
275The scheme above allows interpretation of a wide range of dates, particularly
276if 4-digit years are used.
90ca0aaa 277
06ef4121 278Please note, however, that the range of dates that can be actually be handled
279depends on the size of an integer (time_t) on a given platform.
280Currently, this is 32 bits for most systems, yielding an approximate range
281from Dec 1901 to Jan 2038.
282
283Both timelocal() and timegm() croak if given dates outside the supported
284range.
285
823a6996 286=head2 Ambiguous Local Times (DST)
287
288Because of DST changes, there are many time zones where the same local
289time occurs for two different UTC times on the same day. For example,
290in the "Europe/Paris" time zone, the local time of 2001-10-28 02:30:00
291can represent either 2001-10-28 00:30:00 UTC, B<or> 2001-10-28
29201:30:00 UTC.
293
294When given an ambiguous local time, the timelocal() function should
295always return the epoch for the I<earlier> of the two possible UTC
296times.
297
298=head2 Negative Epoch Values
299
300Negative epoch (time_t) values are not officially supported by the
301POSIX standards, so this module's tests do not test them. On some
302systems, they are known not to work. These include MacOS (pre-OSX)
303and Win32.
304
305On systems which do support negative epoch values, this module should
306be able to cope with dates before the start of the epoch, down the
307minimum value of time_t for the system.
308
06ef4121 309=head1 IMPLEMENTATION
310
311These routines are quite efficient and yet are always guaranteed to agree
312with localtime() and gmtime(). We manage this by caching the start times
313of any months we've seen before. If we know the start time of the month,
314we can always calculate any time within the month. The start times
326557bd 315are calculated using a mathematical formula. Unlike other algorithms
316that do multiple calls to gmtime().
06ef4121 317
318timelocal() is implemented using the same cache. We just assume that we're
319translating a GMT time, and then fudge it when we're done for the timezone
320and daylight savings arguments. Note that the timezone is evaluated for
321each date because countries occasionally change their official timezones.
322Assuming that localtime() corrects for these changes, this routine will
326557bd 323also be correct.
06ef4121 324
325=head1 BUGS
326
327The whole scheme for interpreting two-digit years can be considered a bug.
328
1c41b6a4 329=head1 SUPPORT
330
331Support for this module is provided via the perl5-porters@perl.org
332email list. See http://lists.perl.org/ for more details.
333
334Please submit bugs using the RT system at bugs.perl.org, the perlbug
335script, or as a last resort, to the perl5-porters@perl.org list.
336
337=head1 AUTHOR
338
339This module is based on a Perl 4 library, timelocal.pl, that was
340included with Perl 4.036, and was most likely written by Tom
341Christiansen.
342
343The current version was written by Graham Barr.
344
345It is now being maintained separately from the Perl core by Dave
346Rolsky, <autarch@urth.org>.
347
06ef4121 348=cut
326557bd 349