5 use DateTime::TimeZone;
8 use Test::More tests => 31;
11 BEGIN { use_ok('Koha::DateUtils'); }
13 my $tz = C4::Context->tz;
15 isa_ok( $tz, 'DateTime::TimeZone', 'Context returns timezone object' );
17 my $testdate_iso = '2011-06-16'; # Bloomsday 2011
18 my $dt = dt_from_string( $testdate_iso, 'iso' );
20 isa_ok( $dt, 'DateTime', 'dt_from_string returns a DateTime object' );
22 cmp_ok( $dt->ymd(), 'eq', $testdate_iso, 'Returned object matches input' );
29 my $module_context = new Test::MockModule('C4::Context');
30 $module_context->mock(
37 my $dateformat = C4::Context->preference('dateformat');
38 cmp_ok output_pref({ dt => $dt, dateformat => $dateformat }),
41 'output_pref gives an hashref or a dt';
43 $date_string = output_pref({ dt => $dt, dateformat => 'iso', timeformat => '24hr' });
44 cmp_ok $date_string, 'eq', '2011-06-16 12:00', 'iso output';
46 $date_string = output_pref({ dt => $dt, dateformat => 'iso', timeformat => '12hr' });
47 cmp_ok $date_string, 'eq', '2011-06-16 12:00 PM', 'iso output 12hr';
49 # "notime" doesn't actually mean anything in this context, but we
50 # can't pass undef or output_pref will try to access the database
51 $date_string = output_pref({ dt => $dt, dateformat => 'iso', timeformat => 'notime', dateonly => 1 });
52 cmp_ok $date_string, 'eq', '2011-06-16', 'iso output (date only)';
54 $date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => '24hr' });
55 cmp_ok $date_string, 'eq', '06/16/2011 12:00', 'us output';
57 $date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => '12hr' });
58 cmp_ok $date_string, 'eq', '06/16/2011 12:00 PM', 'us output 12hr';
60 $date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => 'notime', dateonly => 1 });
61 cmp_ok $date_string, 'eq', '06/16/2011', 'us output (date only)';
63 # metric should return the French Revolutionary Calendar Really
64 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr' });
65 cmp_ok $date_string, 'eq', '16/06/2011 12:00', 'metric output';
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)';
70 my $dear_dirty_dublin = DateTime::TimeZone->new( name => 'Europe/Dublin' );
71 my $new_dt = dt_from_string( '16/06/2011', 'metric', $dear_dirty_dublin );
72 isa_ok( $new_dt, 'DateTime', 'Create DateTime with different timezone' );
73 cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso,
74 'Returned Dublin object matches input' );
76 $new_dt = dt_from_string( '2011-06-16 12:00', 'sql' );
77 isa_ok( $new_dt, 'DateTime', 'Create DateTime from (mysql) sql' );
78 cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso, 'sql returns correct date' );
80 $new_dt = dt_from_string( $dt, 'iso' );
81 isa_ok( $new_dt, 'DateTime', 'Passed a DateTime dt_from_string returns it' );
83 # C4::Dates allowed 00th of the month
85 my $ymd = '2012-01-01';
86 my $dt0 = dt_from_string( '00/01/2012', 'metric' );
87 isa_ok( $dt0, 'DateTime',
88 'dt_from_string returns a DateTime object passed a zero metric day' );
89 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects metric day 0' );
91 $dt0 = dt_from_string( '01/00/2012', 'us' );
92 isa_ok( $dt0, 'DateTime',
93 'dt_from_string returns a DateTime object passed a zero us day' );
94 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects us day 0' );
96 $dt0 = dt_from_string( '2012-01-00', 'iso' );
97 isa_ok( $dt0, 'DateTime',
98 'dt_from_string returns a DateTime object passed a zero iso day' );
99 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects iso day 0' );
101 # Return undef if passed mysql 0 dates
102 $dt0 = dt_from_string( '0000-00-00', 'iso' );
103 is( $dt0, undef, "undefined returned for 0 iso date" );
105 my $formatted = format_sqldatetime( '2011-06-16 12:00:07', 'metric', '24hr' );
106 cmp_ok( $formatted, 'eq', '16/06/2011 12:00', 'format_sqldatetime conversion' );
108 $formatted = format_sqldatetime( undef, 'metric' );
109 cmp_ok( $formatted, 'eq', q{},
110 'format_sqldatetime formats undef as empty string' );
112 # Test the as_due_date parameter
120 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr', as_due_date => 1 });
121 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date with hours and timeformat 24hr';
123 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr', dateonly => 1, as_due_date => 1});
124 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date without hours and timeformat 24hr';
126 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '12hr', as_due_date => 1 });
127 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date with hours and timeformat 12hr';
129 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '12hr', dateonly => 1, as_due_date => 1});
130 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date without hours and timeformat 12hr';