Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Validate date value

by ashokpj (Hermit)
on Oct 04, 2007 at 11:21 UTC ( #642627=perlquestion: print w/ replies, xml ) Need Help??
ashokpj has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I like to validate date value, below scripts will find if day is more the 31 days and month is more than 12. But this scripts incorrectly allow for february to have 31 days

please let me know which module support to validate date value(involved leap years ) and format.

use Time::Local; my ($sec,$min,$hour) = qw( 0 0 12 ); my ($mday, $mon, $year) = qw(31 02 2012); $mon--; # because that's how unix/C treat the month value eval { my $dummy = timelocal($sec,$min,$hour,$mday,$mon,$year); }; print $@; if (my $err = $@) { print "This is an invalid date."; } else { print "Yay"; };

Comment on Validate date value
Download Code
Re: Validate date value
by Punitha (Priest) on Oct 04, 2007 at 11:42 UTC
    Hi ashok,
    This is the another way to validate the date value.Try this
    use strict; use warnings; use Date::Calc qw(check_date); my ($mday, $mon, $year) = qw(28 02 2012); if (check_date($year,$mon,$mday)){ ####Your stuffs here } else{ print "Check the date is not a valid one\n"; }
    Punitha
Re: Validate date value
by apl (Monsignor) on Oct 04, 2007 at 11:43 UTC
    CPAN is your friend.
Re: Validate date value
by svenXY (Deacon) on Oct 04, 2007 at 11:47 UTC
    Hi,
    ... my $dummy; eval { $dummy = timelocal($sec,$min,$hour,$mday,$mon,$year); }; ...
    will output:
    $ perl 642627.pl Day '31' out of range 1..29 at 642627.pl line 13 This is an invalid date.
    You define your $dummy within the eval{}, therefore it's invisible after eval finishes.
    Regards,
    svenXY
Re: Validate date value
by bart (Canon) on Oct 04, 2007 at 11:47 UTC
    You're using timelocal already... good. Now you just have to check, after conversion of $dummy (which is an epoch time in seconds) back to date/time components, with localtime, whether you end up with the same day of the month. If not, it must have wrapped to the next month.
    eval { my $dummy = timelocal($sec,$min,$hour,$mday,$mon,$year); die "Day out of range" unless (localtime($dummy))[3] == $mday; };

    Note that you also got accepted dates for the 31st of all other short months than just February: April, June, September, November. Now they'll all be caught.

Re: Validate date value
by shmem (Canon) on Oct 04, 2007 at 11:57 UTC
    In the Tutorials section, you find Getting started with DateTime scrolling around a little bit. DateTime is on CPAN.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2014-12-28 22:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (183 votes), past polls