Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

date validation module

by arcnon (Monk)
on Jan 30, 2006 at 18:13 UTC ( [id://526509]=perlquestion: print w/replies, xml ) Need Help??

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

I am looking for a module that validates a date taking into account leap years and what not and returns true or false.
Anyone know of a module like this?

ex.
1-32-2006 0,--bad day range
2-29-2006 0,--not a leap year

Replies are listed 'Best First'.
Re: date validation module
by bobf (Monsignor) on Jan 30, 2006 at 18:22 UTC

    Date::Calc has a check_date function. From the docs:

    if (check_date($year,$month,$day))
    This function returns ``true'' (``1'') if the given three numerical values ``$year'', ``$month'' and ``$day'' constitute a valid date, and ``false'' (``0'') otherwise.

    For example:

    use strict; use warnings; use Date::Calc qw( check_date ); my ( $year, $month, $day ) = ( 2006, 1, 30 ); printf "[$year, $month, $day] %s a valid date\n", check_date( $year, $month, $day ) ? 'is' : 'is not'; ( $year, $month, $day ) = ( 2006, 1, 32 ); printf "[$year, $month, $day] %s a valid date\n", check_date( $year, $month, $day ) ? 'is' : 'is not'; ( $year, $month, $day ) = ( 2006, 2, 29 ); printf "[$year, $month, $day] %s a valid date\n", check_date( $year, $month, $day ) ? 'is' : 'is not';
    This prints:
    [2006, 1, 30] is a valid date [2006, 1, 32] is not a valid date [2006, 2, 29] is not a valid date

Re: date validation module
by Roy Johnson (Monsignor) on Jan 30, 2006 at 18:21 UTC
    See Date::Calc's Decode_Date_US function.

    Caution: Contents may have been coded under pressure.
Re: date validation module
by ysth (Canon) on Jan 30, 2006 at 18:56 UTC
    Any of the popular date modules allow this, AFAIK. Pick one of them and learn how it works. (DateTime, Date::Calc, Date::Manip, etc.)

    To do it with only core modules, try something like (untested):

    use Time::Local "timegm"; sub bad_date { my ($y,$m,$d) = @_; ! defined eval { timegm(0,0,0,$d,$m-1,$y) } }
    (update: added missing defined)
Re: date validation module
by ikegami (Patriarch) on Jan 30, 2006 at 18:47 UTC
    You can also use core module Time::Local
    use Time::Local qw( timelocal ); my $date_str = '2-31-2006'; my ($m, $d, $y) = $date_str =~ /^(\d+)-(\d+)-(\d+)$/ or die("Invalid input format. Use day-month-year.\n"); my $time = eval { timelocal(0, 0, 0, $d, $m-1, $y) } or die("Invalid date: $@\n"); # For Time::Local < 1.04. Perl >= 5.8.0 doesn't need this. (localtime($time))[3] == $d or die("Invalid date\n");

    Update: After doing some testing, I've found that Time::Local's checking is pretty dumb (Update2:) in version < 1.04. Specifically, it assumes every month has 31 days. I added a statment — the last one — to the snippet in this node to fix the problem.

    As a function:

    use Time::Local qw( timelocal ); sub is_valid_date { my ($date_str) = @_; my ($m, $d, $y) = $date_str =~ /^(\d+)-(\d+)-(\d+)$/ or return 0; my $time = eval { timelocal(0, 0, 0, $d, $m-1, $y) } or return 0; # For Time::Local < 1.04. Perl >= 5.8.0 doesn't need this. (localtime($time))[3] == $d or return 0; return 1; } print(is_valid_date('1-31-2006') ? 'valid' : 'invalid', "\n"); print(is_valid_date('1-32-2006') ? 'valid' : 'invalid', "\n"); print(is_valid_date('2-29-2006') ? 'valid' : 'invalid', "\n");
      Update: After doing some testing, I've found that Time::Local's checking is pretty dumb. Specifically, it assumes every month has 31 days. I added a statment — the last one — to the snippet in this node to fix the problem.
      This appears to be fixed in the Time::Local included with 5.8.0 and later.
Re: date validation module
by vennirajan (Friar) on Jan 31, 2006 at 05:09 UTC
    In addition to the above modules, you also have a look at the module, DateTime::Precise It also contains a lot of functions to handle the dates.

    Regards,
    S.Venni Rajan.
    "A Flair For Excellence."
                    -- BK Systems.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-03-28 18:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found