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