From ea3e665b8cf203ef20377f645284b879cd197faf Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 28 May 2020 16:12:57 +0200 Subject: [PATCH] Bug 25617: (bug 25133 follow-up) 12 PM is not 24 but 0 There was an error in the precedent code, in 12hr format, 12PM is actually 00:00 --- Koha/DateUtils.pm | 5 ++++- t/DateUtils.t | 13 +++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/Koha/DateUtils.pm b/Koha/DateUtils.pm index 0475312d07..773d83959d 100644 --- a/Koha/DateUtils.pm +++ b/Koha/DateUtils.pm @@ -183,7 +183,10 @@ 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'; + if ( $ampm && $ampm eq 'PM' ) { + $dt_params{hour} += 12; + $dt_params{hour} %= 24; + } my $dt = eval { DateTime->new( diff --git a/t/DateUtils.t b/t/DateUtils.t index 3c861d6fbe..fb68d03b7f 100755 --- a/t/DateUtils.t +++ b/t/DateUtils.t @@ -4,7 +4,7 @@ use DateTime::TimeZone; use C4::Context; -use Test::More tests => 76; +use Test::More tests => 78; use Test::MockModule; use Test::Warn; @@ -258,7 +258,16 @@ $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 ' ); - +$dt = dt_from_string('2015-01-31 12:02 AM'); +is( output_pref( {dt => $dt} ), '31/01/2015 12:02', 'dt_from_string ' ); +$dt = dt_from_string('2015-01-31 12:02:03 AM'); +is( output_pref( {dt => $dt} ), '31/01/2015 12:02', 'dt_from_string ' ); + +t::lib::Mocks::mock_preference('TimeFormat', '12hr'); +$dt = DateTime->new( year => 2020, month => 5, day => 28, hour => 12, minute => 49 ); +my $output = output_pref({ dt => $dt, dateformat => 'iso' }); +$dt = dt_from_string( $output, 'iso' ); +t::lib::Mocks::mock_preference('TimeFormat', '24hr'); # output_pref with no parameters, single parameter (no hash) is( output_pref(), undef, 'Call output_pref without parameters' ); -- 2.20.1