From bb49c771de936453830e9040cd8cc8bbd0cbd6c4 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 2 Dec 2013 17:21:13 +0100 Subject: [PATCH] Bug 11148: Add a as_due_date parameter to the output_pref routine 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). --- Koha/DateUtils.pm | 56 ++++++++++++++++++++++++++++---------------------- opac/opac-reserve.pl | 2 +- t/DateUtils.t | 22 +++++++++++++++++++- 3 files changed, 54 insertions(+), 26 deletions(-) diff --git a/Koha/DateUtils.pm b/Koha/DateUtils.pm index f7d6fe1..1f12e87 100644 --- a/Koha/DateUtils.pm +++ b/Koha/DateUtils.pm @@ -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,32 +130,39 @@ 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 = do { + given ($pref) { + when (/^iso/) { + $dateonly + ? $dt->strftime("%Y-%m-%d") + : $dt->strftime("%Y-%m-%d $time"); + } + when (/^metric/) { + $dateonly + ? $dt->strftime("%d/%m/%Y") + : $dt->strftime("%d/%m/%Y $time"); + } + when (/^us/) { + $dateonly + ? $dt->strftime("%m/%d/%Y") + : $dt->strftime("%m/%d/%Y $time"); + } + default { + $dateonly + ? $dt->strftime("%Y-%m-%d") + : $dt->strftime("%Y-%m-%d $time"); + } - given ($pref) { - when (/^iso/) { - return $dateonly - ? $dt->strftime("%Y-%m-%d") - : $dt->strftime("%Y-%m-%d $time"); - } - when (/^metric/) { - return $dateonly - ? $dt->strftime("%d/%m/%Y") - : $dt->strftime("%d/%m/%Y $time"); - } - when (/^us/) { - - return $dateonly - ? $dt->strftime("%m/%d/%Y") - : $dt->strftime("%m/%d/%Y $time"); - } - default { - return $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; + + return $date; } =head2 format_sqldatetime diff --git a/opac/opac-reserve.pl b/opac/opac-reserve.pl index c07a648..228deea 100755 --- a/opac/opac-reserve.pl +++ b/opac/opac-reserve.pl @@ -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'; } diff --git a/t/DateUtils.t b/t/DateUtils.t index 00dc402..de34d70 100755 --- a/t/DateUtils.t +++ b/t/DateUtils.t @@ -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'; -- 1.7.10.4