3 use DateTime::TimeZone;
6 use Test::More tests => 55;
8 use Time::HiRes qw/ gettimeofday /;
11 BEGIN { use_ok('Koha::DateUtils'); }
13 t::lib::Mocks::mock_preference('dateformat', 'us');
14 t::lib::Mocks::mock_preference('TimeFormat', 'This_is_not_used_but_called');
16 my $tz = C4::Context->tz;
18 isa_ok( $tz, 'DateTime::TimeZone', 'Context returns timezone object' );
20 my $testdate_iso = '2011-06-16'; # Bloomsday 2011
21 my $dt = dt_from_string( $testdate_iso, 'iso' );
23 isa_ok( $dt, 'DateTime', 'dt_from_string returns a DateTime object' );
25 cmp_ok( $dt->ymd(), 'eq', $testdate_iso, 'Returned object matches input' );
32 my $dateformat = C4::Context->preference('dateformat');
33 cmp_ok output_pref({ dt => $dt, dateformat => $dateformat }),
36 'output_pref gives an hashref or a dt';
38 $date_string = output_pref({ dt => $dt, dateformat => 'iso', timeformat => '24hr' });
39 cmp_ok $date_string, 'eq', '2011-06-16 12:00', 'iso output';
41 $date_string = output_pref({ dt => $dt, dateformat => 'iso', timeformat => '12hr' });
42 cmp_ok $date_string, 'eq', '2011-06-16 12:00 PM', 'iso output 12hr';
44 # "notime" doesn't actually mean anything in this context, but we
45 # can't pass undef or output_pref will try to access the database
46 $date_string = output_pref({ dt => $dt, dateformat => 'iso', timeformat => 'notime', dateonly => 1 });
47 cmp_ok $date_string, 'eq', '2011-06-16', 'iso output (date only)';
49 $date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => '24hr' });
50 cmp_ok $date_string, 'eq', '06/16/2011 12:00', 'us output';
52 $date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => '12hr' });
53 cmp_ok $date_string, 'eq', '06/16/2011 12:00 PM', 'us output 12hr';
55 $date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => 'notime', dateonly => 1 });
56 cmp_ok $date_string, 'eq', '06/16/2011', 'us output (date only)';
58 # metric should return the French Revolutionary Calendar Really
59 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr' });
60 cmp_ok $date_string, 'eq', '16/06/2011 12:00', 'metric output';
62 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => 'notime', dateonly => 1 });
63 cmp_ok $date_string, 'eq', '16/06/2011', 'metric output (date only)';
65 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr' });
66 cmp_ok $date_string, 'eq', '16/06/2011 12:00',
67 'output_pref preserves non midnight HH:SS';
69 my $dear_dirty_dublin = DateTime::TimeZone->new( name => 'Europe/Dublin' );
70 my $new_dt = dt_from_string( '16/06/2011', 'metric', $dear_dirty_dublin );
71 isa_ok( $new_dt, 'DateTime', 'Create DateTime with different timezone' );
72 cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso,
73 'Returned Dublin object matches input' );
75 for ( qw/ 2014-01-01 2100-01-01 9999-01-01 / ) {
76 my $duration = gettimeofday();
77 $new_dt = dt_from_string($_, 'iso', $dear_dirty_dublin);
78 $duration = gettimeofday() - $duration;
79 cmp_ok $duration, '<', 1, "Create DateTime with dt_from_string() for $_ with TZ in less than 1s";
80 $duration = gettimeofday();
81 output_pref( { dt => $new_dt } );
82 $duration = gettimeofday() - $duration;
83 cmp_ok $duration, '<', 1, "Create DateTime with output_pref() for $_ with TZ in less than 1s";
86 $new_dt = dt_from_string( '2011-06-16 12:00', 'sql' );
87 isa_ok( $new_dt, 'DateTime', 'Create DateTime from (mysql) sql' );
88 cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso, 'sql returns correct date' );
90 $new_dt = dt_from_string( $dt, 'iso' );
91 isa_ok( $new_dt, 'DateTime', 'Passed a DateTime dt_from_string returns it' );
93 # C4::Dates allowed 00th of the month
95 my $ymd = '2012-01-01';
96 my $dt0 = dt_from_string( '00/01/2012', 'metric' );
97 isa_ok( $dt0, 'DateTime',
98 'dt_from_string returns a DateTime object passed a zero metric day' );
99 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects metric day 0' );
101 $dt0 = dt_from_string( '01/00/2012', 'us' );
102 isa_ok( $dt0, 'DateTime',
103 'dt_from_string returns a DateTime object passed a zero us day' );
104 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects us day 0' );
106 $dt0 = dt_from_string( '2012-01-00', 'iso' );
107 isa_ok( $dt0, 'DateTime',
108 'dt_from_string returns a DateTime object passed a zero iso day' );
109 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects iso day 0' );
111 # Return undef if passed mysql 0 dates
112 $dt0 = dt_from_string( '0000-00-00', 'iso' );
113 is( $dt0, undef, "undefined returned for 0 iso date" );
115 # Return undef if passed mysql 9999-* date
116 my $dt9999 = dt_from_string( '9999-12-31', 'sql' );
117 is( $dt9999->ymd(), '9999-12-31', "dt_from_string should return a DateTime object for 9999-12-31" );
119 my $formatted = format_sqldatetime( '2011-06-16 12:00:07', 'metric', '24hr' );
120 cmp_ok( $formatted, 'eq', '16/06/2011 12:00', 'format_sqldatetime conversion' );
122 $formatted = format_sqldatetime( undef, 'metric' );
123 cmp_ok( $formatted, 'eq', q{},
124 'format_sqldatetime formats undef as empty string' );
126 # Test the as_due_date parameter
134 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr', as_due_date => 1 });
135 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date with hours and timeformat 24hr';
137 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr', dateonly => 1, as_due_date => 1});
138 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date without hours and timeformat 24hr';
140 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '12hr', as_due_date => 1 });
141 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date with hours and timeformat 12hr';
143 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '12hr', dateonly => 1, as_due_date => 1});
144 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date without hours and timeformat 12hr';
146 # Test as_due_date for hourly loans
154 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr', as_due_date => 1 });
155 cmp_ok $date_string, 'eq', '11/12/2013 18:35', 'as_due_date with hours and timeformat 24hr (non-midnight time)';
156 $date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => '12hr', as_due_date => 1 });
157 cmp_ok $date_string, 'eq', '12/11/2013 06:35 PM', 'as_due_date with hours and timeformat 12hr (non-midnight time)';
159 my $now = DateTime->now;
160 is( dt_from_string, $now, "Without parameter, dt_from_string should return today" );
162 my $module_context = new Test::MockModule('C4::Context');
163 $module_context->mock(
166 return DateTime::TimeZone->new( name => 'Europe/Lisbon' );
170 $dt = dt_from_string('1979-04-01');
171 isa_ok( $dt, 'DateTime', 'dt_from_string should return a DateTime object if a DST is given' );
173 $module_context->mock(
176 return DateTime::TimeZone->new( name => 'Europe/Paris' );
180 $dt = dt_from_string('2014-03-30 02:00:00');
181 isa_ok( $dt, 'DateTime', 'dt_from_string should return a DateTime object if a DST is given' );
183 # Test dt_from_string
184 t::lib::Mocks::mock_preference('dateformat', 'metric');
185 t::lib::Mocks::mock_preference('TimeFormat', '24hr');
187 # dt_from_string should take into account the dateformat pref, or the given parameter
188 $dt = dt_from_string('31/01/2015');
189 is( ref($dt), 'DateTime', '31/01/2015 is a correct date in metric format' );
190 is( output_pref( { dt => $dt, dateonly => 1 } ), '31/01/2015' );
191 $dt = eval { dt_from_string( '31/01/2015', 'iso' ); };
192 is( ref($dt), '', '31/01/2015 is not a correct date in iso format' );
193 $dt = eval { dt_from_string( '01/01/2015', 'iso' ); };
194 is( ref($dt), '', '01/01/2015 is not a correct date in iso format' );
195 $dt = eval { dt_from_string( '31/01/2015', 'us' ); };
196 is( ref($dt), '', '31/01/2015 is not a correct date in us format' );
197 $dt = dt_from_string( '01/01/2015', 'us' );
198 is( ref($dt), 'DateTime', '01/01/2015 is a correct date in us format' );
201 # default value for hh and mm is 00:00
202 $dt = dt_from_string('31/01/2015');
203 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' );
205 $dt = dt_from_string('31/01/2015 12:34');
206 is( output_pref( { dt => $dt } ), '31/01/2015 12:34', 'dt_from_string should match hh:mm' );
208 $dt = dt_from_string('31/01/2015 12:34:56');
209 is( output_pref( { dt => $dt } ), '31/01/2015 12:34', 'dt_from_string should match hh:mm:ss' );
212 $dt = dt_from_string('01/01/1900');
213 is( output_pref( { dt => $dt, dateonly => 1 } ), '01/01/1900', 'dt_from_string should manage date < 1900' );
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' );