5 use DateTime::TimeZone;
8 use Test::More tests => 34;
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 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr' });
71 cmp_ok $date_string, 'eq', '16/06/2011 12:00',
72 'output_pref preserves non midnight HH:SS';
74 my $dear_dirty_dublin = DateTime::TimeZone->new( name => 'Europe/Dublin' );
75 my $new_dt = dt_from_string( '16/06/2011', 'metric', $dear_dirty_dublin );
76 isa_ok( $new_dt, 'DateTime', 'Create DateTime with different timezone' );
77 cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso,
78 'Returned Dublin object matches input' );
80 $new_dt = dt_from_string( '2011-06-16 12:00', 'sql' );
81 isa_ok( $new_dt, 'DateTime', 'Create DateTime from (mysql) sql' );
82 cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso, 'sql returns correct date' );
84 $new_dt = dt_from_string( $dt, 'iso' );
85 isa_ok( $new_dt, 'DateTime', 'Passed a DateTime dt_from_string returns it' );
87 # C4::Dates allowed 00th of the month
89 my $ymd = '2012-01-01';
90 my $dt0 = dt_from_string( '00/01/2012', 'metric' );
91 isa_ok( $dt0, 'DateTime',
92 'dt_from_string returns a DateTime object passed a zero metric day' );
93 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects metric day 0' );
95 $dt0 = dt_from_string( '01/00/2012', 'us' );
96 isa_ok( $dt0, 'DateTime',
97 'dt_from_string returns a DateTime object passed a zero us day' );
98 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects us day 0' );
100 $dt0 = dt_from_string( '2012-01-00', 'iso' );
101 isa_ok( $dt0, 'DateTime',
102 'dt_from_string returns a DateTime object passed a zero iso day' );
103 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects iso day 0' );
105 # Return undef if passed mysql 0 dates
106 $dt0 = dt_from_string( '0000-00-00', 'iso' );
107 is( $dt0, undef, "undefined returned for 0 iso date" );
109 my $formatted = format_sqldatetime( '2011-06-16 12:00:07', 'metric', '24hr' );
110 cmp_ok( $formatted, 'eq', '16/06/2011 12:00', 'format_sqldatetime conversion' );
112 $formatted = format_sqldatetime( undef, 'metric' );
113 cmp_ok( $formatted, 'eq', q{},
114 'format_sqldatetime formats undef as empty string' );
116 # Test the as_due_date parameter
124 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr', as_due_date => 1 });
125 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date with hours and timeformat 24hr';
127 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr', dateonly => 1, as_due_date => 1});
128 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date without hours and timeformat 24hr';
130 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '12hr', as_due_date => 1 });
131 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date with hours and timeformat 12hr';
133 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '12hr', dateonly => 1, as_due_date => 1});
134 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date without hours and timeformat 12hr';
136 # Test as_due_date for hourly loans
144 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr', as_due_date => 1 });
145 cmp_ok $date_string, 'eq', '11/12/2013 18:35', 'as_due_date with hours and timeformat 24hr (non-midnight time)';
146 $date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => '12hr', as_due_date => 1 });
147 cmp_ok $date_string, 'eq', '12/11/2013 06:35 PM', 'as_due_date with hours and timeformat 12hr (non-midnight time)';