5 use DateTime::TimeZone;
8 use Test::More tests => 31;
10 BEGIN { use_ok('Koha::DateUtils'); }
12 my $tz = C4::Context->tz;
14 isa_ok( $tz, 'DateTime::TimeZone', 'Context returns timezone object' );
16 my $testdate_iso = '2011-06-16'; # Bloomsday 2011
17 my $dt = dt_from_string( $testdate_iso, 'iso' );
19 isa_ok( $dt, 'DateTime', 'dt_from_string returns a DateTime object' );
21 cmp_ok( $dt->ymd(), 'eq', $testdate_iso, 'Returned object matches input' );
28 my $dateformat = C4::Context->preference('dateformat');
29 cmp_ok output_pref({ dt => $dt, dateformat => $dateformat }),
32 'output_pref gives an hashref or a dt';
34 $date_string = output_pref({ dt => $dt, dateformat => 'iso', timeformat => '24hr' });
35 cmp_ok $date_string, 'eq', '2011-06-16 12:00', 'iso output';
37 $date_string = output_pref({ dt => $dt, dateformat => 'iso', timeformat => '12hr' });
38 cmp_ok $date_string, 'eq', '2011-06-16 12:00 PM', 'iso output 12hr';
40 # "notime" doesn't actually mean anything in this context, but we
41 # can't pass undef or output_pref will try to access the database
42 $date_string = output_pref({ dt => $dt, dateformat => 'iso', timeformat => 'notime', dateonly => 1 });
43 cmp_ok $date_string, 'eq', '2011-06-16', 'iso output (date only)';
45 $date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => '24hr' });
46 cmp_ok $date_string, 'eq', '06/16/2011 12:00', 'us output';
48 $date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => '12hr' });
49 cmp_ok $date_string, 'eq', '06/16/2011 12:00 PM', 'us output 12hr';
51 $date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => 'notime', dateonly => 1 });
52 cmp_ok $date_string, 'eq', '06/16/2011', 'us output (date only)';
54 # metric should return the French Revolutionary Calendar Really
55 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr' });
56 cmp_ok $date_string, 'eq', '16/06/2011 12:00', 'metric output';
58 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => 'notime', dateonly => 1 });
59 cmp_ok $date_string, 'eq', '16/06/2011', 'metric output (date only)';
61 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr' });
62 cmp_ok $date_string, 'eq', '16/06/2011 12:00',
63 'output_pref_due preserves non midnight HH:SS';
67 $date_string = output_pref_due({ dt => $dt, dateformat => 'metric', timeformat => '24hr' });
68 cmp_ok $date_string, 'eq', '16/06/2011',
69 'output_pref_due truncates HH:SS at midnight';
71 my $dear_dirty_dublin = DateTime::TimeZone->new( name => 'Europe/Dublin' );
72 my $new_dt = dt_from_string( '16/06/2011', 'metric', $dear_dirty_dublin );
73 isa_ok( $new_dt, 'DateTime', 'Create DateTime with different timezone' );
74 cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso,
75 'Returned Dublin object matches input' );
77 $new_dt = dt_from_string( '2011-06-16 12:00', 'sql' );
78 isa_ok( $new_dt, 'DateTime', 'Create DateTime from (mysql) sql' );
79 cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso, 'sql returns correct date' );
81 $new_dt = dt_from_string( $dt, 'iso' );
82 isa_ok( $new_dt, 'DateTime', 'Passed a DateTime dt_from_string returns it' );
84 # C4::Dates allowed 00th of the month
86 my $ymd = '2012-01-01';
87 my $dt0 = dt_from_string( '00/01/2012', 'metric' );
88 isa_ok( $dt0, 'DateTime',
89 'dt_from_string returns a DateTime object passed a zero metric day' );
90 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects metric day 0' );
92 $dt0 = dt_from_string( '01/00/2012', 'us' );
93 isa_ok( $dt0, 'DateTime',
94 'dt_from_string returns a DateTime object passed a zero us day' );
95 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects us day 0' );
97 $dt0 = dt_from_string( '2012-01-00', 'iso' );
98 isa_ok( $dt0, 'DateTime',
99 'dt_from_string returns a DateTime object passed a zero iso day' );
100 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects iso day 0' );
102 # Return undef if passed mysql 0 dates
103 $dt0 = dt_from_string( '0000-00-00', 'iso' );
104 is( $dt0, undef, "undefined returned for 0 iso date" );
106 my $formatted = format_sqldatetime( '2011-06-16 12:00:07', 'metric', '24hr' );
107 cmp_ok( $formatted, 'eq', '16/06/2011 12:00', 'format_sqldatetime conversion' );
109 $formatted = format_sqldatetime( undef, 'metric' );
110 cmp_ok( $formatted, 'eq', q{},
111 'format_sqldatetime formats undef as empty string' );
113 $formatted = format_sqlduedatetime( '2011-06-16 12:00:07', 'metric', '24hr' );
117 'format_sqlduedatetime conversion for hourly loans'
120 $formatted = format_sqlduedatetime( '2011-06-16 23:59:07', 'metric', '24hr' );
121 cmp_ok( $formatted, 'eq', '16/06/2011',
122 'format_sqlduedatetime conversion for daily loans' );