1Time::Local(3pm) Perl Programmers Reference Guide Time::Local(3pm)
2
3
4
6 Time::Local - efficiently compute time from local and GMT time
7
9 $time = timelocal($sec,$min,$hour,$mday,$mon,$year);
10 $time = timegm($sec,$min,$hour,$mday,$mon,$year);
11
13 These routines are the inverse of built-in perl functions localtime()
14 and gmtime(). They accept a date as a six-element array, and return
15 the corresponding time(2) value in seconds since the system epoch (Mid‐
16 night, January 1, 1970 GMT on Unix, for example). This value can be
17 positive or negative, though POSIX only requires support for positive
18 values, so dates before the system's epoch may not work on all operat‐
19 ing systems.
20
21 It is worth drawing particular attention to the expected ranges for the
22 values provided. The value for the day of the month is the actual day
23 (ie 1..31), while the month is the number of months since January
24 (0..11). This is consistent with the values returned from localtime()
25 and gmtime().
26
27 The timelocal() and timegm() functions perform range checking on the
28 input $sec, $min, $hour, $mday, and $mon values by default. If you'd
29 rather they didn't, you can explicitly import the timelocal_nocheck()
30 and timegm_nocheck() functions.
31
32 use Time::Local 'timelocal_nocheck';
33
34 {
35 # The 365th day of 1999
36 print scalar localtime timelocal_nocheck 0,0,0,365,0,99;
37
38 # The twenty thousandth day since 1970
39 print scalar localtime timelocal_nocheck 0,0,0,20000,0,70;
40
41 # And even the 10,000,000th second since 1999!
42 print scalar localtime timelocal_nocheck 10000000,0,0,1,0,99;
43 }
44
45 Your mileage may vary when trying these with minutes and hours, and it
46 doesn't work at all for months.
47
48 Strictly speaking, the year should also be specified in a form consis‐
49 tent with localtime(), i.e. the offset from 1900. In order to make the
50 interpretation of the year easier for humans, however, who are more
51 accustomed to seeing years as two-digit or four-digit values, the fol‐
52 lowing conventions are followed:
53
54 · Years greater than 999 are interpreted as being the actual year,
55 rather than the offset from 1900. Thus, 1964 would indicate the
56 year Martin Luther King won the Nobel prize, not the year 3864.
57
58 · Years in the range 100..999 are interpreted as offset from 1900, so
59 that 112 indicates 2012. This rule also applies to years less than
60 zero (but see note below regarding date range).
61
62 · Years in the range 0..99 are interpreted as shorthand for years in
63 the rolling "current century," defined as 50 years on either side
64 of the current year. Thus, today, in 1999, 0 would refer to 2000,
65 and 45 to 2045, but 55 would refer to 1955. Twenty years from now,
66 55 would instead refer to 2055. This is messy, but matches the way
67 people currently think about two digit dates. Whenever possible,
68 use an absolute four digit year instead.
69
70 The scheme above allows interpretation of a wide range of dates, par‐
71 ticularly if 4-digit years are used.
72
73 Please note, however, that the range of dates that can be actually be
74 handled depends on the size of an integer (time_t) on a given platform.
75 Currently, this is 32 bits for most systems, yielding an approximate
76 range from Dec 1901 to Jan 2038.
77
78 Both timelocal() and timegm() croak if given dates outside the sup‐
79 ported range.
80
81 Ambiguous Local Times (DST)
82
83 Because of DST changes, there are many time zones where the same local
84 time occurs for two different GMT times on the same day. For example,
85 in the "Europe/Paris" time zone, the local time of 2001-10-28 02:30:00
86 can represent either 2001-10-28 00:30:00 GMT, or 2001-10-28 01:30:00
87 GMT.
88
89 When given an ambiguous local time, the timelocal() function should
90 always return the epoch for the earlier of the two possible GMT times.
91
92 Non-Existent Local Times (DST)
93
94 When a DST change causes a locale clock to skip one hour forward, there
95 will be an hour's worth of local times that don't exist. Again, for
96 the "Europe/Paris" time zone, the local clock jumped from 2001-03-25
97 01:59:59 to 2001-03-25 03:00:00.
98
99 If the timelocal() function is given a non-existent local time, it will
100 simply return an epoch value for the time one hour later.
101
102 Negative Epoch Values
103
104 Negative epoch (time_t) values are not officially supported by the
105 POSIX standards, so this module's tests do not test them. On some sys‐
106 tems, they are known not to work. These include MacOS (pre-OSX) and
107 Win32.
108
109 On systems which do support negative epoch values, this module should
110 be able to cope with dates before the start of the epoch, down the min‐
111 imum value of time_t for the system.
112
114 These routines are quite efficient and yet are always guaranteed to
115 agree with localtime() and gmtime(). We manage this by caching the
116 start times of any months we've seen before. If we know the start time
117 of the month, we can always calculate any time within the month. The
118 start times are calculated using a mathematical formula. Unlike other
119 algorithms that do multiple calls to gmtime().
120
121 timelocal() is implemented using the same cache. We just assume that
122 we're translating a GMT time, and then fudge it when we're done for the
123 timezone and daylight savings arguments. Note that the timezone is
124 evaluated for each date because countries occasionally change their
125 official timezones. Assuming that localtime() corrects for these
126 changes, this routine will also be correct.
127
129 The whole scheme for interpreting two-digit years can be considered a
130 bug.
131
133 Support for this module is provided via the datetime@perl.org email
134 list. See http://lists.perl.org/ for more details.
135
136 Please submit bugs using the RT system at rt.cpan.org, or as a last
137 resort, to the datetime@perl.org list.
138
140 This module is based on a Perl 4 library, timelocal.pl, that was
141 included with Perl 4.036, and was most likely written by Tom Chris‐
142 tiansen.
143
144 The current version was written by Graham Barr.
145
146 It is now being maintained separately from the Perl core by Dave Rol‐
147 sky, <autarch@urth.org>.
148
149
150
151perl v5.8.8 2001-09-21 Time::Local(3pm)