Bug 11148: Add a as_due_date parameter to the output_pref routine
authorJonathan Druart <jonathan.druart@biblibre.com>
Mon, 2 Dec 2013 16:21:13 +0000 (17:21 +0100)
committerGalen Charlton <gmc@esilibrary.com>
Fri, 18 Apr 2014 21:45:45 +0000 (21:45 +0000)
This parameter is a boolean, if true, the hours won't be displayed if
the time is 23:59 (24hr format) or 11:59 PM (12hr format).

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Galen Charlton <gmc@esilibrary.com>
Koha/DateUtils.pm
opac/opac-reserve.pl
t/DateUtils.t

index 85c90d6..b372a3a 100644 (file)
@@ -93,7 +93,7 @@ s/(\d{4})(\d{2})(\d{2})\s+(\d{2})(\d{2})(\d{2})/$1-$2-$3T$4:$5:$6/;
 
 =head2 output_pref
 
-$date_string = output_pref({ dt => $dt [, dateformat => $date_format, timeformat => $time_format, dateonly => 0|1 ] });
+$date_string = output_pref({ dt => $dt [, dateformat => $date_format, timeformat => $time_format, dateonly => 0|1, as_due_date => 0|1 ] });
 $date_string = output_pref( $dt );
 
 Returns a string containing the time & date formatted as per the C4::Context setting,
@@ -110,12 +110,13 @@ should be returned without the time.
 
 sub output_pref {
     my $params = shift;
-    my ( $dt, $force_pref, $force_time, $dateonly );
+    my ( $dt, $force_pref, $force_time, $dateonly, $as_due_date );
     if ( ref $params eq 'HASH' ) {
         $dt         = $params->{dt};
         $force_pref = $params->{dateformat};         # if testing we want to override Context
         $force_time = $params->{timeformat};
         $dateonly   = $params->{dateonly} || 0;    # if you don't want the hours and minutes
+        $as_due_date = $params->{as_due_date} || 0; # don't display the hours and minutes if eq to 23:59 or 11:59 (depending the TimeFormat value)
     } else {
         $dt = $params;
     }
@@ -129,29 +130,35 @@ sub output_pref {
 
     my $time_format = $force_time || C4::Context->preference('TimeFormat');
     my $time = ( $time_format eq '12hr' ) ? '%I:%M %p' : '%H:%M';
-
+    my $date;
     if ( $pref =~ m/^iso/ ) {
-        return $dateonly
+        $date = $dateonly
           ? $dt->strftime("%Y-%m-%d")
           : $dt->strftime("%Y-%m-%d $time");
     }
     elsif ( $pref =~ m/^metric/ ) {
-        return $dateonly
+        $date = $dateonly
           ? $dt->strftime("%d/%m/%Y")
           : $dt->strftime("%d/%m/%Y $time");
     }
     elsif ( $pref =~ m/^us/ ) {
-
-        return $dateonly
+        $date = $dateonly
           ? $dt->strftime("%m/%d/%Y")
           : $dt->strftime("%m/%d/%Y $time");
     }
     else {
-        return $dateonly
+        $date = $dateonly
           ? $dt->strftime("%Y-%m-%d")
           : $dt->strftime("%Y-%m-%d $time");
     }
 
+    if ( $as_due_date ) {
+        $time_format eq '12hr'
+            ? $date =~ s| 11:59 PM$||
+            : $date =~ s| 23:59$||;
+    }
+
+    return $date;
 }
 
 =head2 format_sqldatetime
index 9649231..473ead8 100755 (executable)
@@ -426,7 +426,7 @@ foreach my $biblioNum (@biblionumbers) {
         # change the background color.
         my $issues= GetItemIssue($itemNum);
         if ( $issues->{'date_due'} ) {
-            $itemLoopIter->{dateDue} = output_pref({ dt => dt_from_string($issues->{date_due}, 'sql'), dateonly => 1 });
+            $itemLoopIter->{dateDue} = output_pref({ dt => dt_from_string($issues->{date_due}, 'sql'), as_due_date => 1 });
             $itemLoopIter->{backgroundcolor} = 'onloan';
         }
 
index 00dc402..de34d70 100755 (executable)
@@ -5,7 +5,7 @@ use DateTime;
 use DateTime::TimeZone;
 
 use C4::Context;
-use Test::More tests => 27;
+use Test::More tests => 31;
 use Test::MockModule;
 
 BEGIN { use_ok('Koha::DateUtils'); }
@@ -108,3 +108,23 @@ cmp_ok( $formatted, 'eq', '16/06/2011 12:00', 'format_sqldatetime conversion' );
 $formatted = format_sqldatetime( undef, 'metric' );
 cmp_ok( $formatted, 'eq', q{},
     'format_sqldatetime formats undef as empty string' );
+
+# Test the as_due_date parameter
+$dt = DateTime->new(
+    year       => 2013,
+    month      => 12,
+    day        => 11,
+    hour       => 23,
+    minute     => 59,
+);
+$date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr', as_due_date => 1 });
+cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date with hours and timeformat 24hr';
+
+$date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr', dateonly => 1, as_due_date => 1});
+cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date without hours and timeformat 24hr';
+
+$date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '12hr', as_due_date => 1 });
+cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date with hours and timeformat 12hr';
+
+$date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '12hr', dateonly => 1, as_due_date => 1});
+cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date without hours and timeformat 12hr';