in reply to Simple Date Validation
What does validate mean in this context? Do you want to check whether the submitted date conforms to some format? Or do you want to check whether the submitted date is in some valid range?
It all depends on the format used in $userdate. Is it YYYY-MM-DD ? DD.MM.YY ? MM-DD-YYYY ? Jan 11 05 ? Does it contain time?
Without some restrition on the accepted format, there's no way to tell whether 01.02.03 is valid, and which part of that string means year, month and day respectively.
If you have some pre-defined format, a regular expression would suffice to check whether $userdate complies. Then you know which fields mean what. To convert e.g. Jan 12 2007 to a unix timestamp with Time::Local and validate there's no day or month overflow (e.g. Apr 31 2007):
use Time::Local; my $userdate = "Jan 12 2007"; my @months = qw (Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov + Dec); @monthname {@months} = 0 .. 11; my ($m, $day, $year) = split /\s+/, $userdate; defined $monthname {$m} or die "no valid month\n"; my $month = $monthname {$m}; for ($day, $year) { /^\d+$/ or die "$_ invalid\n" } my $timestamp = eval { local $SIG{__DIE__}; timelocal (0, 0, 0, $day, +$month, $year) }; if ($@) { warn "invalid date: $@\n" } else { my @ltime = localtime ($timestamp); printf "date: %02d %s %04d\n", $ltime [3], $months [$ltime[4]], $l +time [5] + 1900; } print "done\n"; __END__
That properly warns of e.g. "Feb 31 2007" being an invalid date, but reports "29 Feb 2004" as being correct.
--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}
|
|---|