Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: High-speed Date Formatting

by rjt (Deacon)
on Jul 12, 2013 at 00:54 UTC ( #1043845=note: print w/ replies, xml ) Need Help??


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.


Comment on Re^2: High-speed Date Formatting
Select or Download Code
Re^3: High-speed Date Formatting
by BrowserUk (Pope) on Jul 12, 2013 at 01:39 UTC
    No timezones; This much is trivial to fix....

    Indeed, that is sufficiently efficient to make it silly not to include it. Thank you.

    Though It seems silly not to let the regex do its work. I reformulated that as:

    sub str2epoch { my( $d, $m, $y, $H, $M, $S, $tzs, $tzh, $tzm ) = $_[0] =~ m[^.... (\d\d) (...) (\d\d\d\d) (\d\d):(\d\d):(\d\d) ([+-])(\d +\d)(\d\d)] 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 - ($tzs eq '-' ? -1 : 1)*$tzh*3600 + $tzm*60; }
    See my answer for timegm, which does slow things down, but is a bit more robust

    All the others can be handled, but eventually you just end up with the morass that is DateTime which I have no time for :)

    (There is a simple workaround for the 2100 problem, but it wouldn't come to mind as I write that. And the last version I wrote is archived on a CD somewhere.)


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1043845]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2014-09-19 00:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (128 votes), past polls