[perl #41318] Patch: win32.c typo in #define MULTIPLICITY
[p5sagit/p5-mst-13.2.git] / lib / Time / Local.pm
CommitLineData
a0d0e21e 1package Time::Local;
1c41b6a4 2
a0d0e21e 3require Exporter;
4use Carp;
e7ec2331 5use Config;
b75c8c73 6use strict;
326557bd 7use integer;
a0d0e21e 8
1c41b6a4 9use vars qw( $VERSION @ISA @EXPORT @EXPORT_OK );
e6f8b432 10$VERSION = '1.13';
11
1eed7ad1 12@ISA = qw( Exporter );
13@EXPORT = qw( timegm timelocal );
14@EXPORT_OK = qw( timegm_nocheck timelocal_nocheck );
a0d0e21e 15
1eed7ad1 16my @MonthDays = ( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
326557bd 17
06ef4121 18# Determine breakpoint for rolling century
1eed7ad1 19my $ThisYear = ( localtime() )[5];
20my $Breakpoint = ( $ThisYear + 50 ) % 100;
21my $NextCentury = $ThisYear - $ThisYear % 100;
22$NextCentury += 100 if $Breakpoint < 50;
23my $Century = $NextCentury - 100;
24my $SecOff = 0;
326557bd 25
1eed7ad1 26my ( %Options, %Cheat );
326557bd 27
1eed7ad1 28use constant SECS_PER_MINUTE => 60;
29use constant SECS_PER_HOUR => 3600;
30use constant SECS_PER_DAY => 86400;
8f230aaa 31
1eed7ad1 32my $MaxInt = ( ( 1 << ( 8 * $Config{intsize} - 2 ) ) -1 ) * 2 + 1;
33my $MaxDay = int( ( $MaxInt - ( SECS_PER_DAY / 2 ) ) / SECS_PER_DAY ) - 1;
34
35if ( $^O eq 'MacOS' ) {
823a6996 36 # time_t is unsigned...
1eed7ad1 37 $MaxInt = ( 1 << ( 8 * $Config{intsize} ) ) - 1;
38}
39else {
40 $MaxInt = ( ( 1 << ( 8 * $Config{intsize} - 2 ) ) - 1 ) * 2 + 1;
823a6996 41}
67627c52 42
326557bd 43# Determine the EPOC day for this machine
88db9e9a 44my $Epoc = 0;
1eed7ad1 45if ( $^O eq 'vos' ) {
46 # work around posix-977 -- VOS doesn't handle dates in the range
47 # 1970-1980.
48 $Epoc = _daygm( 0, 0, 0, 1, 0, 70, 4, 0 );
67627c52 49}
1eed7ad1 50elsif ( $^O eq 'MacOS' ) {
51 $MaxDay *=2 if $^O eq 'MacOS'; # time_t unsigned ... quick hack?
52 # MacOS time() is seconds since 1 Jan 1904, localtime
53 # so we need to calculate an offset to apply later
54 $Epoc = 693901;
55 $SecOff = timelocal( localtime(0)) - timelocal( gmtime(0) ) ;
56 $Epoc += _daygm( gmtime(0) );
67627c52 57}
58else {
1eed7ad1 59 $Epoc = _daygm( gmtime(0) );
88db9e9a 60}
61
1eed7ad1 62%Cheat = (); # clear the cache as epoc has changed
326557bd 63
326557bd 64sub _daygm {
326557bd 65
1eed7ad1 66 # This is written in such a byzantine way in order to avoid
67 # lexical variables and sub calls, for speed
68 return $_[3] + (
69 $Cheat{ pack( 'ss', @_[ 4, 5 ] ) } ||= do {
70 my $month = ( $_[4] + 10 ) % 12;
71 my $year = $_[5] + 1900 - $month / 10;
72
73 ( ( 365 * $year )
74 + ( $year / 4 )
75 - ( $year / 100 )
76 + ( $year / 400 )
77 + ( ( ( $month * 306 ) + 5 ) / 10 )
78 )
79 - $Epoc;
80 }
81 );
326557bd 82}
9bb8015a 83
1eed7ad1 84sub _timegm {
85 my $sec =
86 $SecOff + $_[0] + ( SECS_PER_MINUTE * $_[1] ) + ( SECS_PER_HOUR * $_[2] );
e36f48eb 87
1eed7ad1 88 return $sec + ( SECS_PER_DAY * &_daygm );
823a6996 89}
90
9bb8015a 91sub timegm {
1eed7ad1 92 my ( $sec, $min, $hour, $mday, $month, $year ) = @_;
326557bd 93
1eed7ad1 94 if ( $year >= 1000 ) {
95 $year -= 1900;
326557bd 96 }
1eed7ad1 97 elsif ( $year < 100 and $year >= 0 ) {
98 $year += ( $year > $Breakpoint ) ? $Century : $NextCentury;
326557bd 99 }
100
1eed7ad1 101 unless ( $Options{no_range_check} ) {
102 if ( abs($year) >= 0x7fff ) {
103 $year += 1900;
104 croak
105 "Cannot handle date ($sec, $min, $hour, $mday, $month, *$year*)";
106 }
326557bd 107
1eed7ad1 108 croak "Month '$month' out of range 0..11"
109 if $month > 11
110 or $month < 0;
326557bd 111
112 my $md = $MonthDays[$month];
1eed7ad1 113 ++$md
114 unless $month != 1 or $year % 4 or !( $year % 400 );
115
116 croak "Day '$mday' out of range 1..$md" if $mday > $md or $mday < 1;
117 croak "Hour '$hour' out of range 0..23" if $hour > 23 or $hour < 0;
118 croak "Minute '$min' out of range 0..59" if $min > 59 or $min < 0;
119 croak "Second '$sec' out of range 0..59" if $sec > 59 or $sec < 0;
06ef4121 120 }
326557bd 121
1eed7ad1 122 my $days = _daygm( undef, undef, undef, $mday, $month, $year );
123
124 unless ($Options{no_range_check} or abs($days) < $MaxDay) {
125 my $msg = '';
126 $msg .= "Day too big - $days > $MaxDay\n" if $days > $MaxDay;
127
326557bd 128 $year += 1900;
1eed7ad1 129 $msg .= "Cannot handle date ($sec, $min, $hour, $mday, $month, $year)";
326557bd 130
1eed7ad1 131 croak $msg;
132 }
67627c52 133
1eed7ad1 134 return $sec
135 + $SecOff
136 + ( SECS_PER_MINUTE * $min )
137 + ( SECS_PER_HOUR * $hour )
138 + ( SECS_PER_DAY * $days );
9bb8015a 139}
140
e36f48eb 141sub timegm_nocheck {
b75c8c73 142 local $Options{no_range_check} = 1;
1eed7ad1 143 return &timegm;
e36f48eb 144}
145
9bb8015a 146sub timelocal {
326557bd 147 my $ref_t = &timegm;
e6f8b432 148 my $loc_for_ref_t = _timegm( localtime($ref_t) );
16bb4654 149
e6f8b432 150 my $zone_off = $loc_for_ref_t - $ref_t
151 or return $loc_for_ref_t;
823a6996 152
326557bd 153 # Adjust for timezone
e6f8b432 154 my $loc_t = $ref_t - $zone_off;
16bb4654 155
326557bd 156 # Are we close to a DST change or are we done
e6f8b432 157 my $dst_off = $ref_t - _timegm( localtime($loc_t) );
158
159 # If this evaluates to true, it means that the value in $loc_t is
160 # the _second_ hour after a DST change where the local time moves
161 # backward.
162 if ( ! $dst_off &&
163 ( ( $ref_t - SECS_PER_HOUR ) - _timegm( localtime( $loc_t - SECS_PER_HOUR ) ) < 0 )
164 ) {
165 return $loc_t - SECS_PER_HOUR;
166 }
326557bd 167
168 # Adjust for DST change
13ef5feb 169 $loc_t += $dst_off;
170
e6f8b432 171 return $loc_t if $dst_off > 0;
823a6996 172
e6f8b432 173 # If the original date was a non-extent gap in a forward DST jump,
174 # we should now have the wrong answer - undo the DST adjustment
1eed7ad1 175 my ( $s, $m, $h ) = localtime($loc_t);
13ef5feb 176 $loc_t -= $dst_off if $s != $_[0] || $m != $_[1] || $h != $_[2];
177
1eed7ad1 178 return $loc_t;
a0d0e21e 179}
180
e36f48eb 181sub timelocal_nocheck {
b75c8c73 182 local $Options{no_range_check} = 1;
1eed7ad1 183 return &timelocal;
e36f48eb 184}
185
a0d0e21e 1861;
06ef4121 187
188__END__
189
190=head1 NAME
191
192Time::Local - efficiently compute time from local and GMT time
193
194=head1 SYNOPSIS
195
396e3838 196 $time = timelocal($sec,$min,$hour,$mday,$mon,$year);
197 $time = timegm($sec,$min,$hour,$mday,$mon,$year);
06ef4121 198
199=head1 DESCRIPTION
200
e6f8b432 201This module provides functions that are the inverse of built-in perl
202functions C<localtime()> and C<gmtime()>. They accept a date as a
203six-element array, and return the corresponding C<time(2)> value in
204seconds since the system epoch (Midnight, January 1, 1970 GMT on Unix,
205for example). This value can be positive or negative, though POSIX
206only requires support for positive values, so dates before the
207system's epoch may not work on all operating systems.
06ef4121 208
209It is worth drawing particular attention to the expected ranges for
e6f8b432 210the values provided. The value for the day of the month is the actual
1eed7ad1 211day (ie 1..31), while the month is the number of months since January
e6f8b432 212(0..11). This is consistent with the values returned from
213C<localtime()> and C<gmtime()>.
214
215=head1 FUNCTIONS
216
217This module exports two functions by default, C<timelocal()> and
218C<timegm()>.
06ef4121 219
e6f8b432 220The C<timelocal()> and C<timegm()> functions perform range checking on
221the input $sec, $min, $hour, $mday, and $mon values by default.
222
223If you are working with data you know to be valid, you can speed your
224code up by using the "nocheck" variants, C<timelocal_nocheck()> and
225C<timegm_nocheck()>. These variants must be explicitly imported.
ac54365a 226
1eed7ad1 227 use Time::Local 'timelocal_nocheck';
ac54365a 228
1eed7ad1 229 # The 365th day of 1999
230 print scalar localtime timelocal_nocheck 0,0,0,365,0,99;
ac54365a 231
e6f8b432 232If you supply data which is not valid (month 27, second 1,000) the
233results will be unpredictable (so don't do that).
234
235=head2 Year Value Interpretation
236
237Strictly speaking, the year should be specified in a form consistent
238with C<localtime()>, i.e. the offset from 1900. In order to make the
239interpretation of the year easier for humans, however, who are more
240accustomed to seeing years as two-digit or four-digit values, the
241following conventions are followed:
06ef4121 242
243=over 4
244
245=item *
246
247Years greater than 999 are interpreted as being the actual year,
e6f8b432 248rather than the offset from 1900. Thus, 1964 would indicate the year
5847cf89 249Martin Luther King won the Nobel prize, not the year 3864.
06ef4121 250
251=item *
252
e6f8b432 253Years in the range 100..999 are interpreted as offset from 1900, so
254that 112 indicates 2012. This rule also applies to years less than
255zero (but see note below regarding date range).
06ef4121 256
257=item *
258
259Years in the range 0..99 are interpreted as shorthand for years in the
1eed7ad1 260rolling "current century," defined as 50 years on either side of the
e6f8b432 261current year. Thus, today, in 1999, 0 would refer to 2000, and 45 to
2622045, but 55 would refer to 1955. Twenty years from now, 55 would
263instead refer to 2055. This is messy, but matches the way people
264currently think about two digit dates. Whenever possible, use an
1eed7ad1 265absolute four digit year instead.
06ef4121 266
267=back
268
1eed7ad1 269The scheme above allows interpretation of a wide range of dates,
270particularly if 4-digit years are used.
90ca0aaa 271
e6f8b432 272=head2 Limits of time_t
273
274The range of dates that can be actually be handled depends on the size
275of C<time_t> (usually a signed integer) on the given
1eed7ad1 276platform. Currently, this is 32 bits for most systems, yielding an
277approximate range from Dec 1901 to Jan 2038.
06ef4121 278
e6f8b432 279Both C<timelocal()> and C<timegm()> croak if given dates outside the
1eed7ad1 280supported range.
06ef4121 281
823a6996 282=head2 Ambiguous Local Times (DST)
283
284Because of DST changes, there are many time zones where the same local
e6f8b432 285time occurs for two different GMT times on the same day. For example,
823a6996 286in the "Europe/Paris" time zone, the local time of 2001-10-28 02:30:00
4ab0373f 287can represent either 2001-10-28 00:30:00 GMT, B<or> 2001-10-28
28801:30:00 GMT.
823a6996 289
290When given an ambiguous local time, the timelocal() function should
4ab0373f 291always return the epoch for the I<earlier> of the two possible GMT
823a6996 292times.
293
4ab0373f 294=head2 Non-Existent Local Times (DST)
295
296When a DST change causes a locale clock to skip one hour forward,
e6f8b432 297there will be an hour's worth of local times that don't exist. Again,
4ab0373f 298for the "Europe/Paris" time zone, the local clock jumped from
2992001-03-25 01:59:59 to 2001-03-25 03:00:00.
300
e6f8b432 301If the C<timelocal()> function is given a non-existent local time, it
4ab0373f 302will simply return an epoch value for the time one hour later.
303
823a6996 304=head2 Negative Epoch Values
305
e6f8b432 306Negative epoch (C<time_t>) values are not officially supported by the
307POSIX standards, so this module's tests do not test them. On some
308systems, they are known not to work. These include MacOS (pre-OSX) and
309Win32.
823a6996 310
311On systems which do support negative epoch values, this module should
312be able to cope with dates before the start of the epoch, down the
313minimum value of time_t for the system.
314
06ef4121 315=head1 IMPLEMENTATION
316
1eed7ad1 317These routines are quite efficient and yet are always guaranteed to
e6f8b432 318agree with C<localtime()> and C<gmtime()>. We manage this by caching
319the start times of any months we've seen before. If we know the start
1eed7ad1 320time of the month, we can always calculate any time within the month.
321The start times are calculated using a mathematical formula. Unlike
e6f8b432 322other algorithms that do multiple calls to C<gmtime()>.
06ef4121 323
e6f8b432 324The C<timelocal()> function is implemented using the same cache. We
325just assume that we're translating a GMT time, and then fudge it when
326we're done for the timezone and daylight savings arguments. Note that
327the timezone is evaluated for each date because countries occasionally
328change their official timezones. Assuming that C<localtime()> corrects
329for these changes, this routine will also be correct.
06ef4121 330
331=head1 BUGS
332
1eed7ad1 333The whole scheme for interpreting two-digit years can be considered a
334bug.
06ef4121 335
1c41b6a4 336=head1 SUPPORT
337
1eed7ad1 338Support for this module is provided via the datetime@perl.org email
e6f8b432 339list. See http://lists.perl.org/ for more details.
1c41b6a4 340
e6f8b432 341Please submit bugs to the CPAN RT system at
342http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Time-Local or via email
343at bug-time-local@rt.cpan.org.
1c41b6a4 344
345=head1 AUTHOR
346
347This module is based on a Perl 4 library, timelocal.pl, that was
348included with Perl 4.036, and was most likely written by Tom
349Christiansen.
350
351The current version was written by Graham Barr.
352
353It is now being maintained separately from the Perl core by Dave
354Rolsky, <autarch@urth.org>.
355
06ef4121 356=cut