Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Fast(er) date parsing

by Anonymous Monk
on Jul 19, 2000 at 01:19 UTC ( #23065=categorized question: print w/replies, xml ) Need Help??
Contributed by Anonymous Monk on Jul 19, 2000 at 01:19 UTC
Q&A  > dates and times


I need to parse dates that are in the standard format:

    Mon Jul 17 17:59:31 2000

Currently i'm using Date::Parse but this is fairly slow.
Does anyone have a specialized and possibly faster way to do it?
Maybe a combination of split() with table lookup?

Answer: Fast(er) date parsing
contributed by spoon!

I think the POSIX::strptime module is what you're looking for. It has an strptime() function that acts as the inverse of strftime():

use POSIX::strptime; use Time::Local; my $result = timelocal strptime $d, '%a %b %e %H:%M:%S %Y';
Answer: Fast(er) date parsing
contributed by lhoward

Since you know the format and the format is constant then the fastest parser would probably be a split, followed by a month name to month number lookup table. Then take all your numbers and add throw them into an apropriate equation to get the timestamp format and voila.

Answer: Fast(er) date parsing
contributed by fundflow

Thanks guys, maybe i wasn't clear. I want to get the unix timing (since epoch) from the data and was wondering if there is an inverse function to strftime(). I just realized that its not that hard and so here is my solution, following lhoward's suggestion:

use Time::Local; my %months = ( Jan=> 0, Feb=> 1, Mar=> 2, Apr=> 3, May=>4, Jun=> 5, Jul=> 6, Aug=> 7, Sep=> 8, Sep=> 8, Oct=> 9, Nov=> 10, Dec= +> 11); # Parse the date: Fri Jun 30 11:07:39 2000 my ($day, $month, $mday, $time, $year)=split(" ", $d); my ($hour,$min,$sec)=split(":",$time); my $result = timelocal($sec,$min,$hour,$mday,$months{$month},$year);

I'd prefer not having to use any module but this seems reasonable
The reason i need this is to go through some log files and print a summary of the events that happened in the past 24 hours.

Answer: Fast(er) date parsing
contributed by crazyinsomniac

my( $day, $month, $date, $time, $hour, $min, $sec, $year); ($day,$month,$date,$time,$year)=split(" ","Mon Jul 17 17:59:31 2000"); ($hour,$min,$sec)=split(":",$time);
Answer: Fast(er) date parsing
contributed by chanio

See the great Date::Calc that has plenty of functions to do this:

perl -MDate::Calc -e'my $string="8 nov,2005";my ($year,$month,$day)= D +ate::Calc::Decode_Date_EU($string); print("\n$year/$month/$day\n")' 2005/11/8

Please (register and) log in if you wish to add an answer

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (4)
    As of 2020-09-23 23:42 GMT
    Find Nodes?
      Voting Booth?
      If at first I donít succeed, I Ö

      Results (132 votes). Check out past polls.