Bug 5549 : DateUtils handling odd cases
[koha.git] / t / DateUtils.t
1 use strict;
2 use warnings;
3 use 5.010;
4 use DateTime;
5 use DateTime::TimeZone;
6
7 use C4::Context;
8 use Test::More tests => 19;    # last test to print
9
10 BEGIN { use_ok('Koha::DateUtils'); }
11
12 my $tz = C4::Context->tz;
13
14 isa_ok( $tz, 'DateTime::TimeZone', 'Context returns timezone object' );
15
16 my $testdate_iso = '2011-06-16';                   # Bloomsday 2011
17 my $dt = dt_from_string( $testdate_iso, 'iso' );
18
19 isa_ok( $dt, 'DateTime', 'dt_from_string returns a DateTime object' );
20
21 cmp_ok( $dt->ymd(), 'eq', $testdate_iso, 'Returned object matches input' );
22
23 $dt->set_hour(12);
24 $dt->set_minute(0);
25
26 my $date_string = output_pref( $dt, 'iso' );
27 cmp_ok $date_string, 'eq', '2011-06-16 12:00', 'iso output';
28
29 $date_string = output_pref( $dt, 'us' );
30 cmp_ok $date_string, 'eq', '06/16/2011 12:00', 'us output';
31
32 # metric should return the French Revolutionary Calendar Really
33 $date_string = output_pref( $dt, 'metric' );
34 cmp_ok $date_string, 'eq', '16/06/2011 12:00', 'metric output';
35
36 my $dear_dirty_dublin = DateTime::TimeZone->new( name => 'Europe/Dublin' );
37 my $new_dt = dt_from_string( '16/06/2011', 'metric', $dear_dirty_dublin );
38 isa_ok( $new_dt, 'DateTime', 'Create DateTime with different timezone' );
39 cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso,
40     'Returned Dublin object matches input' );
41
42 $new_dt = dt_from_string( '2011-06-16 12:00', 'sql' );
43 isa_ok( $new_dt, 'DateTime', 'Create DateTime from (mysql) sql' );
44 cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso, 'sql returns correct date' );
45
46 $new_dt = dt_from_string( $dt, 'iso' );
47 isa_ok( $new_dt, 'DateTime', 'Passed a DateTime dt_from_string returns it' );
48
49 # C4::Dates allowed 00th of the month
50
51 my $ymd = '2012-01-01';
52 my $dt0 = dt_from_string( '00/01/2012', 'metric' );
53 isa_ok( $dt0, 'DateTime',
54     'dt_from_string returns a DateTime object passed a zero metric day' );
55 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects metric day 0' );
56
57 $dt0 = dt_from_string( '01/00/2012', 'us' );
58 isa_ok( $dt0, 'DateTime',
59     'dt_from_string returns a DateTime object passed a zero us day' );
60 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects us day 0' );
61
62 $dt0 = dt_from_string( '2012-01-00', 'iso' );
63 isa_ok( $dt0, 'DateTime',
64     'dt_from_string returns a DateTime object passed a zero iso day' );
65 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects iso day 0' );
66
67 # Return undef if passed mysql 0 dates
68 $dt0 = dt_from_string( '0000-00-00', 'iso' );
69 is( $dt0, undef, "undefined returned for 0 iso date" );