From 16ce7c7b3b7055638926e36a6bcc6a3f1e1cb3e2 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). Signed-off-by: Marcel de Rooy Signed-off-by: Galen Charlton (cherry picked from commit 4ea09932ddbfbcc0e9201f7cc6b2a5e08a4efabf) Signed-off-by: Fridolin Somers --- Koha/DateUtils.pm | 23 +++++++++++++++-------- opac/opac-reserve.pl | 2 +- t/DateUtils.t | 22 +++++++++++++++++++++- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/Koha/DateUtils.pm b/Koha/DateUtils.pm index 85c90d6876..b372a3a5a4 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,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 diff --git a/opac/opac-reserve.pl b/opac/opac-reserve.pl index 9649231de0..473ead89e8 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 00dc4029a1..de34d70bd9 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'; -- 2.39.5