5 use DateTime::TimeZone;
8 use Test::More tests => 30;
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' );
26 my $date_string = output_pref( $dt, 'iso', '24hr' );
27 cmp_ok $date_string, 'eq', '2011-06-16 12:00', 'iso output';
29 $date_string = output_pref( $dt, 'iso', '12hr' );
30 cmp_ok $date_string, 'eq', '2011-06-16 12:00 PM', 'iso output 12hr';
32 # "notime" doesn't actually mean anything in this context, but we
33 # can't pass undef or output_pref will try to access the database
34 $date_string = output_pref( $dt, 'iso', 'notime', 1 );
35 cmp_ok $date_string, 'eq', '2011-06-16', 'iso output (date only)';
37 $date_string = output_pref( $dt, 'us', '24hr' );
38 cmp_ok $date_string, 'eq', '06/16/2011 12:00', 'us output';
40 $date_string = output_pref( $dt, 'us', '12hr' );
41 cmp_ok $date_string, 'eq', '06/16/2011 12:00 PM', 'us output 12hr';
43 $date_string = output_pref( $dt, 'us', 'notime', 1 );
44 cmp_ok $date_string, 'eq', '06/16/2011', 'us output (date only)';
46 # metric should return the French Revolutionary Calendar Really
47 $date_string = output_pref( $dt, 'metric', '24hr' );
48 cmp_ok $date_string, 'eq', '16/06/2011 12:00', 'metric output';
50 $date_string = output_pref( $dt, 'metric', 'notime', 1 );
51 cmp_ok $date_string, 'eq', '16/06/2011', 'metric output (date only)';
53 $date_string = output_pref_due( $dt, 'metric', '24hr' );
54 cmp_ok $date_string, 'eq', '16/06/2011 12:00',
55 'output_pref_due preserves non midnight HH:SS';
59 $date_string = output_pref_due( $dt, 'metric', '24hr' );
60 cmp_ok $date_string, 'eq', '16/06/2011',
61 'output_pref_due truncates HH:SS at midnight';
63 my $dear_dirty_dublin = DateTime::TimeZone->new( name => 'Europe/Dublin' );
64 my $new_dt = dt_from_string( '16/06/2011', 'metric', $dear_dirty_dublin );
65 isa_ok( $new_dt, 'DateTime', 'Create DateTime with different timezone' );
66 cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso,
67 'Returned Dublin object matches input' );
69 $new_dt = dt_from_string( '2011-06-16 12:00', 'sql' );
70 isa_ok( $new_dt, 'DateTime', 'Create DateTime from (mysql) sql' );
71 cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso, 'sql returns correct date' );
73 $new_dt = dt_from_string( $dt, 'iso' );
74 isa_ok( $new_dt, 'DateTime', 'Passed a DateTime dt_from_string returns it' );
76 # C4::Dates allowed 00th of the month
78 my $ymd = '2012-01-01';
79 my $dt0 = dt_from_string( '00/01/2012', 'metric' );
80 isa_ok( $dt0, 'DateTime',
81 'dt_from_string returns a DateTime object passed a zero metric day' );
82 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects metric day 0' );
84 $dt0 = dt_from_string( '01/00/2012', 'us' );
85 isa_ok( $dt0, 'DateTime',
86 'dt_from_string returns a DateTime object passed a zero us day' );
87 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects us day 0' );
89 $dt0 = dt_from_string( '2012-01-00', 'iso' );
90 isa_ok( $dt0, 'DateTime',
91 'dt_from_string returns a DateTime object passed a zero iso day' );
92 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects iso day 0' );
94 # Return undef if passed mysql 0 dates
95 $dt0 = dt_from_string( '0000-00-00', 'iso' );
96 is( $dt0, undef, "undefined returned for 0 iso date" );
98 my $formatted = format_sqldatetime( '2011-06-16 12:00:07', 'metric', '24hr' );
99 cmp_ok( $formatted, 'eq', '16/06/2011 12:00', 'format_sqldatetime conversion' );
101 $formatted = format_sqldatetime( undef, 'metric' );
102 cmp_ok( $formatted, 'eq', q{},
103 'format_sqldatetime formats undef as empty string' );
105 $formatted = format_sqlduedatetime( '2011-06-16 12:00:07', 'metric', '24hr' );
109 'format_sqlduedatetime conversion for hourly loans'
112 $formatted = format_sqlduedatetime( '2011-06-16 23:59:07', 'metric', '24hr' );
113 cmp_ok( $formatted, 'eq', '16/06/2011',
114 'format_sqlduedatetime conversion for daily loans' );