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