1package Time::Local; 2 3use strict; 4 5use Carp (); 6use Exporter; 7 8our $VERSION = '1.30'; 9 10use parent 'Exporter'; 11 12our @EXPORT = qw( timegm timelocal ); 13our @EXPORT_OK = qw( 14 timegm_modern 15 timelocal_modern 16 timegm_nocheck 17 timelocal_nocheck 18 timegm_posix 19 timelocal_posix 20); 21 22my @MonthDays = ( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ); 23 24# Determine breakpoint for rolling century 25my $ThisYear = ( localtime() )[5]; 26my $Breakpoint = ( $ThisYear + 50 ) % 100; 27my $NextCentury = $ThisYear - $ThisYear % 100; 28$NextCentury += 100 if $Breakpoint < 50; 29my $Century = $NextCentury - 100; 30my $SecOff = 0; 31 32my ( %Options, %Cheat ); 33 34use constant SECS_PER_MINUTE => 60; 35use constant SECS_PER_HOUR => 3600; 36use constant SECS_PER_DAY => 86400; 37 38my $MaxDay; 39if ( $] < 5.012000 ) { 40 require Config; 41 ## no critic (Variables::ProhibitPackageVars) 42 43 my $MaxInt; 44 if ( $^O eq 'MacOS' ) { 45 46 # time_t is unsigned... 47 $MaxInt = ( 1 << ( 8 * $Config::Config{ivsize} ) ) 48 - 1; ## no critic qw(ProhibitPackageVars) 49 } 50 else { 51 $MaxInt 52 = ( ( 1 << ( 8 * $Config::Config{ivsize} - 2 ) ) - 1 ) * 2 53 + 1; ## no critic qw(ProhibitPackageVars) 54 } 55 56 $MaxDay = int( ( $MaxInt - ( SECS_PER_DAY / 2 ) ) / SECS_PER_DAY ) - 1; 57} 58else { 59 # recent localtime()'s limit is the year 2**31 60 $MaxDay = 365 * ( 2**31 ); 61} 62 63# Determine the EPOC day for this machine 64my $Epoc = 0; 65if ( $^O eq 'vos' ) { 66 67 # work around posix-977 -- VOS doesn't handle dates in the range 68 # 1970-1980. 69 $Epoc = _daygm( 0, 0, 0, 1, 0, 70, 4, 0 ); 70} 71elsif ( $^O eq 'MacOS' ) { 72 $MaxDay *= 2; # time_t unsigned ... quick hack? 73 # MacOS time() is seconds since 1 Jan 1904, localtime 74 # so we need to calculate an offset to apply later 75 $Epoc = 693901; 76 $SecOff = timelocal( localtime(0) ) - timelocal( gmtime(0) ); 77 $Epoc += _daygm( gmtime(0) ); 78} 79else { 80 $Epoc = _daygm( gmtime(0) ); 81} 82 83%Cheat = (); # clear the cache as epoc has changed 84 85sub _daygm { 86 87 # This is written in such a byzantine way in order to avoid 88 # lexical variables and sub calls, for speed 89 return $_[3] + ( 90 $Cheat{ pack( 'ss', @_[ 4, 5 ] ) } ||= do { 91 my $month = ( $_[4] + 10 ) % 12; 92 my $year = $_[5] + 1900 - int( $month / 10 ); 93 94 ( ( 365 * $year ) 95 + int( $year / 4 ) 96 - int( $year / 100 ) 97 + int( $year / 400 ) 98 + int( ( ( $month * 306 ) + 5 ) / 10 ) ) - $Epoc; 99 } 100 ); 101} 102 103sub _timegm { 104 my $sec 105 = $SecOff + $_[0] 106 + ( SECS_PER_MINUTE * $_[1] ) 107 + ( SECS_PER_HOUR * $_[2] ); 108 109 return $sec + ( SECS_PER_DAY * &_daygm ); 110} 111 112sub timegm { 113 my ( $sec, $min, $hour, $mday, $month, $year ) = @_; 114 115 if ( $Options{no_year_munging} ) { 116 $year -= 1900; 117 } 118 elsif ( !$Options{posix_year} ) { 119 if ( $year >= 1000 ) { 120 $year -= 1900; 121 } 122 elsif ( $year < 100 and $year >= 0 ) { 123 $year += ( $year > $Breakpoint ) ? $Century : $NextCentury; 124 } 125 } 126 127 unless ( $Options{no_range_check} ) { 128 Carp::croak("Month '$month' out of range 0..11") 129 if $month > 11 130 or $month < 0; 131 132 my $md = $MonthDays[$month]; 133 ++$md 134 if $month == 1 && _is_leap_year( $year + 1900 ); 135 136 Carp::croak("Day '$mday' out of range 1..$md") 137 if $mday > $md or $mday < 1; 138 Carp::croak("Hour '$hour' out of range 0..23") 139 if $hour > 23 or $hour < 0; 140 Carp::croak("Minute '$min' out of range 0..59") 141 if $min > 59 or $min < 0; 142 Carp::croak("Second '$sec' out of range 0..59") 143 if $sec >= 60 or $sec < 0; 144 } 145 146 my $days = _daygm( undef, undef, undef, $mday, $month, $year ); 147 148 unless ( $Options{no_range_check} or abs($days) < $MaxDay ) { 149 my $msg = q{}; 150 $msg .= "Day too big - $days > $MaxDay\n" if $days > $MaxDay; 151 152 $year += 1900; 153 $msg 154 .= "Cannot handle date ($sec, $min, $hour, $mday, $month, $year)"; 155 156 Carp::croak($msg); 157 } 158 159 return 160 $sec + $SecOff 161 + ( SECS_PER_MINUTE * $min ) 162 + ( SECS_PER_HOUR * $hour ) 163 + ( SECS_PER_DAY * $days ); 164} 165 166sub _is_leap_year { 167 return 0 if $_[0] % 4; 168 return 1 if $_[0] % 100; 169 return 0 if $_[0] % 400; 170 171 return 1; 172} 173 174sub timegm_nocheck { 175 local $Options{no_range_check} = 1; 176 return &timegm; 177} 178 179sub timegm_modern { 180 local $Options{no_year_munging} = 1; 181 return &timegm; 182} 183 184sub timegm_posix { 185 local $Options{posix_year} = 1; 186 return &timegm; 187} 188 189sub timelocal { 190 my $ref_t = &timegm; 191 my $loc_for_ref_t = _timegm( localtime($ref_t) ); 192 193 my $zone_off = $loc_for_ref_t - $ref_t 194 or return $loc_for_ref_t; 195 196 # Adjust for timezone 197 my $loc_t = $ref_t - $zone_off; 198 199 # Are we close to a DST change or are we done 200 my $dst_off = $ref_t - _timegm( localtime($loc_t) ); 201 202 # If this evaluates to true, it means that the value in $loc_t is 203 # the _second_ hour after a DST change where the local time moves 204 # backward. 205 if ( 206 !$dst_off 207 && ( ( $ref_t - SECS_PER_HOUR ) 208 - _timegm( localtime( $loc_t - SECS_PER_HOUR ) ) < 0 ) 209 ) { 210 return $loc_t - SECS_PER_HOUR; 211 } 212 213 # Adjust for DST change 214 $loc_t += $dst_off; 215 216 return $loc_t if $dst_off > 0; 217 218 # If the original date was a non-existent gap in a forward DST jump, we 219 # should now have the wrong answer - undo the DST adjustment 220 my ( $s, $m, $h ) = localtime($loc_t); 221 $loc_t -= $dst_off if $s != $_[0] || $m != $_[1] || $h != $_[2]; 222 223 return $loc_t; 224} 225 226sub timelocal_nocheck { 227 local $Options{no_range_check} = 1; 228 return &timelocal; 229} 230 231sub timelocal_modern { 232 local $Options{no_year_munging} = 1; 233 return &timelocal; 234} 235 236sub timelocal_posix { 237 local $Options{posix_year} = 1; 238 return &timelocal; 239} 240 2411; 242 243# ABSTRACT: Efficiently compute time from local and GMT time 244 245__END__ 246 247=pod 248 249=encoding UTF-8 250 251=head1 NAME 252 253Time::Local - Efficiently compute time from local and GMT time 254 255=head1 VERSION 256 257version 1.30 258 259=head1 SYNOPSIS 260 261 use Time::Local qw( timelocal_posix timegm_posix ); 262 263 my $time = timelocal_posix( $sec, $min, $hour, $mday, $mon, $year ); 264 my $time = timegm_posix( $sec, $min, $hour, $mday, $mon, $year ); 265 266=head1 DESCRIPTION 267 268This module provides functions that are the inverse of built-in perl functions 269C<localtime()> and C<gmtime()>. They accept a date as a six-element array, and 270return the corresponding C<time(2)> value in seconds since the system epoch 271(Midnight, January 1, 1970 GMT on Unix, for example). This value can be 272positive or negative, though POSIX only requires support for positive values, 273so dates before the system's epoch may not work on all operating systems. 274 275It is worth drawing particular attention to the expected ranges for the values 276provided. The value for the day of the month is the actual day (i.e. 1..31), 277while the month is the number of months since January (0..11). This is 278consistent with the values returned from C<localtime()> and C<gmtime()>. 279 280=head1 FUNCTIONS 281 282=head2 C<timelocal_posix()> and C<timegm_posix()> 283 284These functions are the exact inverse of Perl's built-in C<localtime> and 285C<gmtime> functions. That means that calling C<< timelocal_posix( 286localtime($value) ) >> will always give you the same C<$value> you started 287with. The same applies to C<< timegm_posix( gmtime($value) ) >>. 288 289The one exception is when the value returned from C<localtime()> represents an 290ambiguous local time because of a DST change. See the documentation below for 291more details. 292 293These functions expect the year value to be the number of years since 1900, 294which is what the C<localtime()> and C<gmtime()> built-ins returns. 295 296They perform range checking by default on the input C<$sec>, C<$min>, 297C<$hour>, C<$mday>, and C<$mon> values and will croak (using C<Carp::croak()>) 298if given a value outside the allowed ranges. 299 300While it would be nice to make this the default behavior, that would almost 301certainly break a lot of code, so you must explicitly import these functions 302and use them instead of the default C<timelocal()> and C<timegm()>. 303 304You are B<strongly> encouraged to use these functions in any new code which 305uses this module. It will almost certainly make your code's behavior less 306surprising. 307 308=head2 C<timelocal_modern()> and C<timegm_modern()> 309 310When C<Time::Local> was first written, it was a common practice to represent 311years as a two-digit value like C<99> for C<1999> or C<1> for C<2001>. This 312caused all sorts of problems (google "Y2K problem" if you're very young) and 313developers eventually realized that this was a terrible idea. 314 315The default exports of C<timelocal()> and C<timegm()> do a complicated 316calculation when given a year value less than 1000. This leads to surprising 317results in many cases. See L</Year Value Interpretation> for details. 318 319The C<time*_modern()> functions do not do this year munging and simply take 320the year value as provided. 321 322They perform range checking by default on the input C<$sec>, C<$min>, 323C<$hour>, C<$mday>, and C<$mon> values and will croak (using C<Carp::croak()>) 324if given a value outside the allowed ranges. 325 326=head2 C<timelocal()> and C<timegm()> 327 328This module exports two functions by default, C<timelocal()> and C<timegm()>. 329 330They perform range checking by default on the input C<$sec>, C<$min>, 331C<$hour>, C<$mday>, and C<$mon> values and will croak (using C<Carp::croak()>) 332if given a value outside the allowed ranges. 333 334B<Warning: The year value interpretation that these functions and their 335nocheck variants use will almost certainly lead to bugs in your code, if not 336now, then in the future. You are strongly discouraged from using these in new 337code, and you should convert old code to using either the C<*_posix> or 338C<*_modern> functions if possible.> 339 340=head2 C<timelocal_nocheck()> and C<timegm_nocheck()> 341 342If you are working with data you know to be valid, you can use the "nocheck" 343variants, C<timelocal_nocheck()> and C<timegm_nocheck()>. These variants must 344be explicitly imported. 345 346If you supply data which is not valid (month 27, second 1,000) the results 347will be unpredictable (so don't do that). 348 349Note that my benchmarks show that this is just a 3% speed increase over the 350checked versions, so unless calling C<Time::Local> is the hottest spot in your 351application, using these nocheck variants is unlikely to have much impact on 352your application. 353 354=head2 Year Value Interpretation 355 356B<This does not apply to the C<*_posix> or C<*_modern> functions. Use those 357exports if you want to ensure consistent behavior as your code ages.> 358 359Strictly speaking, the year should be specified in a form consistent with 360C<localtime()>, i.e. the offset from 1900. In order to make the interpretation 361of the year easier for humans, however, who are more accustomed to seeing 362years as two-digit or four-digit values, the following conventions are 363followed: 364 365=over 4 366 367=item * 368 369Years greater than 999 are interpreted as being the actual year, rather than 370the offset from 1900. Thus, 1964 would indicate the year Martin Luther King 371won the Nobel prize, not the year 3864. 372 373=item * 374 375Years in the range 100..999 are interpreted as offset from 1900, so that 112 376indicates 2012. This rule also applies to years less than zero (but see note 377below regarding date range). 378 379=item * 380 381Years in the range 0..99 are interpreted as shorthand for years in the rolling 382"current century," defined as 50 years on either side of the current 383year. Thus, today, in 1999, 0 would refer to 2000, and 45 to 2045, but 55 384would refer to 1955. Twenty years from now, 55 would instead refer to 3852055. This is messy, but matches the way people currently think about two 386digit dates. Whenever possible, use an absolute four digit year instead. 387 388=back 389 390The scheme above allows interpretation of a wide range of dates, particularly 391if 4-digit years are used. But it also means that the behavior of your code 392changes as time passes, because the rolling "current century" changes each 393year. 394 395=head2 Limits of time_t 396 397On perl versions older than 5.12.0, the range of dates that can be actually be 398handled depends on the size of C<time_t> (usually a signed integer) on the 399given platform. Currently, this is 32 bits for most systems, yielding an 400approximate range from Dec 1901 to Jan 2038. 401 402Both C<timelocal()> and C<timegm()> croak if given dates outside the supported 403range. 404 405As of version 5.12.0, perl has stopped using the time implementation of the 406operating system it's running on. Instead, it has its own implementation of 407those routines with a safe range of at least +/- 2**52 (about 142 million 408years) 409 410=head2 Ambiguous Local Times (DST) 411 412Because of DST changes, there are many time zones where the same local time 413occurs for two different GMT times on the same day. For example, in the 414"Europe/Paris" time zone, the local time of 2001-10-28 02:30:00 can represent 415either 2001-10-28 00:30:00 GMT, B<or> 2001-10-28 01:30:00 GMT. 416 417When given an ambiguous local time, the timelocal() function will always 418return the epoch for the I<earlier> of the two possible GMT times. 419 420=head2 Non-Existent Local Times (DST) 421 422When a DST change causes a locale clock to skip one hour forward, there will 423be an hour's worth of local times that don't exist. Again, for the 424"Europe/Paris" time zone, the local clock jumped from 2001-03-25 01:59:59 to 4252001-03-25 03:00:00. 426 427If the C<timelocal()> function is given a non-existent local time, it will 428simply return an epoch value for the time one hour later. 429 430=head2 Negative Epoch Values 431 432On perl version 5.12.0 and newer, negative epoch values are fully supported. 433 434On older versions of perl, negative epoch (C<time_t>) values, which are not 435officially supported by the POSIX standards, are known not to work on some 436systems. These include MacOS (pre-OSX) and Win32. 437 438On systems which do support negative epoch values, this module should be able 439to cope with dates before the start of the epoch, down the minimum value of 440time_t for the system. 441 442=head1 IMPLEMENTATION 443 444These routines are quite efficient and yet are always guaranteed to agree with 445C<localtime()> and C<gmtime()>. We manage this by caching the start times of 446any months we've seen before. If we know the start time of the month, we can 447always calculate any time within the month. The start times are calculated 448using a mathematical formula. Unlike other algorithms that do multiple calls 449to C<gmtime()>. 450 451The C<timelocal()> function is implemented using the same cache. We just 452assume that we're translating a GMT time, and then fudge it when we're done 453for the timezone and daylight savings arguments. Note that the timezone is 454evaluated for each date because countries occasionally change their official 455timezones. Assuming that C<localtime()> corrects for these changes, this 456routine will also be correct. 457 458=head1 AUTHORS EMERITUS 459 460This module is based on a Perl 4 library, timelocal.pl, that was 461included with Perl 4.036, and was most likely written by Tom 462Christiansen. 463 464The current version was written by Graham Barr. 465 466=head1 BUGS 467 468The whole scheme for interpreting two-digit years can be considered a bug. 469 470Bugs may be submitted at L<https://github.com/houseabsolute/Time-Local/issues>. 471 472There is a mailing list available for users of this distribution, 473L<mailto:datetime@perl.org>. 474 475I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>. 476 477=head1 SOURCE 478 479The source code repository for Time-Local can be found at L<https://github.com/houseabsolute/Time-Local>. 480 481=head1 AUTHOR 482 483Dave Rolsky <autarch@urth.org> 484 485=head1 CONTRIBUTORS 486 487=for stopwords Florian Ragwitz J. Nick Koston Unknown 488 489=over 4 490 491=item * 492 493Florian Ragwitz <rafl@debian.org> 494 495=item * 496 497J. Nick Koston <nick@cpanel.net> 498 499=item * 500 501Unknown <unknown@example.com> 502 503=back 504 505=head1 COPYRIGHT AND LICENSE 506 507This software is copyright (c) 1997 - 2020 by Graham Barr & Dave Rolsky. 508 509This is free software; you can redistribute it and/or modify it under 510the same terms as the Perl 5 programming language system itself. 511 512The full text of the license can be found in the 513F<LICENSE> file included with this distribution. 514 515=cut 516