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

I need to convert this Thu Jan 8 07:01:01 into a valid unix timestamp. Any ideas out there?

20040108 Edit by BazB: Changed title from 'convert date'

  • Comment on Parsing a date/time string into a UNIX timestamp

Replies are listed 'Best First'.
Re: Parsing a date/time string into a UNIX timestamp
by hardburn (Abbot) on Jan 08, 2004 at 20:12 UTC

    DateTime::Format::HTTP supports that format (as the "ANSI C asctime()" format). You need to add a four-digit year on the end to be completely accurate, but it can handle it if you don't (according to the documentation).

    use DateTime::Format::HTTP; my $dt = DateTime::Format::HTTP->parse_datetime("Thu Jan 8 07:01:01");

    From there, you can convert to anything else in the DateTime::Format:: namespace.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

Re: Parsing a date/time string into a UNIX timestamp
by TomDLux (Vicar) on Jan 08, 2004 at 20:25 UTC

    perldoc Time::Local

    part of the Core installation

    NAME
           Time::Local - efficiently compute time from local and GMT time
     
    SYNOPSIS
               $time = timelocal($sec,$min,$hour,$mday,$mon,$year);
               $time = timegm($sec,$min,$hour,$mday,$mon,$year);
     
    DESCRIPTION
           These routines are the inverse of built-in perl functions localtime()
           and gmtime().  They accept a date as a six-element array, and return
           the corresponding time(2) value in seconds since the Epoch (Midnight,
           January 1, 1970).  This value can be positive or negative.
           ...
    

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

Re: Parsing a date/time string into a UNIX timestamp
by Paulster2 (Priest) on Jan 08, 2004 at 21:00 UTC

    Check Date::Manip. Converts just about any format of date/time into any other. Also uses common words to do the conversions. The perldoc on this is quite extensive. Take a look.

    Paulster2

Re: Parsing a date/time string into a UNIX timestamp
by borisz (Canon) on Jan 08, 2004 at 20:28 UTC
    This is hard to do, since you give no year! But for the case that the year is there at the end you can use this:
    use Date::Calc qw/:all/; my $str = "Thu Jan 8 07:01:05 2004"; $time = Date_to_Time(Parse_Date($str), $str =~ /(\d\d):(\d\d):(\d\d)/ +);
Re: Parsing a date/time string into a UNIX timestamp
by CountZero (Bishop) on Jan 08, 2004 at 20:38 UTC
    Provided you also provide the year, it is easy:

    use Time::Local; my $timestring='Thu Jan 8 07:01:01'; my (undef,$mon,$mday,$hour,$min, $sec)=split / |:/, $timestring; my $year=2004; my $time = timelocal($sec,$min,$hour,$mday,$mon,$year); print "$timestring = $time = " . scalar(localtime($time));

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: Parsing a date/time string into a UNIX timestamp
by valdez (Monsignor) on Jan 09, 2004 at 10:51 UTC

    I use Date::Parse, it is lightweight and doesn't need a year in the string.

    Ciao, Valerio

Re: Parsing a date/time string into a UNIX timestamp
by jeffa (Bishop) on Jan 09, 2004 at 19:39 UTC

    Time::Piece rocks at this kind of stuff.

    If you don't supply a year, T::P picks 1970:

    use Time::Piece; my $date = localtime->strptime("Thu Jan 8 07:01:01",'%a %b %d %T'); print $date . "\n";

    You can default to the current year (or any year for that matter) like so:

    use Time::Piece; my $year = localtime->year; my $date = localtime->strptime("Thu Jan 8 $year 07:01:01",'%a %b %d %Y + %T'); print $date . "\n";

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Parsing a date/time string into a UNIX timestamp
by Aristotle (Chancellor) on Jan 11, 2004 at 01:01 UTC
    use HTTP::Date; my $stamp = str2time $timestr;

    Makeshifts last the longest.