1Time::Local(3) User Contributed Perl Documentation Time::Local(3)
2
3
4
6 Time::Local - Efficiently compute time from local and GMT time
7
9 version 1.30
10
12 use Time::Local qw( timelocal_posix timegm_posix );
13
14 my $time = timelocal_posix( $sec, $min, $hour, $mday, $mon, $year );
15 my $time = timegm_posix( $sec, $min, $hour, $mday, $mon, $year );
16
18 This module provides functions that are the inverse of built-in perl
19 functions localtime() and gmtime(). They accept a date as a six-element
20 array, and return the corresponding time(2) value in seconds since the
21 system epoch (Midnight, January 1, 1970 GMT on Unix, for example). This
22 value can be positive or negative, though POSIX only requires support
23 for positive values, so dates before the system's epoch may not work on
24 all operating systems.
25
26 It is worth drawing particular attention to the expected ranges for the
27 values provided. The value for the day of the month is the actual day
28 (i.e. 1..31), while the month is the number of months since January
29 (0..11). This is consistent with the values returned from localtime()
30 and gmtime().
31
33 timelocal_posix() and timegm_posix()
34 These functions are the exact inverse of Perl's built-in "localtime"
35 and "gmtime" functions. That means that calling "timelocal_posix(
36 localtime($value) )" will always give you the same $value you started
37 with. The same applies to "timegm_posix( gmtime($value) )".
38
39 The one exception is when the value returned from localtime()
40 represents an ambiguous local time because of a DST change. See the
41 documentation below for more details.
42
43 These functions expect the year value to be the number of years since
44 1900, which is what the localtime() and gmtime() built-ins returns.
45
46 They perform range checking by default on the input $sec, $min, $hour,
47 $mday, and $mon values and will croak (using Carp::croak()) if given a
48 value outside the allowed ranges.
49
50 While it would be nice to make this the default behavior, that would
51 almost certainly break a lot of code, so you must explicitly import
52 these functions and use them instead of the default timelocal() and
53 timegm().
54
55 You are strongly encouraged to use these functions in any new code
56 which uses this module. It will almost certainly make your code's
57 behavior less surprising.
58
59 timelocal_modern() and timegm_modern()
60 When "Time::Local" was first written, it was a common practice to
61 represent years as a two-digit value like 99 for 1999 or 1 for 2001.
62 This caused all sorts of problems (google "Y2K problem" if you're very
63 young) and developers eventually realized that this was a terrible
64 idea.
65
66 The default exports of timelocal() and timegm() do a complicated
67 calculation when given a year value less than 1000. This leads to
68 surprising results in many cases. See "Year Value Interpretation" for
69 details.
70
71 The "time*_modern()" functions do not do this year munging and simply
72 take the year value as provided.
73
74 They perform range checking by default on the input $sec, $min, $hour,
75 $mday, and $mon values and will croak (using Carp::croak()) if given a
76 value outside the allowed ranges.
77
78 timelocal() and timegm()
79 This module exports two functions by default, timelocal() and timegm().
80
81 They perform range checking by default on the input $sec, $min, $hour,
82 $mday, and $mon values and will croak (using Carp::croak()) if given a
83 value outside the allowed ranges.
84
85 Warning: The year value interpretation that these functions and their
86 nocheck variants use will almost certainly lead to bugs in your code,
87 if not now, then in the future. You are strongly discouraged from using
88 these in new code, and you should convert old code to using either the
89 *_posix or *_modern functions if possible.
90
91 timelocal_nocheck() and timegm_nocheck()
92 If you are working with data you know to be valid, you can use the
93 "nocheck" variants, timelocal_nocheck() and timegm_nocheck(). These
94 variants must be explicitly imported.
95
96 If you supply data which is not valid (month 27, second 1,000) the
97 results will be unpredictable (so don't do that).
98
99 Note that my benchmarks show that this is just a 3% speed increase over
100 the checked versions, so unless calling "Time::Local" is the hottest
101 spot in your application, using these nocheck variants is unlikely to
102 have much impact on your application.
103
104 Year Value Interpretation
105 This does not apply to the *_posix or *_modern functions. Use those
106 exports if you want to ensure consistent behavior as your code ages.
107
108 Strictly speaking, the year should be specified in a form consistent
109 with localtime(), i.e. the offset from 1900. In order to make the
110 interpretation of the year easier for humans, however, who are more
111 accustomed to seeing years as two-digit or four-digit values, the
112 following conventions are followed:
113
114 • Years greater than 999 are interpreted as being the actual year,
115 rather than the offset from 1900. Thus, 1964 would indicate the
116 year Martin Luther King won the Nobel prize, not the year 3864.
117
118 • Years in the range 100..999 are interpreted as offset from 1900, so
119 that 112 indicates 2012. This rule also applies to years less than
120 zero (but see note below regarding date range).
121
122 • Years in the range 0..99 are interpreted as shorthand for years in
123 the rolling "current century," defined as 50 years on either side
124 of the current year. Thus, today, in 1999, 0 would refer to 2000,
125 and 45 to 2045, but 55 would refer to 1955. Twenty years from now,
126 55 would instead refer to 2055. This is messy, but matches the way
127 people currently think about two digit dates. Whenever possible,
128 use an absolute four digit year instead.
129
130 The scheme above allows interpretation of a wide range of dates,
131 particularly if 4-digit years are used. But it also means that the
132 behavior of your code changes as time passes, because the rolling
133 "current century" changes each year.
134
135 Limits of time_t
136 On perl versions older than 5.12.0, the range of dates that can be
137 actually be handled depends on the size of "time_t" (usually a signed
138 integer) on the given platform. Currently, this is 32 bits for most
139 systems, yielding an approximate range from Dec 1901 to Jan 2038.
140
141 Both timelocal() and timegm() croak if given dates outside the
142 supported range.
143
144 As of version 5.12.0, perl has stopped using the time implementation of
145 the operating system it's running on. Instead, it has its own
146 implementation of those routines with a safe range of at least +/-
147 2**52 (about 142 million years)
148
149 Ambiguous Local Times (DST)
150 Because of DST changes, there are many time zones where the same local
151 time occurs for two different GMT times on the same day. For example,
152 in the "Europe/Paris" time zone, the local time of 2001-10-28 02:30:00
153 can represent either 2001-10-28 00:30:00 GMT, or 2001-10-28 01:30:00
154 GMT.
155
156 When given an ambiguous local time, the timelocal() function will
157 always return the epoch for the earlier of the two possible GMT times.
158
159 Non-Existent Local Times (DST)
160 When a DST change causes a locale clock to skip one hour forward, there
161 will be an hour's worth of local times that don't exist. Again, for the
162 "Europe/Paris" time zone, the local clock jumped from 2001-03-25
163 01:59:59 to 2001-03-25 03:00:00.
164
165 If the timelocal() function is given a non-existent local time, it will
166 simply return an epoch value for the time one hour later.
167
168 Negative Epoch Values
169 On perl version 5.12.0 and newer, negative epoch values are fully
170 supported.
171
172 On older versions of perl, negative epoch ("time_t") values, which are
173 not officially supported by the POSIX standards, are known not to work
174 on some systems. These include MacOS (pre-OSX) and Win32.
175
176 On systems which do support negative epoch values, this module should
177 be able to cope with dates before the start of the epoch, down the
178 minimum value of time_t for the system.
179
181 These routines are quite efficient and yet are always guaranteed to
182 agree with localtime() and gmtime(). We manage this by caching the
183 start times of any months we've seen before. If we know the start time
184 of the month, we can always calculate any time within the month. The
185 start times are calculated using a mathematical formula. Unlike other
186 algorithms that do multiple calls to gmtime().
187
188 The timelocal() function is implemented using the same cache. We just
189 assume that we're translating a GMT time, and then fudge it when we're
190 done for the timezone and daylight savings arguments. Note that the
191 timezone is evaluated for each date because countries occasionally
192 change their official timezones. Assuming that localtime() corrects for
193 these changes, this routine will also be correct.
194
196 This module is based on a Perl 4 library, timelocal.pl, that was
197 included with Perl 4.036, and was most likely written by Tom
198 Christiansen.
199
200 The current version was written by Graham Barr.
201
203 The whole scheme for interpreting two-digit years can be considered a
204 bug.
205
206 Bugs may be submitted at
207 <https://github.com/houseabsolute/Time-Local/issues>.
208
209 There is a mailing list available for users of this distribution,
210 <mailto:datetime@perl.org>.
211
212 I am also usually active on IRC as 'autarch' on "irc://irc.perl.org".
213
215 The source code repository for Time-Local can be found at
216 <https://github.com/houseabsolute/Time-Local>.
217
219 Dave Rolsky <autarch@urth.org>
220
222 • Florian Ragwitz <rafl@debian.org>
223
224 • J. Nick Koston <nick@cpanel.net>
225
226 • Unknown <unknown@example.com>
227
229 This software is copyright (c) 1997 - 2020 by Graham Barr & Dave
230 Rolsky.
231
232 This is free software; you can redistribute it and/or modify it under
233 the same terms as the Perl 5 programming language system itself.
234
235 The full text of the license can be found in the LICENSE file included
236 with this distribution.
237
238
239
240perl v5.36.0 2023-01-20 Time::Local(3)