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