in reply to Comedy of StdErrs
I couldn't get your code and Time::Local to invalidate Feb. 31, 2003 08:40 by passing a ref to ('023103','08:40') so I whipped this up. As you can see, I choose to use die statements as opposed to the print and exit combo.
use Date::Calc qw(check_date check_time); our $DEBUG = 1; # set to 0 to disable print "woohoo!\n" if valid( ['033103','12:59'] ); # March 31, 2003 12: +59 valid print "w00t!\n" if valid( ['022803','13:00'] ); # February 28, 20 +03 13:00 valid print "Yippee!\n" if valid( ['023103','08:40'] ); # February 31, 2003 +8:40 invalid sub valid { my $ref = shift; my $date = shift @$ref; my $time = shift @$ref; die "Invalid date format $date" unless $date =~ /(\d\d)(\d\d)(\d\d +)/; my ($month, $day, $year) = ($1, $2, $3); $year += ($year <= 69) ? 2000 : 1900; die "Invalid date $date" unless (check_date($year,$month,$day)); die "Invalid time format $time" unless $time =~ /(\d\d):(\d\d)/; my ($hour, $minute) = ($1, $2); die "Invalid time $time" unless (check_time($hour,$min,$sec)); print "$date $time is valid.\n" if $DEBUG; return 1; }
update: fixed $year update code
|
|---|