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

(-)a/Koha/DateUtils.pm (-1 / +82 lines)
Lines 26-32 use base 'Exporter'; Link Here
26
use version; our $VERSION = qv('1.0.0');
26
use version; our $VERSION = qv('1.0.0');
27
27
28
our @EXPORT = (
28
our @EXPORT = (
29
    qw( dt_from_string output_pref format_sqldatetime )
29
    qw( dt_from_string output_pref format_sqldatetime is_formatted_date_string )
30
);
30
);
31
31
32
=head1 DateUtils
32
=head1 DateUtils
Lines 273-276 sub format_sqldatetime { Link Here
273
    return q{};
273
    return q{};
274
}
274
}
275
275
276
277
=head2 is_formatted_date_string
278
279
$dt = is_formatted_date_string($date_string, [$format]);
280
281
Passed a date string returns 1 if it is formatted following fhe format passed 
282
(default to the system preferences, otherwise 0. If the date string is empty 
283
or starts with 0000-0, 0 is returned. 
284
285
=cut
286
287
sub is_formatted_date_string {
288
    my ( $date_string, $date_format ) = @_;
289
290
    return 0 if $date_string and $date_string =~ m|^0000-0|;
291
    return 0 unless $date_string;
292
293
    $date_format = C4::Context->preference('dateformat') unless $date_format;
294
295
    my $regex;
296
297
    if ( $date_format eq 'metric' ) {
298
        # metric format is "dd/mm/yyyy[ hh:mm:ss]"
299
        $regex = qr|
300
            (?<day>\d{2})
301
            /
302
            (?<month>\d{2})
303
            /
304
            (?<year>\d{4})
305
        |xms;
306
    }
307
    elsif ( $date_format eq 'us' ) {
308
        # us format is "mm/dd/yyyy[ hh:mm:ss]"
309
        $regex = qr|
310
            (?<month>\d{2})
311
            /
312
            (?<day>\d{2})
313
            /
314
            (?<year>\d{4})
315
        |xms;
316
    }
317
    elsif ( $date_format eq 'iso' or $date_format eq 'sql' ) {
318
        # iso or sql format are yyyy-dd-mm[ hh:mm:ss]";
319
        $regex = qr|
320
            (?<year>\d{4})
321
            -
322
            (?<month>\d{2})
323
            -
324
            (?<day>\d{2})
325
        |xms;
326
    }
327
    else {
328
        warn "Invalid dateformat parameter ($date_format)";
329
        return 0;
330
    }
331
332
    # Add the faculative time part [hh:mm[:ss]]
333
    my $time_re .= qr|
334
            (
335
                \s*
336
                (?<hour>\d{2})
337
                :
338
                (?<minute>\d{2})
339
                (
340
                    :
341
                    (?<second>\d{2})
342
                )?
343
            )?
344
    |xms;
345
    $regex .= $time_re;
346
347
    if ( $date_string =~ $regex ) {
348
        return 1;
349
    }
350
    else {
351
        return 0;
352
    }
353
354
    return 0;
355
}
356
276
1;
357
1;
(-)a/t/DateUtils.t (-2 / +22 lines)
Lines 3-9 use DateTime; Link Here
3
use DateTime::TimeZone;
3
use DateTime::TimeZone;
4
4
5
use C4::Context;
5
use C4::Context;
6
use Test::More tests => 55;
6
use Test::More tests => 68;
7
use Test::MockModule;
7
use Test::MockModule;
8
use Time::HiRes qw/ gettimeofday /;
8
use Time::HiRes qw/ gettimeofday /;
9
use t::lib::Mocks;
9
use t::lib::Mocks;
Lines 215-217 is( output_pref( { dt => $dt, dateonly => 1 } ), '01/01/1900', 'dt_from_string s Link Here
215
# fallback
215
# fallback
216
$dt = dt_from_string('2015-01-31 01:02:03');
216
$dt = dt_from_string('2015-01-31 01:02:03');
217
is( output_pref( {dt => $dt} ), '31/01/2015 01:02', 'dt_from_string should fallback to sql format' );
217
is( output_pref( {dt => $dt} ), '31/01/2015 01:02', 'dt_from_string should fallback to sql format' );
218
- 
218
219
# test formatting of date strings
220
221
is( is_formatted_date_string('0000-00-00', 'iso'), 0, 'is_formatted_date_string should return 0 for 0000-00-00');
222
is( is_formatted_date_string('', 'iso'), 0, 'is_formatted_date_string should return 0 for empty string');
223
is( is_formatted_date_string('baddate', 'iso'), 0, 'is_formatted_date_string should return 0 for baddate');
224
is( is_formatted_date_string('11/06/1955', 'badformat'), 0, 'is_formatted_date_string should return 0 for unknown format');
225
226
is( is_formatted_date_string('1955-06-11', 'iso'), 1, 'is_formatted_date_string should return 1 for 1955/06/11 iso');
227
is( is_formatted_date_string('1955-06-11 12:00', 'iso'), 1, 'is_formatted_date_string should return 1 for 1955/06/11 12:00 iso');
228
is( is_formatted_date_string('11/06/1955', 'iso'), 0, 'is_formatted_date_string should return 0 for 11/06/1955 iso');
229
230
is( is_formatted_date_string('06/11/1955', 'us'), 1, 'is_formatted_date_string should return 1 for 06/11/1955 us');
231
is (is_formatted_date_string('06/11/1955 12:00', 'us'), 1, 'is_formatted_date_string should return 1 for 06/11/1955 12:00 us');
232
is( is_formatted_date_string('06-11-1955', 'us'), 0, 'is_formatted_date_string should return 0 for 06-11-1955 us');
233
234
is( is_formatted_date_string('11/06/1955', 'metric'), 1, 'is_formatted_date_string should return 1 for 06/11/1955 metric');
235
is (is_formatted_date_string('11/06/1955 12:00', 'metric'), 1, 'is_formatted_date_string should return 1 for 11/06/1955 12:00 us');
236
is( is_formatted_date_string('06-11-1955', 'metric'), 0, 'is_formatted_date_string should return 0 for 06-11-1955 metric');
237
238

Return to bug 14936