Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: ultimate date check regexp

by davorg (Chancellor)
on Oct 30, 2006 at 09:27 UTC ( [id://581236]=note: print w/replies, xml ) Need Help??


in reply to ultimate date check regexp

A regex that checks values like that is going to be far too complex to maintain. You're better off using a function that checks for valid dates. The timelocal function from Time::Local, converts a list of values (like those returned from localtime) into a epoch seconds value and it dies if passed an invalid date. You can use eval to catch and report on those errors.

#!/usr/bin/perl use strict; use warnings; use Time::Local; while (<DATA>) { chomp; my ($y, $m, $d) = split /-/; eval { timelocal 0, 0, 0, $d, $m-1, $y-1900 }; if ($@) { print "$_ is not a valid date\n"; } else { print "$_ is a valid date\n"; } } __DATA__ 2006-10-30 2006-10-32 2006-02-29 2004-02-29

It's simple enough to wrap that up into a is_valid_date function.

--
<http://dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-25 18:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found