From e13901e6846b1e2f1b50ab33b8b3f6eccb73a43c Mon Sep 17 00:00:00 2001 From: Lari Taskula Date: Fri, 24 Mar 2017 16:29:46 +0200 Subject: [PATCH] Bug 18330: Add RFC3339 to Koha::DateUtils To test: 1. prove t/DateUtils.t Signed-off-by: Benjamin Rokseth Signed-off-by: Tomas Cohen Arazi --- Koha/DateUtils.pm | 20 ++++++++++++++++++++ t/DateUtils.t | 12 +++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/Koha/DateUtils.pm b/Koha/DateUtils.pm index af5ce7ebbd..2b8ee8b5a2 100644 --- a/Koha/DateUtils.pm +++ b/Koha/DateUtils.pm @@ -104,6 +104,22 @@ sub dt_from_string { (?\d{4}) |xms; } + elsif ( $date_format eq 'rfc3339' ) { + $regex = qr/ + (?\d{4}) + - + (?\d{2}) + - + (?\d{2}) + ([Tt\s]) + (?\d{2}) + : + (?\d{2}) + : + (?\d{2}) + (([Zz])|([\+|\-]([01][0-9]|2[0-3]):[0-5][0-9])) + /xms; + } elsif ( $date_format eq 'iso' or $date_format eq 'sql' ) { # iso or sql format are yyyy-dd-mm[ hh:mm:ss]" $regex = $fallback_re; @@ -237,6 +253,10 @@ sub output_pref { ? $dt->strftime("%Y-%m-%d") : $dt->strftime("%Y-%m-%d $time"); } + elsif ( $pref =~ m/^rfc3339/ ) { + $date = $dt->strftime('%FT%T%z'); + substr($date, -2, 0, ':'); # timezone "HHmm" => "HH:mm" + } elsif ( $pref =~ m/^metric/ ) { $date = $dateonly ? $dt->strftime("%d/%m/%Y") diff --git a/t/DateUtils.t b/t/DateUtils.t index 884990b274..b0c8b054ec 100755 --- a/t/DateUtils.t +++ b/t/DateUtils.t @@ -4,7 +4,7 @@ use DateTime::TimeZone; use C4::Context; -use Test::More tests => 63; +use Test::More tests => 67; use Test::MockModule; use Test::Warn; @@ -46,6 +46,9 @@ cmp_ok $date_string, 'eq', '2011-06-16 12:00', 'iso output'; $date_string = output_pref({ dt => $dt, dateformat => 'iso', timeformat => '12hr' }); cmp_ok $date_string, 'eq', '2011-06-16 12:00 PM', 'iso output 12hr'; +$date_string = output_pref({ dt => $dt, dateformat => 'rfc3339' }); +like($date_string, qr/2011-06-16T12:00:00\+|-\d\d:\d\d/, 'RFC3339 output'); + # "notime" doesn't actually mean anything in this context, but we # can't pass undef or output_pref will try to access the database $date_string = output_pref({ dt => $dt, dateformat => 'iso', timeformat => 'notime', dateonly => 1 }); @@ -114,6 +117,11 @@ 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' ); +$dt0 = dt_from_string( '2012-01-00T12:00:00Z', 'rfc3339' ); +isa_ok( $dt0, 'DateTime', + 'dt_from_string returns a DateTime object passed a zero rfc3339 day' ); +cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects rfc3339 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" ); @@ -198,6 +206,8 @@ $dt = eval { dt_from_string( '31/01/2015', 'iso' ); }; is( ref($dt), '', '31/01/2015 is not a correct date in iso format' ); $dt = eval { dt_from_string( '01/01/2015', 'iso' ); }; is( ref($dt), '', '01/01/2015 is not a correct date in iso format' ); +$dt = eval { dt_from_string( '01/01/2015', 'rfc3339' ); }; +is( ref($dt), '', '01/01/2015 is not a correct date in rfc3339 format' ); $dt = eval { dt_from_string( '31/01/2015', 'us' ); }; is( ref($dt), '', '31/01/2015 is not a correct date in us format' ); $dt = dt_from_string( '01/01/2015', 'us' ); -- 2.15.1