3 use DateTime::TimeZone;
7 use Test::More tests => 63;
11 use Time::HiRes qw/ gettimeofday /;
16 BEGIN { use_ok('Koha::DateUtils'); }
18 t::lib::Mocks::mock_preference('dateformat', 'us');
19 t::lib::Mocks::mock_preference('TimeFormat', 'This_is_not_used_but_called');
21 my $tz = C4::Context->tz;
23 isa_ok( $tz, 'DateTime::TimeZone', 'Context returns timezone object' );
25 my $testdate_iso = '2011-06-16'; # Bloomsday 2011
26 my $dt = dt_from_string( $testdate_iso, 'iso' );
28 isa_ok( $dt, 'DateTime', 'dt_from_string returns a DateTime object' );
30 cmp_ok( $dt->ymd(), 'eq', $testdate_iso, 'Returned object matches input' );
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' );
81 skip "Don't test execution time of dt_from_string on slow servers", 6 if $ENV{SLOW_SERVER};
82 for ( qw/ 2014-01-01 2100-01-01 9999-01-01 / ) {
83 my $duration = gettimeofday();
84 $new_dt = dt_from_string($_, 'iso', $dear_dirty_dublin);
85 $duration = gettimeofday() - $duration;
86 cmp_ok $duration, '<', 1, "Create DateTime with dt_from_string() for $_ with TZ in less than 1s";
87 $duration = gettimeofday();
88 output_pref( { dt => $new_dt } );
89 $duration = gettimeofday() - $duration;
90 cmp_ok $duration, '<', 1, "Create DateTime with output_pref() for $_ with TZ in less than 1s";
94 $new_dt = dt_from_string( '2011-06-16 12:00', 'sql' );
95 isa_ok( $new_dt, 'DateTime', 'Create DateTime from (mysql) sql' );
96 cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso, 'sql returns correct date' );
98 $new_dt = dt_from_string( $dt, 'iso' );
99 isa_ok( $new_dt, 'DateTime', 'Passed a DateTime dt_from_string returns it' );
101 my $ymd = '2012-01-01';
102 my $dt0 = dt_from_string( '00/01/2012', 'metric' );
103 isa_ok( $dt0, 'DateTime',
104 'dt_from_string returns a DateTime object passed a zero metric day' );
105 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects metric day 0' );
107 $dt0 = dt_from_string( '01/00/2012', 'us' );
108 isa_ok( $dt0, 'DateTime',
109 'dt_from_string returns a DateTime object passed a zero us day' );
110 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects us day 0' );
112 $dt0 = dt_from_string( '2012-01-00', 'iso' );
113 isa_ok( $dt0, 'DateTime',
114 'dt_from_string returns a DateTime object passed a zero iso day' );
115 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects iso day 0' );
117 # Return undef if passed mysql 0 dates
118 $dt0 = dt_from_string( '0000-00-00', 'iso' );
119 is( $dt0, undef, "undefined returned for 0 iso date" );
121 # Return undef if passed mysql 9999-* date
122 my $dt9999 = dt_from_string( '9999-12-31', 'sql' );
123 is( $dt9999->ymd(), '9999-12-31', "dt_from_string should return a DateTime object for 9999-12-31" );
125 my $formatted = format_sqldatetime( '2011-06-16 12:00:07', 'metric', '24hr' );
126 cmp_ok( $formatted, 'eq', '16/06/2011 12:00', 'format_sqldatetime conversion' );
128 $formatted = format_sqldatetime( undef, 'metric' );
129 cmp_ok( $formatted, 'eq', q{},
130 'format_sqldatetime formats undef as empty string' );
132 # Test the as_due_date parameter
140 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr', as_due_date => 1 });
141 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date with hours and timeformat 24hr';
143 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr', dateonly => 1, as_due_date => 1});
144 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date without hours and timeformat 24hr';
146 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '12hr', as_due_date => 1 });
147 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date with hours and timeformat 12hr';
149 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '12hr', dateonly => 1, as_due_date => 1});
150 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date without hours and timeformat 12hr';
152 # Test as_due_date for hourly loans
160 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr', as_due_date => 1 });
161 cmp_ok $date_string, 'eq', '11/12/2013 18:35', 'as_due_date with hours and timeformat 24hr (non-midnight time)';
162 $date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => '12hr', as_due_date => 1 });
163 cmp_ok $date_string, 'eq', '12/11/2013 06:35 PM', 'as_due_date with hours and timeformat 12hr (non-midnight time)';
165 my $now = DateTime->now;
166 is( dt_from_string, $now, "Without parameter, dt_from_string should return today" );
168 my $module_context = new Test::MockModule('C4::Context');
169 $module_context->mock(
172 return DateTime::TimeZone->new( name => 'Europe/Lisbon' );
176 $dt = dt_from_string('1979-04-01');
177 isa_ok( $dt, 'DateTime', 'dt_from_string should return a DateTime object if a DST is given' );
179 $module_context->mock(
182 return DateTime::TimeZone->new( name => 'Europe/Paris' );
186 $dt = dt_from_string('2014-03-30 02:00:00');
187 isa_ok( $dt, 'DateTime', 'dt_from_string should return a DateTime object if a DST is given' );
189 # Test dt_from_string
190 t::lib::Mocks::mock_preference('dateformat', 'metric');
191 t::lib::Mocks::mock_preference('TimeFormat', '24hr');
193 # dt_from_string should take into account the dateformat pref, or the given parameter
194 $dt = dt_from_string('31/01/2015');
195 is( ref($dt), 'DateTime', '31/01/2015 is a correct date in metric format' );
196 is( output_pref( { dt => $dt, dateonly => 1 } ), '31/01/2015' );
197 $dt = eval { dt_from_string( '31/01/2015', 'iso' ); };
198 is( ref($dt), '', '31/01/2015 is not a correct date in iso format' );
199 $dt = eval { dt_from_string( '01/01/2015', 'iso' ); };
200 is( ref($dt), '', '01/01/2015 is not a correct date in iso format' );
201 $dt = eval { dt_from_string( '31/01/2015', 'us' ); };
202 is( ref($dt), '', '31/01/2015 is not a correct date in us format' );
203 $dt = dt_from_string( '01/01/2015', 'us' );
204 is( ref($dt), 'DateTime', '01/01/2015 is a correct date in us format' );
205 $dt = dt_from_string( '01.01.2015', 'dmydot' );
206 is( ref($dt), 'DateTime', '01.01.2015 is a correct date in dmydot format' );
209 # default value for hh and mm is 00:00
210 $dt = dt_from_string('31/01/2015');
211 is( output_pref( { dt => $dt } ), '31/01/2015 00:00', 'dt_from_string should generate a DT object with 00:00 as default hh:mm' );
213 $dt = dt_from_string('31/01/2015 12:34');
214 is( output_pref( { dt => $dt } ), '31/01/2015 12:34', 'dt_from_string should match hh:mm' );
216 $dt = dt_from_string('31/01/2015 12:34:56');
217 is( output_pref( { dt => $dt } ), '31/01/2015 12:34', 'dt_from_string should match hh:mm:ss' );
220 $dt = dt_from_string('01/01/1900');
221 is( output_pref( { dt => $dt, dateonly => 1 } ), '01/01/1900', 'dt_from_string should manage date < 1900' );
224 $dt = dt_from_string('2015-01-31 01:02:03');
225 is( output_pref( {dt => $dt} ), '31/01/2015 01:02', 'dt_from_string should fallback to sql format' );
227 # output_pref with no parameters, single parameter (no hash)
228 is( output_pref(), undef, 'Call output_pref without parameters' );
230 output_pref( 'no_dt' );
231 ok( 0, 'Passed single invalid dt to output_pref' );
233 is( ref($_), 'Koha::Exceptions::WrongParameter',
234 'Passed single invalid dt to output_pref' );
237 # pass invalid dt via hash
239 output_pref({ dt => 'no_dt' });
240 ok( 0, 'Passed invalid dt in hash to output_pref' );
242 is( ref($_), 'Koha::Exceptions::WrongParameter',
243 'Passed invalid dt in hash to output_pref' );
246 # output_pref with str parameter
247 is( output_pref( { 'str' => $testdate_iso, dateformat => 'iso', dateonly => 1 } ), $testdate_iso, 'output_pref should handle correctly the iso parameter' );
248 my $output_for_invalid_date;
250 $output_for_invalid_date = output_pref({ str => 'invalid_date' });
251 ok( 0, 'Invalid date string passed to output_pref' );
253 is( ref($_), 'Koha::Exceptions::WrongParameter',
254 'Invalid date string passed to output_pref' );
256 is( $output_for_invalid_date, undef, 'No return value from output_pref' );
258 output_pref({ 'str' => $testdate_iso, dt => $dt, dateformat => 'iso', dateonly => 1 });
259 ok( 0, 'output_pref should carp if str and dt parameters are passed together' );
261 is( ref($_), 'Koha::Exceptions::WrongParameter', 'output_pref should throw an exception if str and dt parameters are passed together' );