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

Hi all, I have one scaler varible called time in YYYYMMDDHHMMSS format(ex:20090527051000). Is there any class or subroutine to validate the date or not? Can you please tell me hoew to do that?? Thanks

Replies are listed 'Best First'.
Re: time validation
by Anonymous Monk on May 19, 2009 at 10:14 UTC
    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
      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/.

      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 ')}, ...
Re: time validation
by targetsmart (Curate) on May 19, 2009 at 10:33 UTC
    Date::Calc provides check_date and check_time try those.

    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
      Those functions take separate parameters for year, month, day, etc. You'd need some glue code to parse the string of digits into those parts. Eg:
      if ($stringToTest =~ m/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/ & +& check_date($1, $2, $3) && check_time($4, $5, $6) ) { print "It's OK\n"; }
      I suspect there's a way to simplify that regular expression - too much repetition!

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

        >perl -wMstrict -le "my $s = '20090527051000'; my @matches = $s =~ m{ \A \d{4} | \d{2} }xmsg; print qq{@matches}; " 2009 05 27 05 10 00
Re: time validation
by bichonfrise74 (Vicar) on May 20, 2009 at 04:15 UTC
    I agree with other posts and would also use regex to validate the datetime in question.