From 1adb28bc23b11e4c11b97f0524b829e840c70a74 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 20 Apr 2020 10:59:55 +0200 Subject: [PATCH] Bug 25133: Handle 12hr format for dt_from_string Signed-off-by: Nick Clemens Signed-off-by: Kelly McElligott Signed-off-by: Katrin Fischer Signed-off-by: Martin Renvoize Signed-off-by: Joy Nelson --- Koha/DateUtils.pm | 9 +++++++++ circ/circulation.pl | 2 +- t/DateUtils.t | 13 ++++++++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Koha/DateUtils.pm b/Koha/DateUtils.pm index ef89e306cb..3763f3f31c 100644 --- a/Koha/DateUtils.pm +++ b/Koha/DateUtils.pm @@ -139,12 +139,17 @@ sub dt_from_string { : (?\d{2}) )? + ( + \s + (?\w{2}) + )? )? |xms; $regex .= $time_re; $fallback_re .= $time_re; my %dt_params; + my $ampm; if ( $date_string =~ $regex ) { %dt_params = ( year => $+{year}, @@ -154,6 +159,7 @@ sub dt_from_string { minute => $+{minute}, second => $+{second}, ); + $ampm = $+{ampm}; } elsif ( $date_string =~ $fallback_re ) { %dt_params = ( year => $+{year}, @@ -163,6 +169,7 @@ sub dt_from_string { minute => $+{minute}, second => $+{second}, ); + $ampm = $+{ampm}; } else { die "The given date ($date_string) does not match the date format ($date_format)"; @@ -176,6 +183,8 @@ sub dt_from_string { $dt_params{minute} = 00 unless defined $dt_params{minute}; $dt_params{second} = 00 unless defined $dt_params{second}; + $dt_params{hour} += 12 if $ampm && $ampm eq 'PM'; + my $dt = eval { DateTime->new( %dt_params, diff --git a/circ/circulation.pl b/circ/circulation.pl index a07b691607..b7d9782ef1 100755 --- a/circ/circulation.pl +++ b/circ/circulation.pl @@ -178,7 +178,7 @@ for my $barcode ( @$barcodes ) { my $stickyduedate = $query->param('stickyduedate') || $session->param('stickyduedate'); my $duedatespec = $query->param('duedatespec') || $session->param('stickyduedate'); -$duedatespec = eval { output_pref( { dt => dt_from_string( $duedatespec ), dateformat => 'iso', timeformat => '24hr' }); } +$duedatespec = eval { output_pref( { dt => dt_from_string( $duedatespec ), dateformat => 'iso' }); } if ( $duedatespec ); my $restoreduedatespec = $query->param('restoreduedatespec') || $duedatespec || $session->param('stickyduedate'); if ( $restoreduedatespec && $restoreduedatespec eq "highholds_empty" ) { diff --git a/t/DateUtils.t b/t/DateUtils.t index 8a606d6cc6..3c861d6fbe 100755 --- a/t/DateUtils.t +++ b/t/DateUtils.t @@ -4,7 +4,7 @@ use DateTime::TimeZone; use C4::Context; -use Test::More tests => 72; +use Test::More tests => 76; use Test::MockModule; use Test::Warn; @@ -249,6 +249,17 @@ is( output_pref( { dt => $dt, dateonly => 1 } ), '01/01/1900', 'dt_from_string s $dt = dt_from_string('2015-01-31 01:02:03'); is( output_pref( {dt => $dt} ), '31/01/2015 01:02', 'dt_from_string should fallback to sql format' ); +# 12hr format +$dt = dt_from_string('2015-01-31 01:02 AM'); +is( output_pref( {dt => $dt} ), '31/01/2015 01:02', 'dt_from_string ' ); +$dt = dt_from_string('2015-01-31 01:02:03 AM'); +is( output_pref( {dt => $dt} ), '31/01/2015 01:02', 'dt_from_string ' ); +$dt = dt_from_string('2015-01-31 01:02 PM'); +is( output_pref( {dt => $dt} ), '31/01/2015 13:02', 'dt_from_string ' ); +$dt = dt_from_string('2015-01-31 01:02:03 PM'); +is( output_pref( {dt => $dt} ), '31/01/2015 13:02', 'dt_from_string ' ); + + # output_pref with no parameters, single parameter (no hash) is( output_pref(), undef, 'Call output_pref without parameters' ); try { -- 2.39.5