Bug 19176: Compare the number of seconds when comparing dates in tests
[koha.git] / t / Test / Dates.t
1 use Modern::Perl;
2 use Test::More tests => 7;
3 use t::lib::Dates;
4 use Koha::DateUtils qw( dt_from_string );
5
6 my $date_1 = '2017-01-01 01:00:00';
7 my $date_2 = '2018-02-02 01:00:00';
8 my $dt_1   = dt_from_string($date_1);
9 my $dt_2   = dt_from_string($date_2);
10
11 is( t::lib::Dates::compare( $dt_1, $dt_2 ), -1, '2017 is before 2018' );
12 is( t::lib::Dates::compare( $dt_2, $dt_1 ), 1,  '2018 is after 2017' );
13
14 is( t::lib::Dates::compare( $date_1, $date_2 ), -1, '2017 is before 2018 (strings comparison)' );
15 is( t::lib::Dates::compare( $date_2, $date_1 ), 1,  '2018 is after 2017 (strings comparison)' );
16
17 my $dt_3 = $dt_1->clone->subtract( seconds => 59 );
18 is( t::lib::Dates::compare( $dt_1, $dt_3 ),
19     0, 'If there is less than 1min, the dates are considered identicals' );
20 is( t::lib::Dates::compare( $dt_3, $dt_1 ),
21     0, 'If there is less than 1min, the dates are considered identicals' );
22
23 $dt_1->set_time_zone('+0000');
24 $dt_3 = $dt_1->clone->set_time_zone('+0400');
25
26 is( t::lib::Dates::compare( $dt_1, $dt_3 ), -1, "Compare different timezones" );