@@ -5,12 +5,13 @@ use strict;
55use Carp ();
66use Exporter;
77
8- our $VERSION = ' 1.25 ' ;
8+ our $VERSION = ' 1.28 ' ;
99
1010use parent ' Exporter' ;
1111
12- our @EXPORT = qw( timegm timelocal ) ;
13- our @EXPORT_OK = qw( timegm_nocheck timelocal_nocheck ) ;
12+ our @EXPORT = qw( timegm timelocal ) ;
13+ our @EXPORT_OK
14+ = qw( timegm_modern timelocal_modern timegm_nocheck timelocal_nocheck ) ;
1415
1516my @MonthDays = ( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
1617
@@ -62,9 +63,9 @@ if ( $^O eq 'vos' ) {
6263 $Epoc = _daygm( 0, 0, 0, 1, 0, 70, 4, 0 );
6364}
6465elsif ( $^O eq ' MacOS' ) {
65- $MaxDay *= 2 if $^O eq ' MacOS ' ; # time_t unsigned ... quick hack?
66- # MacOS time() is seconds since 1 Jan 1904, localtime
67- # so we need to calculate an offset to apply later
66+ $MaxDay *= 2; # time_t unsigned ... quick hack?
67+ # MacOS time() is seconds since 1 Jan 1904, localtime
68+ # so we need to calculate an offset to apply later
6869 $Epoc = 693901;
6970 $SecOff = timelocal( localtime (0) ) - timelocal( gmtime (0) );
7071 $Epoc += _daygm( gmtime (0) );
7374 $Epoc = _daygm( gmtime (0) );
7475}
7576
76- %Cheat = (); # clear the cache as epoc has changed
77+ %Cheat = (); # clear the cache as epoc has changed
7778
7879sub _daygm {
7980
@@ -105,11 +106,16 @@ sub _timegm {
105106sub timegm {
106107 my ( $sec , $min , $hour , $mday , $month , $year ) = @_ ;
107108
108- if ( $year >= 1000 ) {
109+ if ( $Options { no_year_munging } ) {
109110 $year -= 1900;
110111 }
111- elsif ( $year < 100 and $year >= 0 ) {
112- $year += ( $year > $Breakpoint ) ? $Century : $NextCentury ;
112+ else {
113+ if ( $year >= 1000 ) {
114+ $year -= 1900;
115+ }
116+ elsif ( $year < 100 and $year >= 0 ) {
117+ $year += ( $year > $Breakpoint ) ? $Century : $NextCentury ;
118+ }
113119 }
114120
115121 unless ( $Options {no_range_check } ) {
@@ -164,6 +170,11 @@ sub timegm_nocheck {
164170 return &timegm;
165171}
166172
173+ sub timegm_modern {
174+ local $Options {no_year_munging } = 1;
175+ return &timegm;
176+ }
177+
167178sub timelocal {
168179 my $ref_t = &timegm;
169180 my $loc_for_ref_t = _timegm( localtime ($ref_t ) );
@@ -184,7 +195,7 @@ sub timelocal {
184195 !$dst_off
185196 && ( ( $ref_t - SECS_PER_HOUR )
186197 - _timegm( localtime ( $loc_t - SECS_PER_HOUR ) ) < 0 )
187- ) {
198+ ) {
188199 return $loc_t - SECS_PER_HOUR;
189200 }
190201
@@ -206,6 +217,11 @@ sub timelocal_nocheck {
206217 return &timelocal;
207218}
208219
220+ sub timelocal_modern {
221+ local $Options {no_year_munging } = 1;
222+ return &timelocal;
223+ }
224+
2092251;
210226
211227# ABSTRACT: Efficiently compute time from local and GMT time
@@ -222,7 +238,7 @@ Time::Local - Efficiently compute time from local and GMT time
222238
223239=head1 VERSION
224240
225- version 1.25
241+ version 1.28
226242
227243=head1 SYNOPSIS
228244
@@ -247,6 +263,28 @@ consistent with the values returned from C<localtime()> and C<gmtime()>.
247263
248264=head1 FUNCTIONS
249265
266+ =head2 C<timelocal_modern() > and C<timegm_modern() >
267+
268+ When C<Time::Local > was first written, it was a common practice to represent
269+ years as a two-digit value like C<99 > for C<1999 > or C<1 > for C<2001 > . This
270+ caused all sorts of problems (google "Y2K problem" if you're very young) and
271+ developers eventually realized that this was a terrible idea.
272+
273+ The default exports of C<timelocal() > and C<timegm() > do a complicated
274+ calculation when given a year value less than 1000. This leads to surprising
275+ results in many cases. See L</Year Value Interpretation> for details.
276+
277+ The C<time*_modern() > subs do not do this year munging and simply take the
278+ year value as provided.
279+
280+ While it would be nice to make this the default behavior, that would almost
281+ certainly break a lot of code, so you must explicitly import these subs and
282+ use them instead of the default C<timelocal() > and C<timegm() > .
283+
284+ You are B<strongly > encouraged to use these subs in any new code which uses
285+ this module. It will almost certainly make your code's behavior less
286+ surprising.
287+
250288=head2 C<timelocal() > and C<timegm() >
251289
252290This module exports two functions by default, C<timelocal() > and C<timegm() > .
@@ -270,6 +308,9 @@ will be unpredictable (so don't do that).
270308
271309=head2 Year Value Interpretation
272310
311+ B<This does not apply to C<timelocal_modern > or C<timegm_modern > . Use those
312+ exports if you want to ensure consistent behavior as your code ages.>
313+
273314Strictly speaking, the year should be specified in a form consistent with
274315C<localtime() > , i.e. the offset from 1900. In order to make the interpretation
275316of the year easier for humans, however, who are more accustomed to seeing
@@ -314,9 +355,10 @@ approximate range from Dec 1901 to Jan 2038.
314355Both C<timelocal() > and C<timegm() > croak if given dates outside the supported
315356range.
316357
317- As of version 5.12.0, perl has stopped using the underlying time library of
318- the operating system it's running on and has its own implementation of those
319- routines with a safe range of at least +/ 2**52 (about 142 million years).
358+ As of version 5.12.0, perl has stopped using the time implementation of the
359+ operating system it's running on. Instead, it has its own implementation of
360+ those routines with a safe range of at least +/- 2**52 (about 142 million
361+ years)
320362
321363=head2 Ambiguous Local Times (DST)
322364
@@ -378,13 +420,17 @@ The current version was written by Graham Barr.
378420
379421The whole scheme for interpreting two-digit years can be considered a bug.
380422
381- Bugs may be submitted through L<https://github.com/houseabsolute/Time-Local/issues> .
423+ Bugs may be submitted at L<https://github.com/houseabsolute/Time-Local/issues> .
382424
383425There is a mailing list available for users of this distribution,
384426L<mailto:datetime@perl.org> .
385427
386428I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org > .
387429
430+ =head1 SOURCE
431+
432+ The source code repository for Time-Local can be found at L<https://github.com/houseabsolute/Time-Local> .
433+
388434=head1 AUTHOR
389435
390436Dave Rolsky <autarch@urth.org>
@@ -411,9 +457,12 @@ Unknown <unknown@example.com>
411457
412458=head1 COPYRIGHT AND LICENSE
413459
414- This software is copyright (c) 1997 - 2016 by Graham Barr & Dave Rolsky.
460+ This software is copyright (c) 1997 - 2018 by Graham Barr & Dave Rolsky.
415461
416462This is free software; you can redistribute it and/or modify it under
417463the same terms as the Perl 5 programming language system itself.
418464
465+ The full text of the license can be found in the
466+ F<LICENSE> file included with this distribution.
467+
419468=cut
0 commit comments