| 1 | package Time::Local; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | use Carp (); | ||
| 6 | use Exporter; | ||
| 7 | |||
| 8 | our $VERSION = '1.25'; | ||
| 9 | |||
| 10 | use parent 'Exporter'; | ||
| 11 | |||
| 12 | our @EXPORT = qw( timegm timelocal ); | ||
| 13 | our @EXPORT_OK = qw( timegm_nocheck timelocal_nocheck ); | ||
| 14 | |||
| 15 | my @MonthDays = ( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ); | ||
| 16 | |||
| 17 | # Determine breakpoint for rolling century | ||
| 18 | my $ThisYear = ( localtime() )[5]; | ||
| 19 | my $Breakpoint = ( $ThisYear + 50 ) % 100; | ||
| 20 | my $NextCentury = $ThisYear - $ThisYear % 100; | ||
| 21 | $NextCentury += 100 if $Breakpoint < 50; | ||
| 22 | my $Century = $NextCentury - 100; | ||
| 23 | my $SecOff = 0; | ||
| 24 | |||
| 25 | my ( %Options, %Cheat ); | ||
| 26 | |||
| 27 | use constant SECS_PER_MINUTE => 60; | ||
| 28 | use constant SECS_PER_HOUR => 3600; | ||
| 29 | use constant SECS_PER_DAY => 86400; | ||
| 30 | |||
| 31 | my $MaxDay; | ||
| 32 | if ( $] < 5.012000 ) { | ||
| 33 | require Config; | ||
| 34 | ## no critic (Variables::ProhibitPackageVars) | ||
| 35 | |||
| 36 | my $MaxInt; | ||
| 37 | if ( $^O eq 'MacOS' ) { | ||
| 38 | |||
| 39 | # time_t is unsigned... | ||
| 40 | $MaxInt = ( 1 << ( 8 * $Config::Config{ivsize} ) ) | ||
| 41 | - 1; ## no critic qw(ProhibitPackageVars) | ||
| 42 | } | ||
| 43 | else { | ||
| 44 | $MaxInt | ||
| 45 | = ( ( 1 << ( 8 * $Config::Config{ivsize} - 2 ) ) - 1 ) * 2 | ||
| 46 | + 1; ## no critic qw(ProhibitPackageVars) | ||
| 47 | } | ||
| 48 | |||
| 49 | $MaxDay = int( ( $MaxInt - ( SECS_PER_DAY / 2 ) ) / SECS_PER_DAY ) - 1; | ||
| 50 | } | ||
| 51 | else { | ||
| 52 | # recent localtime()'s limit is the year 2**31 | ||
| 53 | $MaxDay = 365 * ( 2**31 ); | ||
| 54 | } | ||
| 55 | |||
| 56 | # Determine the EPOC day for this machine | ||
| 57 | my $Epoc = 0; | ||
| 58 | if ( $^O eq 'vos' ) { | ||
| 59 | |||
| 60 | # work around posix-977 -- VOS doesn't handle dates in the range | ||
| 61 | # 1970-1980. | ||
| 62 | $Epoc = _daygm( 0, 0, 0, 1, 0, 70, 4, 0 ); | ||
| 63 | } | ||
| 64 | elsif ( $^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 | ||
| 68 | $Epoc = 693901; | ||
| 69 | $SecOff = timelocal( localtime(0) ) - timelocal( gmtime(0) ); | ||
| 70 | $Epoc += _daygm( gmtime(0) ); | ||
| 71 | } | ||
| 72 | else { | ||
| 73 | $Epoc = _daygm( gmtime(0) ); | ||
| 74 | } | ||
| 75 | |||
| 76 | %Cheat = (); # clear the cache as epoc has changed | ||
| 77 | |||
| 78 | sub _daygm { | ||
| 79 | |||
| 80 | # This is written in such a byzantine way in order to avoid | ||
| 81 | # lexical variables and sub calls, for speed | ||
| 82 | return $_[3] + ( | ||
| 83 | $Cheat{ pack( 'ss', @_[ 4, 5 ] ) } ||= do { | ||
| 84 | my $month = ( $_[4] + 10 ) % 12; | ||
| 85 | my $year = $_[5] + 1900 - int( $month / 10 ); | ||
| 86 | |||
| 87 | ( ( 365 * $year ) | ||
| 88 | + int( $year / 4 ) | ||
| 89 | - int( $year / 100 ) | ||
| 90 | + int( $year / 400 ) | ||
| 91 | + int( ( ( $month * 306 ) + 5 ) / 10 ) ) - $Epoc; | ||
| 92 | } | ||
| 93 | ); | ||
| 94 | } | ||
| 95 | |||
| 96 | sub _timegm { | ||
| 97 | my $sec | ||
| 98 | = $SecOff + $_[0] | ||
| 99 | + ( SECS_PER_MINUTE * $_[1] ) | ||
| 100 | + ( SECS_PER_HOUR * $_[2] ); | ||
| 101 | |||
| 102 | return $sec + ( SECS_PER_DAY * &_daygm ); | ||
| 103 | } | ||
| 104 | |||
| 105 | sub timegm { | ||
| 106 | my ( $sec, $min, $hour, $mday, $month, $year ) = @_; | ||
| 107 | |||
| 108 | if ( $year >= 1000 ) { | ||
| 109 | $year -= 1900; | ||
| 110 | } | ||
| 111 | elsif ( $year < 100 and $year >= 0 ) { | ||
| 112 | $year += ( $year > $Breakpoint ) ? $Century : $NextCentury; | ||
| 113 | } | ||
| 114 | |||
| 115 | unless ( $Options{no_range_check} ) { | ||
| 116 | Carp::croak("Month '$month' out of range 0..11") | ||
| 117 | if $month > 11 | ||
| 118 | or $month < 0; | ||
| 119 | |||
| 120 | my $md = $MonthDays[$month]; | ||
| 121 | ++$md | ||
| 122 | if $month == 1 && _is_leap_year( $year + 1900 ); | ||
| 123 | |||
| 124 | Carp::croak("Day '$mday' out of range 1..$md") | ||
| 125 | if $mday > $md or $mday < 1; | ||
| 126 | Carp::croak("Hour '$hour' out of range 0..23") | ||
| 127 | if $hour > 23 or $hour < 0; | ||
| 128 | Carp::croak("Minute '$min' out of range 0..59") | ||
| 129 | if $min > 59 or $min < 0; | ||
| 130 | Carp::croak("Second '$sec' out of range 0..59") | ||
| 131 | if $sec >= 60 or $sec < 0; | ||
| 132 | } | ||
| 133 | |||
| 134 | my $days = _daygm( undef, undef, undef, $mday, $month, $year ); | ||
| 135 | |||
| 136 | unless ( $Options{no_range_check} or abs($days) < $MaxDay ) { | ||
| 137 | my $msg = q{}; | ||
| 138 | $msg .= "Day too big - $days > $MaxDay\n" if $days > $MaxDay; | ||
| 139 | |||
| 140 | $year += 1900; | ||
| 141 | $msg | ||
| 142 | .= "Cannot handle date ($sec, $min, $hour, $mday, $month, $year)"; | ||
| 143 | |||
| 144 | Carp::croak($msg); | ||
| 145 | } | ||
| 146 | |||
| 147 | return | ||
| 148 | $sec + $SecOff | ||
| 149 | + ( SECS_PER_MINUTE * $min ) | ||
| 150 | + ( SECS_PER_HOUR * $hour ) | ||
| 151 | + ( SECS_PER_DAY * $days ); | ||
| 152 | } | ||
| 153 | |||
| 154 | sub _is_leap_year { | ||
| 155 | return 0 if $_[0] % 4; | ||
| 156 | return 1 if $_[0] % 100; | ||
| 157 | return 0 if $_[0] % 400; | ||
| 158 | |||
| 159 | return 1; | ||
| 160 | } | ||
| 161 | |||
| 162 | sub timegm_nocheck { | ||
| 163 | local $Options{no_range_check} = 1; | ||
| 164 | return &timegm; | ||
| 165 | } | ||
| 166 | |||
| 167 | sub timelocal { | ||
| 168 | my $ref_t = &timegm; | ||
| 169 | my $loc_for_ref_t = _timegm( localtime($ref_t) ); | ||
| 170 | |||
| 171 | my $zone_off = $loc_for_ref_t - $ref_t | ||
| 172 | or return $loc_for_ref_t; | ||
| 173 | |||
| 174 | # Adjust for timezone | ||
| 175 | my $loc_t = $ref_t - $zone_off; | ||
| 176 | |||
| 177 | # Are we close to a DST change or are we done | ||
| 178 | my $dst_off = $ref_t - _timegm( localtime($loc_t) ); | ||
| 179 | |||
| 180 | # If this evaluates to true, it means that the value in $loc_t is | ||
| 181 | # the _second_ hour after a DST change where the local time moves | ||
| 182 | # backward. | ||
| 183 | if ( | ||
| 184 | !$dst_off | ||
| 185 | && ( ( $ref_t - SECS_PER_HOUR ) | ||
| 186 | - _timegm( localtime( $loc_t - SECS_PER_HOUR ) ) < 0 ) | ||
| 187 | ) { | ||
| 188 | return $loc_t - SECS_PER_HOUR; | ||
| 189 | } | ||
| 190 | |||
| 191 | # Adjust for DST change | ||
| 192 | $loc_t += $dst_off; | ||
| 193 | |||
| 194 | return $loc_t if $dst_off > 0; | ||
| 195 | |||
| 196 | # If the original date was a non-extent gap in a forward DST jump, | ||
| 197 | # we should now have the wrong answer - undo the DST adjustment | ||
| 198 | my ( $s, $m, $h ) = localtime($loc_t); | ||
| 199 | $loc_t -= $dst_off if $s != $_[0] || $m != $_[1] || $h != $_[2]; | ||
| 200 | |||
| 201 | return $loc_t; | ||
| 202 | } | ||
| 203 | |||
| 204 | sub timelocal_nocheck { | ||
| 205 | local $Options{no_range_check} = 1; | ||
| 206 | return &timelocal; | ||
| 207 | } | ||
| 208 | |||
| 209 | 1; | ||
| 210 | |||
| 211 | # ABSTRACT: Efficiently compute time from local and GMT time | ||
| 212 | |||
| 213 | __END__ |