File: | t/DateUtils.t |
Coverage: | 100.0% |
line | stmt | bran | cond | sub | time | code |
---|---|---|---|---|---|---|
1 | 1 1 1 | 1.33283523210362e+15 2 36 | use strict; | |||
2 | 1 1 1 | 5 1 27 | use warnings; | |||
3 | 1 1 1 1 1 1 | 70 49 18 874 41 8 | use 5.010; | |||
4 | 1 1 1 | 224 174933 43 | use DateTime; | |||
5 | 1 1 1 | 16 6 26 | use DateTime::TimeZone; | |||
6 | ||||||
7 | 1 1 1 | 242 3 9 | use C4::Context; | |||
8 | 1 1 1 | 163 21075 13 | use Test::More tests => 25; | |||
9 | ||||||
10 | 1 | 1299 | BEGIN { use_ok('Koha::DateUtils'); } | |||
11 | ||||||
12 | 1 | 393579 | my $tz = C4::Context->tz; | |||
13 | ||||||
14 | 1 | 0 | isa_ok( $tz, 'DateTime::TimeZone', 'Context returns timezone object' ); | |||
15 | ||||||
16 | 1 | 583 | my $testdate_iso = '2011-06-16'; # Bloomsday 2011 | |||
17 | 1 | 8 | my $dt = dt_from_string( $testdate_iso, 'iso' ); | |||
18 | ||||||
19 | 1 | 1448 | isa_ok( $dt, 'DateTime', 'dt_from_string returns a DateTime object' ); | |||
20 | ||||||
21 | 1 | 551 | cmp_ok( $dt->ymd(), 'eq', $testdate_iso, 'Returned object matches input' ); | |||
22 | ||||||
23 | 1 | 604 | $dt->set_hour(12); | |||
24 | 1 | 907 | $dt->set_minute(0); | |||
25 | ||||||
26 | 1 | 1120 | my $date_string = output_pref( $dt, 'iso' ); | |||
27 | 1 | 96 | cmp_ok $date_string, 'eq', '2011-06-16 12:00', 'iso output'; | |||
28 | ||||||
29 | 1 | 720 | $date_string = output_pref( $dt, 'us' ); | |||
30 | 1 | 103 | cmp_ok $date_string, 'eq', '06/16/2011 12:00', 'us output'; | |||
31 | ||||||
32 | # metric should return the French Revolutionary Calendar Really | |||||
33 | 1 | 532 | $date_string = output_pref( $dt, 'metric' ); | |||
34 | 1 | 93 | cmp_ok $date_string, 'eq', '16/06/2011 12:00', 'metric output'; | |||
35 | ||||||
36 | 1 | 496 | $date_string = output_pref_due( $dt, 'metric' ); | |||
37 | 1 | 6 | cmp_ok $date_string, 'eq', '16/06/2011 12:00', | |||
38 | 'output_pref_due preserves non midnight HH:SS'; | |||||
39 | ||||||
40 | 1 | 317 | $dt->set_hour(23); | |||
41 | 1 | 514 | $dt->set_minute(59); | |||
42 | 1 | 606 | $date_string = output_pref_due( $dt, 'metric' ); | |||
43 | 1 | 8 | cmp_ok $date_string, 'eq', '16/06/2011', | |||
44 | 'output_pref_due truncates HH:SS at midnight'; | |||||
45 | ||||||
46 | 1 | 563 | my $dear_dirty_dublin = DateTime::TimeZone->new( name => 'Europe/Dublin' ); | |||
47 | 1 | 129937 | my $new_dt = dt_from_string( '16/06/2011', 'metric', $dear_dirty_dublin ); | |||
48 | 1 | 1124 | isa_ok( $new_dt, 'DateTime', 'Create DateTime with different timezone' ); | |||
49 | 1 | 338 | cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso, | |||
50 | 'Returned Dublin object matches input' ); | |||||
51 | ||||||
52 | 1 | 655 | $new_dt = dt_from_string( '2011-06-16 12:00', 'sql' ); | |||
53 | 1 | 732 | isa_ok( $new_dt, 'DateTime', 'Create DateTime from (mysql) sql' ); | |||
54 | 1 | 552 | cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso, 'sql returns correct date' ); | |||
55 | ||||||
56 | 1 | 602 | $new_dt = dt_from_string( $dt, 'iso' ); | |||
57 | 1 | 11 | isa_ok( $new_dt, 'DateTime', 'Passed a DateTime dt_from_string returns it' ); | |||
58 | ||||||
59 | # C4::Dates allowed 00th of the month | |||||
60 | ||||||
61 | 1 | 476 | my $ymd = '2012-01-01'; | |||
62 | 1 | 7 | my $dt0 = dt_from_string( '00/01/2012', 'metric' ); | |||
63 | 1 | 1008 | isa_ok( $dt0, 'DateTime', | |||
64 | 'dt_from_string returns a DateTime object passed a zero metric day' ); | |||||
65 | 1 | 485 | cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects metric day 0' ); | |||
66 | ||||||
67 | 1 | 525 | $dt0 = dt_from_string( '01/00/2012', 'us' ); | |||
68 | 1 | 803 | isa_ok( $dt0, 'DateTime', | |||
69 | 'dt_from_string returns a DateTime object passed a zero us day' ); | |||||
70 | 1 | 862 | cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects us day 0' ); | |||
71 | ||||||
72 | 1 | 651 | $dt0 = dt_from_string( '2012-01-00', 'iso' ); | |||
73 | 1 | 901 | isa_ok( $dt0, 'DateTime', | |||
74 | 'dt_from_string returns a DateTime object passed a zero iso day' ); | |||||
75 | 1 | 552 | cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects iso day 0' ); | |||
76 | ||||||
77 | # Return undef if passed mysql 0 dates | |||||
78 | 1 | 565 | $dt0 = dt_from_string( '0000-00-00', 'iso' ); | |||
79 | 1 | 16 | is( $dt0, undef, "undefined returned for 0 iso date" ); | |||
80 | ||||||
81 | 1 | 494 | my $formatted = format_sqldatetime( '2011-06-16 12:00:07', 'metric' ); | |||
82 | 1 | 95 | cmp_ok( $formatted, 'eq', '16/06/2011 12:00', 'format_sqldatetime conversion' ); | |||
83 | ||||||
84 | 1 | 537 | $formatted = format_sqldatetime( undef, 'metric' ); | |||
85 | 1 | 6 | cmp_ok( $formatted, 'eq', q{}, | |||
86 | 'format_sqldatetime formats undef as empty string' ); | |||||
87 | ||||||
88 | 1 | 504 | $formatted = format_sqlduedatetime( '2011-06-16 12:00:07', 'metric' ); | |||
89 | 1 | 0 | cmp_ok( | |||
90 | $formatted, 'eq', | |||||
91 | '16/06/2011 12:00', | |||||
92 | 'format_sqlduedatetime conversion for hourly loans' | |||||
93 | ); | |||||
94 | ||||||
95 | 1 | 243 | $formatted = format_sqlduedatetime( '2011-06-16 23:59:07', 'metric' ); | |||
96 | 1 | 7 | cmp_ok( $formatted, 'eq', '16/06/2011', | |||
97 | 'format_sqlduedatetime conversion for daily loans' ); |