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

Hi,

I'm trying to convert a date and time in the format '2006-06-26 11:24:56' into seconds. I have the following subroutine that already handles the date, but not the time. Can anyone help me add to this code to incorporate the time?
Thanks!
Sandy

Code:
use strict; use Time::localtime; ########################################################## # date_to_seconds subroutine sub date_to_seconds{ my $date = shift(@_); my ($year, $month, $day) = split(/-/, $date); $month--; return timelocal(0,0,0, $day, $month, $year); } ##########################################################

Replies are listed 'Best First'.
Re: Converting date and time into seconds
by Limbic~Region (Chancellor) on Jun 26, 2006 at 15:39 UTC
    sandycat05,
    I am not familiar with Time::localtime. If you used Time::Local, you would find everything you are looking for. Parsing using split or a regex is asking for trouble unless you are certain your data is in a rigid format. Otherwise, there are plenty of other date parsing modules on CPAN that also provide conversion to epoch.
    # untested sub stamp2epoch { my $stamp = shift @_; my ($yr, $mon, $day, $hr, $min, $sec) = split /[ :-]+/, $stamp; --$mon; return timelocal($sec, $min, $hr, $day, $mon, $yr); }
    Update: This post went through a series of rapid updates (code example, warning on using split in addition to regex, disclosure of ignorance with Time::localtime, and this update itself. Blame it on being Monday morning.

    Cheers - L~R

Re: Converting date and time into seconds
by Tanktalus (Canon) on Jun 26, 2006 at 16:06 UTC

    As I recall, that is exactly the format handled by Date::Parse. Or, rather, one of the precise formats handled by that module.

    There are so many modules already written to handle this that I'm sure you can find one that you like. Just search http://search.cpan.org for Date, and I'm sure you'll find dozens.

Re: Converting date and time into seconds
by davorg (Chancellor) on Jun 26, 2006 at 15:42 UTC

    What are you having trouble with? The code that you already have is so close that it's hard to see how you'd have problems extending it to take account of the time as well. You just need to adjust your call to "split" so it also splits out the components of the time, and then include those values in the call to "timelocal".

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Converting date and time into seconds
by shmem (Chancellor) on Jun 26, 2006 at 16:02 UTC
    I would recommend the CPAN module... ah, well. Anyways, have a look at DateTime.

    If you are too exhausted to follow the advice of the previous post and do it yourself - click on the link below.

    sub date_to_seconds{ my ($date, $time) = split(/\s/,shift(@_)); my ($year, $month, $day) = split(/-/, $date); my ($h, $m, $s) = split(/:/, $time); $month--; return timelocal($s, $m, $h, $day, $month, $year); }

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}