Bug 5549 : Format due dates in opac
authorColin Campbell <colin.campbell@ptfs-europe.com>
Thu, 23 Jun 2011 10:58:09 +0000 (11:58 +0100)
committerChris Cormack <chrisc@catalyst.net.nz>
Tue, 20 Mar 2012 00:24:51 +0000 (13:24 +1300)
Further change to ensure that where dates are formatted from the
db form the duedate loses the 23:59

Koha/DateUtils.pm
opac/opac-detail.pl
opac/opac-readingrecord.pl
opac/opac-reserve.pl
t/DateUtils.t

index 35043ff..e2e92bd 100644 (file)
@@ -26,7 +26,9 @@ use C4::Context;
 use base 'Exporter';
 use version; our $VERSION = qv('1.0.0');
 
-our @EXPORT = (qw( dt_from_string output_pref format_sqldatetime output_pref_due ));
+our @EXPORT = (
+    qw( dt_from_string output_pref format_sqldatetime output_pref_due format_sqlduedatetime)
+);
 
 =head1 DateUtils
 
@@ -138,7 +140,7 @@ the time portion is stripped if it is '23:59'
 
 sub output_pref_due {
     my $disp_str = output_pref(@_);
-    $disp_str=~s/ 23:59//;
+    $disp_str =~ s/ 23:59//;
     return $disp_str;
 }
 
@@ -162,4 +164,24 @@ sub format_sqldatetime {
     return q{};
 }
 
+=head2 format_sqlduedatetime
+
+$string = format_sqldatetime( $string_as_returned_from_db );
+
+a convenience routine for calling dt_from_string and formatting the result
+with output_pref_due as it is a frequent activity in scripts
+
+=cut
+
+sub format_sqlduedatetime {
+    my $str        = shift;
+    my $force_pref = shift;    # if testing we want to override Context
+    if ( defined $str && $str =~ m/^\d{4}-\d{2}-\d{2}/ ) {
+        my $dt = dt_from_string( $str, 'sql' );
+        $dt->truncate( to => 'minutes' );
+        return output_pref_due( $dt, $force_pref );
+    }
+    return q{};
+}
+
 1;
index b570215..b45250d 100755 (executable)
@@ -495,7 +495,7 @@ for my $itm (@items) {
         # I can't actually find any case in which this is defined. --amoore 2008-12-09
         $itm->{ $itm->{'publictype'} } = 1;
     }
-    $itm->{datedue}      = format_sqldatetime($itm->{datedue});
+    $itm->{datedue}      = format_sqlduedatetime($itm->{datedue});
     $itm->{datelastseen} = format_date($itm->{datelastseen});
 
     # get collection code description, too
index 5a10fb5..6872660 100755 (executable)
@@ -89,7 +89,7 @@ foreach my $issue (@{$issues} ) {
     $line{title}           = $issue->{'title'};
     $line{author}          = $issue->{'author'};
     $line{itemcallnumber}  = $issue->{'itemcallnumber'};
-    $line{date_due}        = format_sqldatetime( $issue->{date_due} );
+    $line{date_due}        = format_sqlduedatetime( $issue->{date_due} );
     $line{returndate}      = format_sqldatetime( $issue->{returndate} );
     $line{volumeddesc}     = $issue->{'volumeddesc'};
     $issue->{'itemtype'}   = C4::Context->preference('item-level_itypes') ? $issue->{'itype'} : $issue->{'itemtype'};
index 04b66f6..bcf623d 100755 (executable)
@@ -398,7 +398,7 @@ foreach my $biblioNum (@biblionumbers) {
         # change the background color.
         my $issues= GetItemIssue($itemNum);
         if ( $issues->{'date_due'} ) {
-            $itemLoopIter->{dateDue} = format_sqldatetime$issues->{date_due});
+            $itemLoopIter->{dateDue} = format_sqlduedatetime$issues->{date_due});
             $itemLoopIter->{backgroundcolor} = 'onloan';
         }
 
index 82d499f..dbf38a6 100755 (executable)
@@ -5,7 +5,7 @@ use DateTime;
 use DateTime::TimeZone;
 
 use C4::Context;
-use Test::More tests => 23;
+use Test::More tests => 25;
 
 BEGIN { use_ok('Koha::DateUtils'); }
 
@@ -34,12 +34,14 @@ $date_string = output_pref( $dt, 'metric' );
 cmp_ok $date_string, 'eq', '16/06/2011 12:00', 'metric output';
 
 $date_string = output_pref_due( $dt, 'metric' );
-cmp_ok $date_string, 'eq', '16/06/2011 12:00', 'output_pref_due preserves non midnight HH:SS';
+cmp_ok $date_string, 'eq', '16/06/2011 12:00',
+  'output_pref_due preserves non midnight HH:SS';
 
 $dt->set_hour(23);
 $dt->set_minute(59);
 $date_string = output_pref_due( $dt, 'metric' );
-cmp_ok $date_string, 'eq', '16/06/2011', 'output_pref_due truncates HH:SS at midnight';
+cmp_ok $date_string, 'eq', '16/06/2011',
+  'output_pref_due truncates HH:SS at midnight';
 
 my $dear_dirty_dublin = DateTime::TimeZone->new( name => 'Europe/Dublin' );
 my $new_dt = dt_from_string( '16/06/2011', 'metric', $dear_dirty_dublin );
@@ -82,3 +84,14 @@ 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' );
+
+$formatted = format_sqlduedatetime( '2011-06-16 12:00:07', 'metric' );
+cmp_ok(
+    $formatted, 'eq',
+    '16/06/2011 12:00',
+    'format_sqlduedatetime conversion for hourly loans'
+);
+
+$formatted = format_sqlduedatetime( '2011-06-16 23:59:07', 'metric' );
+cmp_ok( $formatted, 'eq', '16/06/2011',
+    'format_sqlduedatetime conversion for daily loans' );