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 <crc@liblime.com> Signed-off-by: Joshua Ferraro <jmf@liblime.com>
This commit is contained in:
parent
69fee47751
commit
3ebb4ec84f
1 changed files with 24 additions and 0 deletions
24
C4/Dates.pm
24
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;
|
||||
|
|
Loading…
Reference in a new issue