Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

How to check if a date exists ?

by HJO (Acolyte)
on Oct 18, 2012 at 10:02 UTC ( #999698=perlquestion: print w/ replies, xml ) Need Help??
HJO has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I'm currently having troubles packaging a script which contains the module DateTime::Format::ISO8601, so I'm looking for another way to check if the date I'm giving in the script options does exists...

I'd like to avoid regexp if that's possible ^^" and also the method consisting in extracting the first digits, checking if they are between 1 and 31 for 7 months and 1 and 30 for 4, and 28 or 29 for february, then the 3th and 4th digits to check if they are between 1 and 12, and the year to check if it's a bisextille year...

Thanks in advance

Comment on How to check if a date exists ?
Download Code
Re: How to check if a date exists ?
by james2vegas (Chaplain) on Oct 18, 2012 at 10:23 UTC
    Time::Piece (in core since 5.10) has strptime, which would throw an error on an invalid date, and if you do something like:
    # no 29 feb 2001 my $dt = Time::Piece->strptime("2001-02-29", "%F"); # will return 2001-03-01 print $dt->strftime("%F");

    Time::Piece will correct your date, and if you need to catch this happening, compare the output of strftime on that object to the original date string. Any invalid date will throw an error that you can catch with eval or one of the many exception catching modules.
    my $dt = eval { Time::Piece->strptime("2001-04-32", "%F") } or warn "This is not a valid date ($@).\n";

      Hi,

      First of all thank you for your answer, secondly, you're saying that this module is in the core since 5.10, but I'm using Perl 5.8.8, so I still can use it but I have to install it first right ?

      And finally, could you please "validate" my code ? :

      my $s = '20121018'; my $date; eval { $date = Time::Piece->strptime("$s"); }; if ( $@ ) { die qq{Error : the date doesn't exist or is not like YYYYMMDD\ +n} } else {print qq{date OK\n}} ;

      Regards

        You need a format, like "%Y%m%d" as the second parameter to strptime (I left that out in my earlier code, so my fault).
Re: How to check if a date exists ?
by zentara (Archbishop) on Oct 18, 2012 at 10:38 UTC

      Hi,

      Thank you for pointing out the answer I'm looking for.I already read that page (I personnaly used the method described by explorer), but I don't think I will use these answers because there are some regex in almost every answer and I'd like to avoid them if possible... I'm not quite familiar with them and if I have to reuse them or modify them I wouldn't be at ease...

      But thanks for your answer anyway

Re: How to check if a date exists ?
by johngg (Abbot) on Oct 18, 2012 at 14:00 UTC

    This post may be helpful. It does use simple regular expressions but only to validate the input.

    Cheers,

    JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://999698]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (12)
As of 2014-10-30 12:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (206 votes), past polls