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

hi Monks,

I know that there are modules that can generate a date string in the format YYYYMMDDHH, but is there one that can actually parse such string into variables, something like?:
my ($year, $month, $day, $hour) = Foo::bar('2008032017');

thank you!

Replies are listed 'Best First'.
Re: Parse YYYYMMDDHH string
by vlademonkey (Pilgrim) on Mar 20, 2008 at 22:17 UTC
    A simple unpack would do the trick:
    my ($year, $month, $day, $hour) = unpack("a4a2a2a2", '2008032017');
      Cool! Thanks for both suggestions.
Re: Parse YYYYMMDDHH string
by FunkyMonk (Bishop) on Mar 20, 2008 at 22:05 UTC
    It doesn't really need a module - it's a simple regex:
    my $date = '2008032017'; my ($year, $month, $day, $hour) = $date =~ m{(\d{4})(\d\d)(\d\d)(\d\d)}; print "$year-$month-$day $hour\n"; #Output: 2008-03-20 17

Re: Parse YYYYMMDDHH string
by Fletch (Bishop) on Mar 20, 2008 at 22:03 UTC

    Maybe.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Parse YYYYMMDDHH string
by parv (Parson) on Mar 21, 2008 at 04:02 UTC

    Date::Parse, Date::Calc, Date::Manip should be able to parse a date with varying degrees of limitations. Try also Date::ICal.

    I have started to loath Time::Piece, which can also parse the date in OP's format, as it is limited to dates upto 2038-01-16T23:59:59. (That is the end point I could find with perl 5.8.7 built for i686-linux-thread-multi.)

    Started to loath -- rant follows -- for it is currently used at my work where ...

    1. it produced incorrect time zone on some of the machines (the offending part was replaced by POSIX::strftime);
    2. in unrelated code not using Time::Piece itself, there was problem with dates in or after Jan 2038. Workaround was to set the limit to date in a year before 2038.
    ... given that above issues are known, I would rather be working on replacing Time::Piece usage instead of working with it.