View | Details | Raw Unified | Return to bug 11148
Collapse All | Expand All

(-)a/Koha/DateUtils.pm (-54 / +1 lines)
Lines 27-33 use base 'Exporter'; Link Here
27
use version; our $VERSION = qv('1.0.0');
27
use version; our $VERSION = qv('1.0.0');
28
28
29
our @EXPORT = (
29
our @EXPORT = (
30
    qw( dt_from_string output_pref format_sqldatetime output_pref_due format_sqlduedatetime)
30
    qw( dt_from_string output_pref format_sqldatetime )
31
);
31
);
32
32
33
=head1 DateUtils
33
=head1 DateUtils
Lines 157-187 sub output_pref { Link Here
157
    return;
157
    return;
158
}
158
}
159
159
160
=head2 output_pref_due
161
162
$date_string = output_pref_due({ dt => $dt [, dateformat => $date_format, timeformat => $time_format, dateonly => 0|1 ] });
163
$date_string = output_pref_due($dt);
164
165
Returns a string containing the time & date formatted as per the C4::Context setting
166
167
This routine can either be passed a DateTime object or or a hashref.  If it is
168
passed a hashref, the expected keys are a mandatory 'dt' for the DateTime,
169
an optional 'dateformat' to override the dateformat system preference, an
170
optional 'timeformat' to override the TimeFormat system preference value,
171
and an optional 'dateonly' to specify that only the formatted date string
172
should be returned without the time.
173
174
This is effectively a wrapper around output_pref for due dates;
175
the time portion is stripped if it is '23:59'
176
177
=cut
178
179
sub output_pref_due {
180
    my $disp_str = output_pref(@_);
181
    $disp_str =~ s/ 23:59//;
182
    return $disp_str;
183
}
184
185
=head2 format_sqldatetime
160
=head2 format_sqldatetime
186
161
187
$string = format_sqldatetime( $string_as_returned_from_db );
162
$string = format_sqldatetime( $string_as_returned_from_db );
Lines 211-242 sub format_sqldatetime { Link Here
211
    return q{};
186
    return q{};
212
}
187
}
213
188
214
=head2 format_sqlduedatetime
215
216
$string = format_sqldatetime( $string_as_returned_from_db );
217
218
a convenience routine for calling dt_from_string and formatting the result
219
with output_pref_due as it is a frequent activity in scripts
220
221
=cut
222
223
sub format_sqlduedatetime {
224
    my $str        = shift;
225
    my $force_pref = shift;    # if testing we want to override Context
226
    my $force_time = shift;
227
    my $dateonly   = shift;
228
229
    if ( defined $str && $str =~ m/^\d{4}-\d{2}-\d{2}/ ) {
230
        my $dt = dt_from_string( $str, 'sql' );
231
        $dt->truncate( to => 'minute' );
232
        return output_pref_due({
233
            dt => $dt,
234
            dateformat => $force_pref,
235
            timeformat => $force_time,
236
            dateonly => $dateonly
237
        });
238
    }
239
    return q{};
240
}
241
242
1;
189
1;
(-)a/opac/opac-reserve.pl (-1 / +1 lines)
Lines 426-432 foreach my $biblioNum (@biblionumbers) { Link Here
426
        # change the background color.
426
        # change the background color.
427
        my $issues= GetItemIssue($itemNum);
427
        my $issues= GetItemIssue($itemNum);
428
        if ( $issues->{'date_due'} ) {
428
        if ( $issues->{'date_due'} ) {
429
            $itemLoopIter->{dateDue} = format_sqlduedatetime($issues->{date_due});
429
            $itemLoopIter->{dateDue} = output_pref({ dt => dt_from_string($issues->{date_due}, 'sql'), dateonly => 1 });
430
            $itemLoopIter->{backgroundcolor} = 'onloan';
430
            $itemLoopIter->{backgroundcolor} = 'onloan';
431
        }
431
        }
432
432
(-)a/t/DateUtils.t (-23 / +1 lines)
Lines 5-11 use DateTime; Link Here
5
use DateTime::TimeZone;
5
use DateTime::TimeZone;
6
6
7
use C4::Context;
7
use C4::Context;
8
use Test::More tests => 31;
8
use Test::More tests => 27;
9
use Test::MockModule;
9
use Test::MockModule;
10
10
11
BEGIN { use_ok('Koha::DateUtils'); }
11
BEGIN { use_ok('Koha::DateUtils'); }
Lines 67-82 cmp_ok $date_string, 'eq', '16/06/2011 12:00', 'metric output'; Link Here
67
$date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => 'notime', dateonly => 1 });
67
$date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => 'notime', dateonly => 1 });
68
cmp_ok $date_string, 'eq', '16/06/2011', 'metric output (date only)';
68
cmp_ok $date_string, 'eq', '16/06/2011', 'metric output (date only)';
69
69
70
$date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr' });
71
cmp_ok $date_string, 'eq', '16/06/2011 12:00',
72
  'output_pref_due preserves non midnight HH:SS';
73
74
$dt->set_hour(23);
75
$dt->set_minute(59);
76
$date_string = output_pref_due({ dt => $dt, dateformat => 'metric', timeformat => '24hr' });
77
cmp_ok $date_string, 'eq', '16/06/2011',
78
  'output_pref_due truncates HH:SS at midnight';
79
80
my $dear_dirty_dublin = DateTime::TimeZone->new( name => 'Europe/Dublin' );
70
my $dear_dirty_dublin = DateTime::TimeZone->new( name => 'Europe/Dublin' );
81
my $new_dt = dt_from_string( '16/06/2011', 'metric', $dear_dirty_dublin );
71
my $new_dt = dt_from_string( '16/06/2011', 'metric', $dear_dirty_dublin );
82
isa_ok( $new_dt, 'DateTime', 'Create DateTime with different timezone' );
72
isa_ok( $new_dt, 'DateTime', 'Create DateTime with different timezone' );
Lines 118-131 cmp_ok( $formatted, 'eq', '16/06/2011 12:00', 'format_sqldatetime conversion' ); Link Here
118
$formatted = format_sqldatetime( undef, 'metric' );
108
$formatted = format_sqldatetime( undef, 'metric' );
119
cmp_ok( $formatted, 'eq', q{},
109
cmp_ok( $formatted, 'eq', q{},
120
    'format_sqldatetime formats undef as empty string' );
110
    'format_sqldatetime formats undef as empty string' );
121
122
$formatted = format_sqlduedatetime( '2011-06-16 12:00:07', 'metric', '24hr' );
123
cmp_ok(
124
    $formatted, 'eq',
125
    '16/06/2011 12:00',
126
    'format_sqlduedatetime conversion for hourly loans'
127
);
128
129
$formatted = format_sqlduedatetime( '2011-06-16 23:59:07', 'metric', '24hr' );
130
cmp_ok( $formatted, 'eq', '16/06/2011',
131
    'format_sqlduedatetime conversion for daily loans' );
132
- 

Return to bug 11148