Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re^2: Getting seconds from date

by ultranerds (Friar)
on Jun 05, 2011 at 10:21 UTC ( #908180=note: print w/replies, xml ) Need Help??

in reply to Re: Getting seconds from date
in thread Getting seconds from date


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?



Replies are listed 'Best First'.
Re^3: Getting seconds from date
by Perlbotics (Chancellor) on Jun 05, 2011 at 10:56 UTC

    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

      You legend, works a charm :)



Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://908180]
[Discipulus]: are we not yet in Summer?
Discipulus yesteday saw flowered poppins outside..
[marto]: This won't take till Summer to complete I hope ;)
[Discipulus]: i hope too, vacuum cleaner permitting, 2-3 afternoon to build. Or is the 1:1 serie? ;=)

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2017-12-18 21:31 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (498 votes). Check out past polls.