Bug 25133: Handle 12hr format for dt_from_string
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Mon, 20 Apr 2020 08:59:55 +0000 (10:59 +0200)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Mon, 4 May 2020 07:51:22 +0000 (08:51 +0100)
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Kelly McElligott <kelly@bywatersolutions.com>
Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Koha/DateUtils.pm
circ/circulation.pl
t/DateUtils.t

index c20256c..0475312 100644 (file)
@@ -139,12 +139,17 @@ sub dt_from_string {
                     :
                     (?<second>\d{2})
                 )?
+                (
+                    \s
+                    (?<ampm>\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,
index 19feb30..81c3bcd 100755 (executable)
@@ -170,7 +170,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" ) {
index 8a606d6..3c861d6 100755 (executable)
@@ -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 {