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