in reply to time validation

DateTime::Format::Strptime
#!/usr/bin/perl -- use strict; use warnings; use DateTime::Format::Strptime; # YYYYMMDDHHMMSS format(ex:20090527051000). Is # 2009 05 27 05 10 00 my $strp = DateTime::Format::Strptime->new( pattern => '%Y%m%d%H%M%S', on_error => 'croak' ); my $date = $strp->parse_datetime("20090527051000"); print "$date\n"; # this suprisingly works, must be a bug $date = $strp->parse_datetime("200905270510XX"); print "$date\n"; # this fails as expected, $date = $strp->parse_datetime("20090527051099"); print "$date\n"; __END__ 2009-05-27T05:10:00 2009-05-27T05:01:00 99 is too large to be a second. at - line 22

Replies are listed 'Best First'.
Re^2: time validation
by wol (Hermit) on May 19, 2009 at 10:53 UTC
    Shame that there's a bug in the module :-(

    So, to work around that bug, you could parse the time as above, then format it back into a string in the original format, and do a string comparison. If it's completely valid, then the round trip will get you back to exactly where you started.

    Also, if you want to avoid your code dying if the date's not valid, then just remove that option from the creation of the object - the default behaviour is to return undef if it's not parsable, which is almost certainly more useful in this case.

    --
    use JAPH;
    print JAPH::asString();

      then format it back into a string in the original format, and do a string comparison.

      Or fix the bug ;) Or since the format is all numbers , easy to screen /\A\d+\z/.

        Don't forget that the string could be the wrong length, so m/^\d{14}$/

        Or fix the bug :-)

        --
        use JAPH;
        print JAPH::asString();

Re^2: time validation
by Anonymous Monk on May 19, 2009 at 10:20 UTC
    There is a bug in Date::Format::Strptime's Makefile.PL
    makefile(824) : fatal error U1036: syntax error : too many names to le +ft of '=' Stop.
    One way to fix is to delete/rename sub MY::postamble. To replicate that part of makefile, add
    WriteMakefile( test => {TESTS => ($^O eq 'MSWin32'? 't/*.t t/more/*.t' : 't/*.t ')}, ...