http://www.perlmonks.org?node_id=521258

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 mysql.pm. I am pulling a row from a MSSQL server which uses this format
Jan 5 2005 11:43AM
or
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 ?
Thanks
  • Comment on Date conversion to epoch w/out mysql.pm

Replies are listed 'Best First'.
Re: Date conversion to epoch w/out mysql.pm
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 mysql.pm
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 mysql.pm
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 --