Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Getting seconds from date

by ultranerds (Pilgrim)
on Jun 05, 2011 at 09:55 UTC ( #908178=perlquestion: print w/ replies, xml ) Need Help??
ultranerds has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm trying to convert a date like so:

2011-06-05 11:00:00

..into the number of seconds.

To get the number of seconds from the current date, I use:

my $now_in_secs     = Time::Local::timelocal(localtime);

...but trying to use that with the other format doesn't work (as it doesn't like the format of the date)

Can anyone suggest a good/effecient way to do this?

TIA!

Andy

Comment on Getting seconds from date
Download Code
Re: Getting seconds from date
by Corion (Pope) on Jun 05, 2011 at 10:16 UTC

    Have you tried converting the date string you get into the format that localtime returns? It's not really hard, you just have to remember that the localtime API is broken and counts months from 0.

      Hi,

      Thanks for the reply. I tried converting it to the localtime format, but unfortunatly didn't have much success. Do you have a module in mind to do that?

      TIA

      Andy

        Dealing with dates and times is quite tricky. Have a look at the module suggestions given in the dates and times section, e.g. Fast(er) date parsing.

        However, if you insist on using Time::Local, you can use this snippet to get started...

        use strict; use warnings; use Time::Local qw(timelocal); sub localdate2epoch { my $date = shift; # insert some sanity checks and normalisation (whitespace removal, e +tc.) my ($year, $mon, $mday, $hour, $min, $sec) = split /[-\s:]+/, $date +; # insert more sanity checks # .oO( get the impression here, it's better to use a module ;-) $year -= 1900; # correct year (2011 = 1900 + 111); $mon -= 1; # correct month (0..11) return timelocal( $sec, $min, $hour, $mday, $mon, $year ); } my $date = "2011-06-05 11:00:00"; my $now_in_secs = localdate2epoch $date; print "In : $date\n"; print "epo: $now_in_secs\n"; print "Out: ", scalar localtime $now_in_secs, "\n"; __END__ In : 2011-06-05 11:00:00 epo: 1307264400 Out: Sun Jun 5 11:00:00 2011

      I think "broken" is too harsh a word. I see why this was done, so that this number can be used an index into an array of text translation values. A lot of stuff in the C library is truly ancient and predates C. Nowadays, I would imagine that location "0" would just get "burned" and not used so that the indicies have the normal human meaning. But an ancient decision made another choice.
      my @monthText = qw (jan feb mar apr may jun jul aug sep oct nov dec); print "$monthText[0] is for January\n"; print "$monthText[11] is for December\n";
Re: Getting seconds from date
by bart (Canon) on Jun 05, 2011 at 11:02 UTC
    timelocal expects a bunch of input parameters, one per date component. It's often rather easy to parse the date yourself, if it has a predefined format, and pull out individual parts. All parameters are numbers, so if you have to parse a month name, you can convert it to a month number using a small Perl data structure. If you parse a month number, be wary that timelocal expects the month number for January is 0. So you may have to subtract 1 to get the correct value.

    It's all not too much work, you can usually make it work within a couple of minutes, but if it proves to be too hard for you, you can always fall back to parsing the date using one of the (rather heavy-weight) date parsing modules, that likely can convert it to epoch seconds for you, as well.

Re: Getting seconds from date
by Not_a_Number (Parson) on Jun 05, 2011 at 16:55 UTC

    ...and just to show you how easy it can be using a module (and the time() function):

    use Date::Parse; print time - str2time( "2011-06-05 11:00:00" );

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (12)
As of 2014-08-28 14:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (262 votes), past polls