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

I've been loving Time::Format- To turn a unix time stamp into a date.. but.. how do I go the other way around? Turn a date string like '2007/07/17 13:21' into a timestamp value?

Replies are listed 'Best First'.
Re: Turn date string into timestamp?
by almut (Canon) on Jul 17, 2007 at 18:06 UTC

    If convenience is more important than size (Date::Manip is about 240k of Perl code), you could do

    use Date::Manip; my $time = UnixDate('2007/07/17 13:21', "%s"); print "time=$time\n"; # 1184671260
Re: Turn date string into timestamp?
by philcrow (Priest) on Jul 17, 2007 at 18:04 UTC
Re: Turn date string into timestamp?
by shmem (Chancellor) on Jul 17, 2007 at 17:41 UTC
    Well, not for all "like" strings, but for exactly that format, I'd do something like
    use Time::Local; my $str = '2007/07/17 13:21'; my @t = $str =~ m!(\d{4})/(\d{2})/(\d{2})\s(\d{2}):(\d{2})!; $t[1]--; my $timestamp = timelocal 0,@t[4,3,2,1,0]; # verify... print scalar localtime $timestamp; __END__ Tue Jul 17 13:21:00 2007

    before looking up any module of CPAN. Things would be different if I had to convert disparate formats.

    --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}
Re: Turn date string into timestamp?
by andreas1234567 (Vicar) on Jul 17, 2007 at 19:29 UTC
    DateTime is also up for the job.
    use strict; use warnings; use DateTime::Format::Strptime; my $str = '2007/07/17 13:21'; my $parser = DateTime::Format::Strptime->new( pattern => '%Y/%m/%d %H:%M' ); my $dt = $parser->parse_datetime( $str ); print $dt->epoch; __END__ C:\src\perl\perlmonks\627080>perl -w 627080.pl 1184678460
    --
    Andreas
Re: Turn date string into timestamp?
by Anonymous Monk on Jul 18, 2007 at 06:48 UTC
    FYI, both are timestamps
      Really? I've been referring to these as a date string and a unix timestamp.