Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | package Time::Local; |
3b825e41 | 2 | use 5.006; |
a0d0e21e LW |
3 | require Exporter; |
4 | use Carp; | |
e7ec2331 | 5 | use Config; |
b75c8c73 | 6 | use strict; |
326557bd | 7 | use integer; |
a0d0e21e | 8 | |
e7ec2331 | 9 | our $VERSION = '1.04'; |
b75c8c73 MS |
10 | our @ISA = qw( Exporter ); |
11 | our @EXPORT = qw( timegm timelocal ); | |
12 | our @EXPORT_OK = qw( timegm_nocheck timelocal_nocheck ); | |
a0d0e21e | 13 | |
326557bd GB |
14 | my @MonthDays = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); |
15 | ||
06ef4121 | 16 | # Determine breakpoint for rolling century |
326557bd GB |
17 | my $ThisYear = (localtime())[5]; |
18 | my $Breakpoint = ($ThisYear + 50) % 100; | |
19 | my $NextCentury = $ThisYear - $ThisYear % 100; | |
20 | $NextCentury += 100 if $Breakpoint < 50; | |
21 | my $Century = $NextCentury - 100; | |
67627c52 | 22 | my $SecOff = 0; |
326557bd GB |
23 | |
24 | my (%Options, %Cheat); | |
25 | ||
67627c52 JH |
26 | my $MaxInt = ((1<<(8 * $Config{intsize} - 2))-1)*2 + 1; |
27 | my $MaxDay = int(($MaxInt-43200)/86400)-1; | |
28 | ||
326557bd | 29 | # Determine the EPOC day for this machine |
88db9e9a PG |
30 | my $Epoc = 0; |
31 | if ($^O eq 'vos') { | |
32 | # work around posix-977 -- VOS doesn't handle dates in | |
33 | # the range 1970-1980. | |
34 | $Epoc = _daygm((0, 0, 0, 1, 0, 70, 4, 0)); | |
67627c52 JH |
35 | } |
36 | elsif ($^O eq 'MacOS') { | |
37 | no integer; | |
38 | ||
39 | $MaxDay *=2 if $^O eq 'MacOS'; # time_t unsigned ... quick hack? | |
40 | # MacOS time() is seconds since 1 Jan 1904, localtime | |
41 | # so we need to calculate an offset to apply later | |
42 | $Epoc = 693901; | |
43 | $SecOff = timelocal(localtime(0)) - timelocal(gmtime(0)); | |
44 | $Epoc += _daygm(gmtime(0)); | |
45 | } | |
46 | else { | |
88db9e9a PG |
47 | $Epoc = _daygm(gmtime(0)); |
48 | } | |
49 | ||
326557bd GB |
50 | %Cheat=(); # clear the cache as epoc has changed |
51 | ||
326557bd GB |
52 | sub _daygm { |
53 | $_[3] + ($Cheat{pack("ss",@_[4,5])} ||= do { | |
54 | my $month = ($_[4] + 10) % 12; | |
55 | my $year = $_[5] + 1900 - $month/10; | |
56 | 365*$year + $year/4 - $year/100 + $year/400 + ($month*306 + 5)/10 - $Epoc | |
57 | }); | |
58 | } | |
59 | ||
60 | ||
61 | sub _timegm { | |
67627c52 JH |
62 | my $sec = $SecOff + $_[0] + 60 * $_[1] + 3600 * $_[2]; |
63 | ||
64 | no integer; | |
65 | ||
66 | $sec + 86400 * &_daygm; | |
326557bd | 67 | } |
9bb8015a | 68 | |
e36f48eb | 69 | |
9bb8015a | 70 | sub timegm { |
326557bd GB |
71 | my ($sec,$min,$hour,$mday,$month,$year) = @_; |
72 | ||
73 | if ($year >= 1000) { | |
74 | $year -= 1900; | |
75 | } | |
76 | elsif ($year < 100 and $year >= 0) { | |
77 | $year += ($year > $Breakpoint) ? $Century : $NextCentury; | |
78 | } | |
79 | ||
80 | unless ($Options{no_range_check}) { | |
81 | if (abs($year) >= 0x7fff) { | |
82 | $year += 1900; | |
83 | croak "Cannot handle date ($sec, $min, $hour, $mday, $month, $year)"; | |
84 | } | |
85 | ||
86 | croak "Month '$month' out of range 0..11" if $month > 11 or $month < 0; | |
87 | ||
88 | my $md = $MonthDays[$month]; | |
89 | ++$md unless $month != 1 or $year % 4 or !($year % 400); | |
90 | ||
91 | croak "Day '$mday' out of range 1..$md" if $mday > $md or $mday < 1; | |
92 | croak "Hour '$hour' out of range 0..23" if $hour > 23 or $hour < 0; | |
93 | croak "Minute '$min' out of range 0..59" if $min > 59 or $min < 0; | |
94 | croak "Second '$sec' out of range 0..59" if $sec > 59 or $sec < 0; | |
06ef4121 | 95 | } |
326557bd GB |
96 | |
97 | my $days = _daygm(undef, undef, undef, $mday, $month, $year); | |
98 | ||
99 | unless ($Options{no_range_check} or abs($days) < $MaxDay) { | |
100 | $year += 1900; | |
101 | croak "Cannot handle date ($sec, $min, $hour, $mday, $month, $year)"; | |
06ef4121 | 102 | } |
326557bd | 103 | |
67627c52 JH |
104 | $sec += $SecOff + 60*$min + 3600*$hour; |
105 | ||
106 | no integer; | |
107 | ||
108 | $sec + 86400*$days; | |
9bb8015a MK |
109 | } |
110 | ||
326557bd | 111 | |
e36f48eb | 112 | sub timegm_nocheck { |
b75c8c73 | 113 | local $Options{no_range_check} = 1; |
e36f48eb GS |
114 | &timegm; |
115 | } | |
116 | ||
326557bd | 117 | |
9bb8015a | 118 | sub timelocal { |
67627c52 | 119 | no integer; |
326557bd GB |
120 | my $ref_t = &timegm; |
121 | my $loc_t = _timegm(localtime($ref_t)); | |
a0d0e21e | 122 | |
326557bd GB |
123 | # Is there a timezone offset from GMT or are we done |
124 | my $zone_off = $ref_t - $loc_t | |
125 | or return $loc_t; | |
16bb4654 | 126 | |
326557bd GB |
127 | # Adjust for timezone |
128 | $loc_t = $ref_t + $zone_off; | |
16bb4654 | 129 | |
326557bd GB |
130 | # Are we close to a DST change or are we done |
131 | my $dst_off = $ref_t - _timegm(localtime($loc_t)) | |
132 | or return $loc_t; | |
133 | ||
134 | # Adjust for DST change | |
135 | $loc_t + $dst_off; | |
a0d0e21e LW |
136 | } |
137 | ||
326557bd | 138 | |
e36f48eb | 139 | sub timelocal_nocheck { |
b75c8c73 | 140 | local $Options{no_range_check} = 1; |
e36f48eb GS |
141 | &timelocal; |
142 | } | |
143 | ||
a0d0e21e | 144 | 1; |
06ef4121 PC |
145 | |
146 | __END__ | |
147 | ||
148 | =head1 NAME | |
149 | ||
150 | Time::Local - efficiently compute time from local and GMT time | |
151 | ||
152 | =head1 SYNOPSIS | |
153 | ||
396e3838 DL |
154 | $time = timelocal($sec,$min,$hour,$mday,$mon,$year); |
155 | $time = timegm($sec,$min,$hour,$mday,$mon,$year); | |
06ef4121 PC |
156 | |
157 | =head1 DESCRIPTION | |
158 | ||
396e3838 | 159 | These routines are the inverse of built-in perl functions localtime() |
06ef4121 PC |
160 | and gmtime(). They accept a date as a six-element array, and return |
161 | the corresponding time(2) value in seconds since the Epoch (Midnight, | |
162 | January 1, 1970). This value can be positive or negative. | |
163 | ||
164 | It is worth drawing particular attention to the expected ranges for | |
eee32007 JH |
165 | the values provided. The value for the day of the month is the actual day |
166 | (ie 1..31), while the month is the number of months since January (0..11). | |
06ef4121 PC |
167 | This is consistent with the values returned from localtime() and gmtime(). |
168 | ||
e36f48eb | 169 | The timelocal() and timegm() functions perform range checking on the |
396e3838 | 170 | input $sec, $min, $hour, $mday, and $mon values by default. If you'd |
e36f48eb GS |
171 | rather they didn't, you can explicitly import the timelocal_nocheck() |
172 | and timegm_nocheck() functions. | |
ac54365a | 173 | |
e36f48eb | 174 | use Time::Local 'timelocal_nocheck'; |
3cb6de81 | 175 | |
a1f33342 | 176 | { |
a1f33342 | 177 | # The 365th day of 1999 |
e36f48eb | 178 | print scalar localtime timelocal_nocheck 0,0,0,365,0,99; |
ac54365a | 179 | |
a1f33342 | 180 | # The twenty thousandth day since 1970 |
e36f48eb | 181 | print scalar localtime timelocal_nocheck 0,0,0,20000,0,70; |
ac54365a | 182 | |
a1f33342 | 183 | # And even the 10,000,000th second since 1999! |
e36f48eb | 184 | print scalar localtime timelocal_nocheck 10000000,0,0,1,0,99; |
a1f33342 | 185 | } |
ac54365a | 186 | |
e36f48eb | 187 | Your mileage may vary when trying these with minutes and hours, |
ac54365a GS |
188 | and it doesn't work at all for months. |
189 | ||
06ef4121 PC |
190 | Strictly speaking, the year should also be specified in a form consistent |
191 | with localtime(), i.e. the offset from 1900. | |
192 | In order to make the interpretation of the year easier for humans, | |
193 | however, who are more accustomed to seeing years as two-digit or four-digit | |
194 | values, the following conventions are followed: | |
195 | ||
196 | =over 4 | |
197 | ||
198 | =item * | |
199 | ||
200 | Years greater than 999 are interpreted as being the actual year, | |
201 | rather than the offset from 1900. Thus, 1963 would indicate the year | |
90ca0aaa | 202 | Martin Luther King won the Nobel prize, not the year 2863. |
06ef4121 PC |
203 | |
204 | =item * | |
205 | ||
206 | Years in the range 100..999 are interpreted as offset from 1900, | |
207 | so that 112 indicates 2012. This rule also applies to years less than zero | |
208 | (but see note below regarding date range). | |
209 | ||
210 | =item * | |
211 | ||
212 | Years in the range 0..99 are interpreted as shorthand for years in the | |
213 | rolling "current century," defined as 50 years on either side of the current | |
214 | year. Thus, today, in 1999, 0 would refer to 2000, and 45 to 2045, | |
215 | but 55 would refer to 1955. Twenty years from now, 55 would instead refer | |
216 | to 2055. This is messy, but matches the way people currently think about | |
217 | two digit dates. Whenever possible, use an absolute four digit year instead. | |
218 | ||
219 | =back | |
220 | ||
221 | The scheme above allows interpretation of a wide range of dates, particularly | |
222 | if 4-digit years are used. | |
90ca0aaa | 223 | |
06ef4121 PC |
224 | Please note, however, that the range of dates that can be actually be handled |
225 | depends on the size of an integer (time_t) on a given platform. | |
226 | Currently, this is 32 bits for most systems, yielding an approximate range | |
227 | from Dec 1901 to Jan 2038. | |
228 | ||
229 | Both timelocal() and timegm() croak if given dates outside the supported | |
230 | range. | |
231 | ||
232 | =head1 IMPLEMENTATION | |
233 | ||
234 | These routines are quite efficient and yet are always guaranteed to agree | |
235 | with localtime() and gmtime(). We manage this by caching the start times | |
236 | of any months we've seen before. If we know the start time of the month, | |
237 | we can always calculate any time within the month. The start times | |
326557bd GB |
238 | are calculated using a mathematical formula. Unlike other algorithms |
239 | that do multiple calls to gmtime(). | |
06ef4121 PC |
240 | |
241 | timelocal() is implemented using the same cache. We just assume that we're | |
242 | translating a GMT time, and then fudge it when we're done for the timezone | |
243 | and daylight savings arguments. Note that the timezone is evaluated for | |
244 | each date because countries occasionally change their official timezones. | |
245 | Assuming that localtime() corrects for these changes, this routine will | |
326557bd | 246 | also be correct. |
06ef4121 PC |
247 | |
248 | =head1 BUGS | |
249 | ||
250 | The whole scheme for interpreting two-digit years can be considered a bug. | |
251 | ||
06ef4121 PC |
252 | The proclivity to croak() is probably a bug. |
253 | ||
254 | =cut | |
326557bd | 255 |