From 3ebb4ec84f517d315177bebca15d6eee445016c8 Mon Sep 17 00:00:00 2001 From: Galen Charlton Date: Thu, 20 Dec 2007 15:17:25 -0600 Subject: [PATCH] C4::Dates - carp if passed semantically invalid date When creating a new C4::Dates object, now carps if the supplied date and/or time is semantically invalid, i.e., 2007-01-32 will pass the regex check but is not a valid date (there being no 32nd of January). As the C4::Dates API is nailed down, these may be converted to croaks in the future. Signed-off-by: Chris Cormack Signed-off-by: Joshua Ferraro --- C4/Dates.pm | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/C4/Dates.pm b/C4/Dates.pm index b33b0a0d1d..353a8c0e79 100644 --- a/C4/Dates.pm +++ b/C4/Dates.pm @@ -20,6 +20,7 @@ use Carp; use C4::Context; use Exporter; use POSIX qw(strftime); +use Date::Calc qw(check_date check_time); use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); $VERSION = 0.03; @@ -70,6 +71,7 @@ sub dmy_map ($$) { $debug and print STDERR "xsub: $xsub \n"; if ($val =~ /$re/) { my $aref = eval $xsub; + _check_date_and_time($aref); return @{$aref}; } # $debug and @@ -77,6 +79,28 @@ sub dmy_map ($$) { return 0; } +sub _check_date_and_time { + my $chron_ref = shift; + my ($year, $month, $day) = _chron_to_ymd($chron_ref); + unless (check_date($year, $month, $day)) { + carp "Illegal date specified (year = $year, month = $month, day = $day)\n"; + } + my ($hour, $minute, $second) = _chron_to_hms($chron_ref); + unless (check_time($hour, $minute, $second)) { + carp "Illegal time specified (hour = $hour, minute = $minute, second = $second)\n"; + } +} + +sub _chron_to_ymd { + my $chron_ref = shift; + return ($chron_ref->[5] + 1900, $chron_ref->[4] + 1, $chron_ref->[3]); +} + +sub _chron_to_hms { + my $chron_ref = shift; + return ($chron_ref->[2], $chron_ref->[1], $chron_ref->[0]); +} + sub new { my $this = shift; my $class = ref($this) || $this; -- 2.39.5