http://qs321.pair.com?node_id=1043845


in reply to Re: High-speed Date Formatting
in thread *SOLVED* High-speed Date Formatting

No timezones;

This much is (sort of) trivial to fix. Add  ([+-]\d{4}) to the end of your regex, and then include - $tz/100*3600 - $tz%100*60 at the end of the expression.

sub str2epoch { my( $d, $m, $y, $H, $M, $S, $tz ) = $_[0] =~ m/^.... (\d\d) (...) (\d\d\d\d) (\d\d):(\d\d):(\d\d) ([+-]\d{4 +})/ or die "Bad format $_[0]"; my $leaps = int( ($y - 1970) / 4 + 0.5 ); (((($y-1970)*365 +$leaps+MONTHS->{$m}+($d-1))*24 +$H)*60 +$M)*60 + +$S - ($tz < 0 ? -1 : 1)*(substr($tz,1,2)*3600 + substr($tz, +3)*60); }
  • No leap seconds;
  • No daylight savings;
  • Only works for another 87 years.

See my answer for timegm, which does slow things down, but is a bit more robust. ++ for pure-Perl that out-performs the "efficient" Time::Local routines and works in most all cases, though!

Edit: Updated $tz calc for fractional hours.