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 154-184 sub output_pref { Link Here
154
154
155
}
155
}
156
156
157
=head2 output_pref_due
158
159
$date_string = output_pref_due({ dt => $dt [, dateformat => $date_format, timeformat => $time_format, dateonly => 0|1 ] });
160
$date_string = output_pref_due($dt);
161
162
Returns a string containing the time & date formatted as per the C4::Context setting
163
164
This routine can either be passed a DateTime object or or a hashref.  If it is
165
passed a hashref, the expected keys are a mandatory 'dt' for the DateTime,
166
an optional 'dateformat' to override the dateformat system preference, an
167
optional 'timeformat' to override the TimeFormat system preference value,
168
and an optional 'dateonly' to specify that only the formatted date string
169
should be returned without the time.
170
171
This is effectively a wrapper around output_pref for due dates;
172
the time portion is stripped if it is '23:59'
173
174
=cut
175
176
sub output_pref_due {
177
    my $disp_str = output_pref(@_);
178
    $disp_str =~ s/ 23:59//;
179
    return $disp_str;
180
}
181
182
=head2 format_sqldatetime
157
=head2 format_sqldatetime
183
158
184
$string = format_sqldatetime( $string_as_returned_from_db );
159
$string = format_sqldatetime( $string_as_returned_from_db );
Lines 208-239 sub format_sqldatetime { Link Here
208
    return q{};
183
    return q{};
209
}
184
}
210
185
211
=head2 format_sqlduedatetime
212
213
$string = format_sqldatetime( $string_as_returned_from_db );
214
215
a convenience routine for calling dt_from_string and formatting the result
216
with output_pref_due as it is a frequent activity in scripts
217
218
=cut
219
220
sub format_sqlduedatetime {
221
    my $str        = shift;
222
    my $force_pref = shift;    # if testing we want to override Context
223
    my $force_time = shift;
224
    my $dateonly   = shift;
225
226
    if ( defined $str && $str =~ m/^\d{4}-\d{2}-\d{2}/ ) {
227
        my $dt = dt_from_string( $str, 'sql' );
228
        $dt->truncate( to => 'minute' );
229
        return output_pref_due({
230
            dt => $dt,
231
            dateformat => $force_pref,
232
            timeformat => $force_time,
233
            dateonly => $dateonly
234
        });
235
    }
236
    return q{};
237
}
238
239
1;
186
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