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}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.