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