Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Regular expression for date

by didess (Sexton)
on Aug 25, 2008 at 20:47 UTC ( [id://706777]=note: print w/replies, xml ) Need Help??


in reply to Regular expression for date

Hi !
1st : Tell us what you want from this script: I suppose it's : make someone enter a date until what he/she types is a "good" date (in "good" format). right ? If yes, you loop until the entry is OK ? 2nd : the format you show is not clear : It could be : 2008, January the 1st 2001, January the 8th, ... and so on It should be better to write it MM-DD-YY (for month, day, year). (FYI + :I'm french and we display dates as DD/MM/YY) for this format, the regex "could" be: (0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])-\d\d but : it ignores that the maximum number of days depends on the month( +28,29,30,31), it ignores leap years (February 28th or 29 th ?) You should use the function "timelocal()" from the standard module "Time::Local" : try to transform the date you have to validate into a +n "epoch" time (as the function time() returns) : if the function fai +ls, the date is wrong : this script seems to work: use Time::Local timelocal; my ($m,$d,$y); do { my $date; print "date (MM-DD-YY) ? "; $date=<>; chomp($date); ($m,$d,$y)=split(/-/,$date); $m -= 1; $y += 1900; } until(timelocal(0,0,0,$d,$m,$y));
I hope it adresses your need and will help you

Replies are listed 'Best First'.
Re^2: Regular expression for date
by didess (Sexton) on Aug 25, 2008 at 21:11 UTC
    Apologizes : The previous script hanged on bad date, this one runs better (thanks to "eval") !
    use Time::Local timelocal; my ($m,$d,$y,$t); do { my ($date); print "date (MM-DD-YY) ? "; $date=<>; chomp($date); ($m,$d,$y)=split(/-/,$date); $m -= 1; $y += 1900; $t = eval "timelocal(0,0,0,$d,$m,$y)"; } until (!$t);

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-03-19 10:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found