C4::Dates - carp if passed semantically invalid date
authorGalen Charlton <galen.charlton@liblime.com>
Thu, 20 Dec 2007 21:17:25 +0000 (15:17 -0600)
committerJoshua Ferraro <jmf@liblime.com>
Fri, 21 Dec 2007 01:16:37 +0000 (19:16 -0600)
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 <crc@liblime.com>
Signed-off-by: Joshua Ferraro <jmf@liblime.com>
C4/Dates.pm

index b33b0a0..353a8c0 100644 (file)
@@ -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;