From 170508e7a0fb8687da196d9f7d3c81e104b64aba Mon Sep 17 00:00:00 2001 From: Colin Campbell Date: Wed, 15 Jun 2011 13:44:06 +0100 Subject: [PATCH] Bug 5549 : DateUtils handling odd cases Really we should not have them but they are in existing code. They were not handled 100% added appropriate tests --- Koha/DateUtils.pm | 5 +++-- t/DateUtils.t | 37 ++++++++++++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/Koha/DateUtils.pm b/Koha/DateUtils.pm index 1e7d19819d..7f4937ed34 100644 --- a/Koha/DateUtils.pm +++ b/Koha/DateUtils.pm @@ -69,11 +69,12 @@ sub dt_from_string { } else { if ( $date_format eq 'iso' ) { $date_string =~ s/-00/-01/; - if ( $date_string =~ m/^0000-00/ ) { + if ( $date_string =~ m/^0000-0/ ) { return; # invalid date in db } } elsif ( $date_format eq 'us' ) { - $date_string =~ s[-00-][-01-]; + $date_string =~ s#-#/#g; + $date_string =~ s[/00/][/01/]; } elsif ( $date_format eq 'sql' ) { $date_string =~ s/(\d{4})(\d{2})(\d{2})\s+(\d{2})(\d{2})(\d{2})/$1-$2-$3T$4:$5:$6/; diff --git a/t/DateUtils.t b/t/DateUtils.t index f922d52eb9..aa0dfcc0ea 100755 --- a/t/DateUtils.t +++ b/t/DateUtils.t @@ -5,7 +5,7 @@ use DateTime; use DateTime::TimeZone; use C4::Context; -use Test::More tests => 9; # last test to print +use Test::More tests => 19; # last test to print BEGIN { use_ok('Koha::DateUtils'); } @@ -33,10 +33,37 @@ cmp_ok $date_string, 'eq', '06/16/2011 12:00', 'us output'; $date_string = output_pref( $dt, 'metric' ); cmp_ok $date_string, 'eq', '16/06/2011 12:00', 'metric output'; -my $dear_dirty_dublin = DateTime::TimeZone->new( name => 'Europe/Dublin'); -my $new_dt = dt_from_string('16/06/2011', 'metric', $dear_dirty_dublin); - +my $dear_dirty_dublin = DateTime::TimeZone->new( name => 'Europe/Dublin' ); +my $new_dt = dt_from_string( '16/06/2011', 'metric', $dear_dirty_dublin ); isa_ok( $new_dt, 'DateTime', 'Create DateTime with different timezone' ); +cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso, + 'Returned Dublin object matches input' ); + +$new_dt = dt_from_string( '2011-06-16 12:00', 'sql' ); +isa_ok( $new_dt, 'DateTime', 'Create DateTime from (mysql) sql' ); +cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso, 'sql returns correct date' ); + +$new_dt = dt_from_string( $dt, 'iso' ); +isa_ok( $new_dt, 'DateTime', 'Passed a DateTime dt_from_string returns it' ); + +# C4::Dates allowed 00th of the month + +my $ymd = '2012-01-01'; +my $dt0 = dt_from_string( '00/01/2012', 'metric' ); +isa_ok( $dt0, 'DateTime', + 'dt_from_string returns a DateTime object passed a zero metric day' ); +cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects metric day 0' ); + +$dt0 = dt_from_string( '01/00/2012', 'us' ); +isa_ok( $dt0, 'DateTime', + 'dt_from_string returns a DateTime object passed a zero us day' ); +cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects us day 0' ); -cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso, 'Returned Dublin object matches input' ); +$dt0 = dt_from_string( '2012-01-00', 'iso' ); +isa_ok( $dt0, 'DateTime', + 'dt_from_string returns a DateTime object passed a zero iso day' ); +cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects iso day 0' ); +# Return undef if passed mysql 0 dates +$dt0 = dt_from_string( '0000-00-00', 'iso' ); +is( $dt0, undef, "undefined returned for 0 iso date" ); -- 2.39.5