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

I have dates as strings in the following format. Sat Sep 20 17:44:55 2003 and would like to convert to: YYYY_MM_DD_HH_MM_SS or something similar. Any idea if there's a module that already does this?

Replies are listed 'Best First'.
Re: Date conversion
by halley (Prior) on Mar 24, 2004 at 21:17 UTC
    There are probably seven such modules. You can predict the names of such modules picking various name elements from a fuzz table. Search CPAN. http://search.cpan.org/
    Text Parse Time Date Format Date Time :: Date :: Parse Parse Time Format Format Text Text
    I'm only half in jest.

    --
    [ e d @ h a l l e y . c c ]

      Using a date module to do such a straight-forward reformatting?? That's a bit like converting your text from English to German and then back again in order to get rid of your dangling participles just because you have such a module and you know it can be told to not dangle when doing the German-to-English step.

      my %mon; @mon{qw/ jan feb mar apr may jun jul aug sep oct nov dec /}= '01'..'12'; s#\w{3} (\w{3}) (\d+) ([\d:]+) (\d+)#$4-$mon{lc $1}-$2 $3#

      - tye        

Re: Date conversion
by matija (Priest) on Mar 24, 2004 at 21:22 UTC
    There is. It's called Date::Manip. The code would look something like this:
    $date = ParseDate($string); print UnixDate($date,"%Y_%m_%d_%H_%M_%S");
Re: Date conversion
by hardburn (Abbot) on Mar 24, 2004 at 21:17 UTC

    Look at the DateTime::Format heiracracy. There's bound to be something in there that works. If not, DateTime::Format::Builder can probably be used as a fallback.

    ----
    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

      You are correct sir, DateTime::Format::Strptime is the ticket
      use DateTime::Format::Strptime; my $Strp1 = DateTime::Format::Strptime::->new( pattern => '%A %B %d %H:%M:%S %Y', locale => 'en_US', on_error => 'croak', ); #YYYY_MM_DD_HH_MM_SS my $Strp2 = DateTime::Format::Strptime::->new( pattern => '%Y_%m_%d_%H_%M_%S', locale => 'en_US', on_error => 'croak', ); my $dt = 'Sat Sep 20 17:44:55 2003'; warn $dt,$/; die $Strp2->format_datetime($Strp1->parse_datetime( $dt )),$/; __END__ Sat Sep 20 17:44:55 2003 2003_09_20_17_44_55

      MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
      I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
      ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Date conversion
by CountZero (Bishop) on Mar 24, 2004 at 22:34 UTC
    Or without modules:
    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time +); $year += 1900; $mon += 1; print join '_', ($year,$mon,$mday,$hour,$min,$sec); # YYYY_MM_DD_HH_M +M_SS
    Comment: It does not print values less than 10 with a leading zero. If you need that, it can be easily added. It is left as an exercise for the reader.

    Update: Added $mon += 1. Thanks graff

    CountZero

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

      You forgot to add one to $mon -- localtime() returns January as "0".
      He didn't say he had Unix epoch numerical times, or wanted the output of time(). He said he had strings in a given format, and wanted them in a different format.

      --
      [ e d @ h a l l e y . c c ]

        You're right, I jumped to conclusions. Mea culpa

        CountZero

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