Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Date conversion to epoch w/out

by Anonymous Monk
on Jan 05, 2006 at 16:33 UTC ( #521258=perlquestion: print w/replies, xml ) Need Help??

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

I found a util but it seems to require a I am pulling a row from a MSSQL server which uses this format
Jan 5 2005 11:43AM
Jan 11 2005 11:14AM
it doesn't use a zero in front of a single digit date
and I need to convert that into epoch, so I can compare it with current time, ie determine how many seconds old that timestamp is.
is there a way to do it without special pm's ?
  • Comment on Date conversion to epoch w/out

Replies are listed 'Best First'.
Re: Date conversion to epoch w/out
by ptum (Priest) on Jan 05, 2006 at 16:51 UTC

    It seems to me that this is something that date functions in SQL could do for you. You could either use DATEDIFF with 1/1/1970 (and multiply by seconds per day) to come up with 'seconds since epoch' or you could use DATEPART to dismember the date and calculate 'seconds since epoch' yourself. Either way, if you're already selecting a row from a MSSQL database, it shouldn't require any extra modules. I'm not a MSSQL guy, but this might help you.

    No good deed goes unpunished. -- (attributed to) Oscar Wilde
      DateDIFF in seconds can be done in the sql on mssql. Thanks !
Re: Date conversion to epoch w/out
by suaveant (Parson) on Jan 05, 2006 at 16:50 UTC
    If you aren't averse to parsing the date yourself and converting it into numbers Time::Local will fix you right up... otherwise there are some Date modules out there that I believe can parse the date and then you can do conversions.

    Pretty easy to parse that, however and turn Jan into 00 and PM into +12...

    Update: Time::Local is a module, but I think it is in the Perl core.

                    - Ant
                    - Some of my best work - (1 2 3)

      suaveant, yes Time::Local is indeed in the Perl core.

      -- vek --
Re: Date conversion to epoch w/out
by vek (Prior) on Jan 06, 2006 at 16:18 UTC

    suaveant's reply has you covered, Time::Local would be my choice if you cannot install Date::* modules from the CPAN.

    use Time::Local; my $epoch = timelocal($sec, $min, $hours, $mday, $mon, $year);

    -- vek --

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://521258]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2022-05-22 19:20 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (81 votes). Check out past polls.