Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

use of date n time functions

by blackgoat (Acolyte)
on Mar 08, 2010 at 06:13 UTC ( [id://827316]=perlquestion: print w/replies, xml ) Need Help??

blackgoat has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!

I have some data stored in text form which appears in the following format:

mon_jan_01_16:00:33_t23.96-d568

A number of such entries are present in my file. I need to pick out(thru regexp)the entry that is most recent. I, however, fail to understand how can the date and time functions be used in this case. Pls help me out...

Thanks!

BG

Replies are listed 'Best First'.
Re: use of date n time functions
by CountZero (Bishop) on Mar 08, 2010 at 07:17 UTC
    Use
    my ($weekday, $month, $day, $hours, $minutes, $seconds, $whatever) = s +plit /[_:]/, 'mon_jan_01_16:00:33_t23.96-d568'
    to get the components of your string (having made a guess at what these components are, of course).

    Then have a look at Time::Local which can transform the above components (hint: the name of the month must still be transformed into its sequence number) of your date-time string into the number of seconds since the epoch. It is then very easy to sort according to that number.

    One difficulty you will have to surmount is the missing year in your date-time string. Perhaps you can assume it is always the present year?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: use of date n time functions
by ZlR (Chaplain) on Mar 08, 2010 at 10:03 UTC
    Or you could parse out the dates with a regex, then use the Date::Calc bazooka to compare theses dates and find the most recent one.

    The advantage of this being that you don't have to take into acount tricky dates pitfalls like leap years and so forth, as Date::Calc does that for you.

      Could you pls give me a sample showing how to use Date::Calc to compare dates??

        It's all in the Date::Calc help page : look at the recipes

        For instance :

        use Date::Calc qw( Delta_Days ); if (Delta_Days($year1,$month1,$day1, $year2,$month2,$day2) > 0)
        There's also examples about : How do I compare two dates with times? , which might be just what you need.
Re: use of date n time functions
by ikegami (Patriarch) on Mar 08, 2010 at 07:18 UTC
    Parse out the date with a regex, pass the dates to Time::Local's timegm, find the biggest of the returned numbers.
Re: use of date n time functions
by thinc (Initiate) on Mar 09, 2010 at 04:49 UTC

    > mon_jan_01_16:00:33_t23.96-d568

    year - unknown (2018,2007,2001,1996? All these years have Jan 1st tha +t falls on a monday) month - January day - 01 hour - 16 minute - 00 second - 33

    what do the "t23.96" and the "d568" represent?

      Pls assume the year to be the current year. I used mon_jan_01 just as an arbitrary example to show format of the data. "t23.96" and "d568" are also just trivial parts of the expression to be matched through regex, pls ignore it!
Re: use of date n time functions
by Anonymous Monk on Mar 08, 2010 at 07:04 UTC
    What is the name of that format?
      I do not know of any name of the format, its just how the data resides.
        I do not know of any name of the format, its just how the data resides.

        Then how are you supposed to make sense of the data?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://827316]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2025-03-26 07:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (67 votes). Check out past polls.

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.