9 use vars qw( $VERSION @ISA @EXPORT @EXPORT_OK );
11 $VERSION = eval $VERSION;
12 @ISA = qw( Exporter );
13 @EXPORT = qw( timegm timelocal );
14 @EXPORT_OK = qw( timegm_nocheck timelocal_nocheck );
16 my @MonthDays = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
18 # Determine breakpoint for rolling century
19 my $ThisYear = (localtime())[5];
20 my $Breakpoint = ($ThisYear + 50) % 100;
21 my $NextCentury = $ThisYear - $ThisYear % 100;
22 $NextCentury += 100 if $Breakpoint < 50;
23 my $Century = $NextCentury - 100;
26 my (%Options, %Cheat, %Min, %Max);
27 my ($MinInt, $MaxInt);
30 # time_t is unsigned...
31 $MaxInt = (1 << (8 * $Config{intsize})) - 1;
34 $MaxInt = ((1 << (8 * $Config{intsize} - 2))-1)*2 + 1;
35 $MinInt = -$MaxInt - 1;
37 # On Win32 (and others?) time_t appears to be signed, but negative
38 # epochs still don't work. - XXX - this is experimental
40 unless defined ((localtime(-1))[0]);
43 $Max{Day} = ($MaxInt >> 1) / 43200;
44 $Min{Day} = $MinInt ? -($Max{Day} + 1) : 0;
46 $Max{Sec} = $MaxInt - 86400 * $Max{Day};
47 $Min{Sec} = $MinInt - 86400 * $Min{Day};
49 # Determine the EPOC day for this machine
52 # work around posix-977 -- VOS doesn't handle dates in
53 # the range 1970-1980.
54 $Epoc = _daygm((0, 0, 0, 1, 0, 70, 4, 0));
56 elsif ($^O eq 'MacOS') {
59 # MacOS time() is seconds since 1 Jan 1904, localtime
60 # so we need to calculate an offset to apply later
62 $SecOff = timelocal(localtime(0)) - timelocal(gmtime(0));
63 $Epoc += _daygm(gmtime(0));
66 $Epoc = _daygm(gmtime(0));
69 %Cheat=(); # clear the cache as epoc has changed
72 $_[3] + ($Cheat{pack("ss",@_[4,5])} ||= do {
73 my $month = ($_[4] + 10) % 12;
74 my $year = $_[5] + 1900 - $month/10;
75 365*$year + $year/4 - $year/100 + $year/400 + ($month*306 + 5)/10 - $Epoc
81 my $sec = $SecOff + $_[0] + 60 * $_[1] + 3600 * $_[2];
85 $sec + 86400 * &_daygm;
90 my ($day, $sec, $time) = @_;
92 $sec = $sec + _timegm(localtime($time)) - $time;
93 if ($sec >= 86400) { $day++; $sec -= 86400; }
94 if ($sec < 0) { $day--; $sec += 86400; }
101 my ($sec,$min,$hour,$mday,$month,$year) = @_;
106 elsif ($year < 100 and $year >= 0) {
107 $year += ($year > $Breakpoint) ? $Century : $NextCentury;
110 unless ($Options{no_range_check}) {
111 if (abs($year) >= 0x7fff) {
113 croak "Cannot handle date ($sec, $min, $hour, $mday, $month, *$year*)";
116 croak "Month '$month' out of range 0..11" if $month > 11 or $month < 0;
118 my $md = $MonthDays[$month];
119 # ++$md if $month == 1 and $year % 4 == 0 and
120 # ($year % 100 != 0 or ($year + 1900) % 400 == 0);
121 ++$md unless $month != 1 or $year % 4 or !($year % 400);
123 croak "Day '$mday' out of range 1..$md" if $mday > $md or $mday < 1;
124 croak "Hour '$hour' out of range 0..23" if $hour > 23 or $hour < 0;
125 croak "Minute '$min' out of range 0..59" if $min > 59 or $min < 0;
126 croak "Second '$sec' out of range 0..59" if $sec > 59 or $sec < 0;
129 my $days = _daygm(undef, undef, undef, $mday, $month, $year);
130 my $xsec = $sec + $SecOff + 60*$min + 3600*$hour;
132 unless ($Options{no_range_check}
133 or ($days > $Min{Day} or $days == $Min{Day} and $xsec >= $Min{Sec})
134 and ($days < $Max{Day} or $days == $Max{Day} and $xsec <= $Max{Sec}))
136 warn "Day too small - $days > $Min{Day}\n" if $days < $Min{Day};
137 warn "Day too big - $days > $Max{Day}\n" if $days > $Max{Day};
138 warn "Sec too small - $days < $Min{Sec}\n" if $days < $Min{Sec};
139 warn "Sec too big - $days > $Max{Sec}\n" if $days > $Max{Sec};
141 croak "Cannot handle date ($sec, $min, $hour, $mday, $month, $year)";
146 $xsec + 86400 * $days;
151 local $Options{no_range_check} = 1;
157 # Adjust Max/Min allowed times to fit local time zone and call timegm
158 local ($Max{Day}, $Max{Sec}) = _zoneadjust($Max{Day}, $Max{Sec}, $MaxInt);
159 local ($Min{Day}, $Min{Sec}) = _zoneadjust($Min{Day}, $Min{Sec}, $MinInt);
162 # Calculate first guess with a one-day delta to avoid localtime overflow
163 my $delta = ($_[5] < 100)? 86400 : -86400;
164 my $loc_t = _timegm(localtime( $ref_t + $delta )) - $delta;
166 # Is there a timezone offset from GMT or are we done
167 my $zone_off = $ref_t - $loc_t
170 # This hack is needed to always pick the first matching time
171 # during a DST change when time would otherwise be ambiguous
172 $zone_off -= 3600 if ($delta > 0 && $ref_t >= 3600);
174 # Adjust for timezone
175 $loc_t = $ref_t + $zone_off;
177 # Are we close to a DST change or are we done
178 my $dst_off = $ref_t - _timegm(localtime($loc_t))
181 # Adjust for DST change
184 return $loc_t if $dst_off >= 0;
186 # for a negative offset from GMT, and if the original date
187 # was a non-extent gap in a forward DST jump, we should
188 # now have the wrong answer - undo the DST adjust;
190 my ($s,$m,$h) = localtime($loc_t);
191 $loc_t -= $dst_off if $s != $_[0] || $m != $_[1] || $h != $_[2];
197 sub timelocal_nocheck {
198 local $Options{no_range_check} = 1;
208 Time::Local - efficiently compute time from local and GMT time
212 $time = timelocal($sec,$min,$hour,$mday,$mon,$year);
213 $time = timegm($sec,$min,$hour,$mday,$mon,$year);
217 These routines are the inverse of built-in perl functions localtime()
218 and gmtime(). They accept a date as a six-element array, and return
219 the corresponding time(2) value in seconds since the system epoch
220 (Midnight, January 1, 1970 GMT on Unix, for example). This value can
221 be positive or negative, though POSIX only requires support for
222 positive values, so dates before the system's epoch may not work on
223 all operating systems.
225 It is worth drawing particular attention to the expected ranges for
226 the values provided. The value for the day of the month is the actual day
227 (ie 1..31), while the month is the number of months since January (0..11).
228 This is consistent with the values returned from localtime() and gmtime().
230 The timelocal() and timegm() functions perform range checking on the
231 input $sec, $min, $hour, $mday, and $mon values by default. If you'd
232 rather they didn't, you can explicitly import the timelocal_nocheck()
233 and timegm_nocheck() functions.
235 use Time::Local 'timelocal_nocheck';
238 # The 365th day of 1999
239 print scalar localtime timelocal_nocheck 0,0,0,365,0,99;
241 # The twenty thousandth day since 1970
242 print scalar localtime timelocal_nocheck 0,0,0,20000,0,70;
244 # And even the 10,000,000th second since 1999!
245 print scalar localtime timelocal_nocheck 10000000,0,0,1,0,99;
248 Your mileage may vary when trying these with minutes and hours,
249 and it doesn't work at all for months.
251 Strictly speaking, the year should also be specified in a form consistent
252 with localtime(), i.e. the offset from 1900.
253 In order to make the interpretation of the year easier for humans,
254 however, who are more accustomed to seeing years as two-digit or four-digit
255 values, the following conventions are followed:
261 Years greater than 999 are interpreted as being the actual year,
262 rather than the offset from 1900. Thus, 1964 would indicate the year
263 Martin Luther King won the Nobel prize, not the year 3864.
267 Years in the range 100..999 are interpreted as offset from 1900,
268 so that 112 indicates 2012. This rule also applies to years less than zero
269 (but see note below regarding date range).
273 Years in the range 0..99 are interpreted as shorthand for years in the
274 rolling "current century," defined as 50 years on either side of the current
275 year. Thus, today, in 1999, 0 would refer to 2000, and 45 to 2045,
276 but 55 would refer to 1955. Twenty years from now, 55 would instead refer
277 to 2055. This is messy, but matches the way people currently think about
278 two digit dates. Whenever possible, use an absolute four digit year instead.
282 The scheme above allows interpretation of a wide range of dates, particularly
283 if 4-digit years are used.
285 Please note, however, that the range of dates that can be actually be handled
286 depends on the size of an integer (time_t) on a given platform.
287 Currently, this is 32 bits for most systems, yielding an approximate range
288 from Dec 1901 to Jan 2038.
290 Both timelocal() and timegm() croak if given dates outside the supported
293 =head2 Ambiguous Local Times (DST)
295 Because of DST changes, there are many time zones where the same local
296 time occurs for two different GMT times on the same day. For example,
297 in the "Europe/Paris" time zone, the local time of 2001-10-28 02:30:00
298 can represent either 2001-10-28 00:30:00 GMT, B<or> 2001-10-28
301 When given an ambiguous local time, the timelocal() function should
302 always return the epoch for the I<earlier> of the two possible GMT
305 =head2 Non-Existent Local Times (DST)
307 When a DST change causes a locale clock to skip one hour forward,
308 there will be an hour's worth of local times that don't exist. Again,
309 for the "Europe/Paris" time zone, the local clock jumped from
310 2001-03-25 01:59:59 to 2001-03-25 03:00:00.
312 If the timelocal() function is given a non-existent local time, it
313 will simply return an epoch value for the time one hour later.
315 =head2 Negative Epoch Values
317 Negative epoch (time_t) values are not officially supported by the
318 POSIX standards, so this module's tests do not test them. On some
319 systems, they are known not to work. These include MacOS (pre-OSX)
322 On systems which do support negative epoch values, this module should
323 be able to cope with dates before the start of the epoch, down the
324 minimum value of time_t for the system.
326 =head1 IMPLEMENTATION
328 These routines are quite efficient and yet are always guaranteed to agree
329 with localtime() and gmtime(). We manage this by caching the start times
330 of any months we've seen before. If we know the start time of the month,
331 we can always calculate any time within the month. The start times
332 are calculated using a mathematical formula. Unlike other algorithms
333 that do multiple calls to gmtime().
335 timelocal() is implemented using the same cache. We just assume that we're
336 translating a GMT time, and then fudge it when we're done for the timezone
337 and daylight savings arguments. Note that the timezone is evaluated for
338 each date because countries occasionally change their official timezones.
339 Assuming that localtime() corrects for these changes, this routine will
344 The whole scheme for interpreting two-digit years can be considered a bug.
348 Support for this module is provided via the datetime@perl.org
349 email list. See http://lists.perl.org/ for more details.
351 Please submit bugs using the RT system at rt.cpan.org, or as a last
352 resort, to the datetime@perl.org list.
356 This module is based on a Perl 4 library, timelocal.pl, that was
357 included with Perl 4.036, and was most likely written by Tom
360 The current version was written by Graham Barr.
362 It is now being maintained separately from the Perl core by Dave
363 Rolsky, <autarch@urth.org>.