use strict;
use Date::Calc qw(check_date check_time);
=head1 Date::Calc functions
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.
if (check_time($hour,$min,$sec))
This function returns "true" ("1") if the given three numerical values
+ "$hour", "$min" and "$sec" constitute a valid time (0 <= $hour < 24,
+ 0 <= $min < 60 and 0 <= $sec < 60), and "false" ("0") otherwise.
=cut
# test routine
while (<DATA>){
chomp;
my $ok = check($_) ? "valid" : "NOT valid";
print "$_ is $ok\n";
}
# check valid date time YYYY-MM-DD hh:mm:ss
sub check {
my $dt = shift;
if ($dt =~ m!^(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)$!){
return ( check_date($1,$2,$3) && check_time($4,$5,$6) );
} else {
return 0;
}
}
__DATA__
2001-01-01 12:12:12
2001-02-29 12:12:12
2004-02-29 12:12:12
2001-01-01 25:12:12
2001-01-01 12:61:12
2001-01-01 12:12:61
01-01-2001 12:12:12
poj |