Browse Source

Bug 12669: Centralize the timezone handle into Koha::DateUtils

This patch adds unit tests for the previous changes and centralize the
timezone handle into the Koha::DateUtils module.
Like that the behavior will affect all date manipulations using this
module (should be all dates in Koha).

Signed-off-by: Chris Cormack <chris@bigballofwax.co.nz>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@gmail.com>
3.20.x
Jonathan Druart 10 years ago
committed by Tomas Cohen Arazi
parent
commit
f82064286d
  1. 22
      Koha/DateUtils.pm
  2. 3
      Koha/Template/Plugin/KohaDates.pm
  3. 22
      t/DateUtils.t
  4. 30
      t/db_dependent/Koha_template_plugin_KohaDates.t

22
Koha/DateUtils.pm

@ -58,9 +58,8 @@ sub dt_from_string {
return DateTime::Format::DateParse->parse_datetime($date_string) return DateTime::Format::DateParse->parse_datetime($date_string)
if $date_string and $date_string =~ /^9999-/; if $date_string and $date_string =~ /^9999-/;
if ( !$tz ) { my $dt;
$tz = C4::Context->tz; $tz ||= C4::Context->tz;
}
if ( !$date_format ) { if ( !$date_format ) {
$date_format = C4::Context->preference('dateformat'); $date_format = C4::Context->preference('dateformat');
} }
@ -89,10 +88,21 @@ s/(\d{4})(\d{2})(\d{2})\s+(\d{2})(\d{2})(\d{2})/$1-$2-$3T$4:$5:$6/;
$date_string =~ s/00T/01T/; $date_string =~ s/00T/01T/;
} }
} }
return DateTime::Format::DateParse->parse_datetime( $date_string,
$tz->name() ); $dt = eval {
DateTime::Format::DateParse->parse_datetime( $date_string,
$tz->name() );
};
if ($@) {
$tz = DateTime::TimeZone->new( name => 'floating' );
$dt = DateTime::Format::DateParse->parse_datetime( $date_string,
$tz->name() );
}
} else {
$dt = DateTime->now( time_zone => $tz );
} }
return DateTime->now( time_zone => $tz );
return $dt;
} }

3
Koha/Template/Plugin/KohaDates.pm

@ -30,8 +30,7 @@ sub filter {
return "" unless $text; return "" unless $text;
$config->{with_hours} //= 0; $config->{with_hours} //= 0;
my $tz = DateTime::TimeZone->new(name => 'floating') unless $config->{with_hours}; my $dt = dt_from_string( $text, 'iso' );
my $dt = dt_from_string( $text, 'iso', $tz );
return $config->{as_due_date} ? return $config->{as_due_date} ?
output_pref({ dt => $dt, as_due_date => 1 }) : output_pref({ dt => $dt, as_due_date => 1 }) :

22
t/DateUtils.t

@ -3,7 +3,7 @@ use DateTime;
use DateTime::TimeZone; use DateTime::TimeZone;
use C4::Context; use C4::Context;
use Test::More tests => 42; use Test::More tests => 44;
use Test::MockModule; use Test::MockModule;
use Time::HiRes qw/ gettimeofday /; use Time::HiRes qw/ gettimeofday /;
@ -162,3 +162,23 @@ cmp_ok $date_string, 'eq', '12/11/2013 06:35 PM', 'as_due_date with hours and ti
my $now = DateTime->now; my $now = DateTime->now;
is( dt_from_string, $now, "Without parameter, dt_from_string should return today" ); is( dt_from_string, $now, "Without parameter, dt_from_string should return today" );
$module_context->mock(
'tz',
sub {
return DateTime::TimeZone->new( name => 'Europe/Lisbon' );
}
);
$dt = dt_from_string('1979-04-01');
isa_ok( $dt, 'DateTime', 'dt_from_string should return a DateTime object if a DST is given' );
$module_context->mock(
'tz',
sub {
return DateTime::TimeZone->new( name => 'Europe/Paris' );
}
);
$dt = dt_from_string('2014-03-30 02:00:00');
isa_ok( $dt, 'DateTime', 'dt_from_string should return a DateTime object if a DST is given' );

30
t/db_dependent/Koha_template_plugin_KohaDates.t

@ -1,16 +1,17 @@
#!/usr/bin/perl #!/usr/bin/perl
#
use strict; use Modern::Perl;
use warnings;
use C4::Context; use C4::Context;
use C4::Dates; use C4::Dates;
use Test::More tests => 5; use Test::More tests => 7;
use Test::MockModule;
BEGIN { BEGIN {
use_ok('Koha::Template::Plugin::KohaDates'); use_ok('Koha::Template::Plugin::KohaDates');
} }
my $module_context = new Test::MockModule('C4::Context');
my $date = "1973-05-21"; my $date = "1973-05-21";
my $context = C4::Context->new(); my $context = C4::Context->new();
my $dateobj = C4::Dates->new(); my $dateobj = C4::Dates->new();
@ -18,7 +19,6 @@ my $dateobj = C4::Dates->new();
my $filter = Koha::Template::Plugin::KohaDates->new(); my $filter = Koha::Template::Plugin::KohaDates->new();
ok ($filter, "new()"); ok ($filter, "new()");
$context->set_preference( "dateformat", 'iso' ); $context->set_preference( "dateformat", 'iso' );
$context->clear_syspref_cache(); $context->clear_syspref_cache();
$dateobj->reset_prefformat; $dateobj->reset_prefformat;
@ -40,3 +40,23 @@ $dateobj->reset_prefformat;
$filtered_date = $filter->filter($date); $filtered_date = $filter->filter($date);
is ($filtered_date,'21/05/1973', "metric conversion") or diag ("metric conversion fails $filtered_date"); is ($filtered_date,'21/05/1973', "metric conversion") or diag ("metric conversion fails $filtered_date");
$module_context->mock(
'tz',
sub {
return DateTime::TimeZone->new( name => 'Europe/Lisbon' );
}
);
$filtered_date = $filter->filter('1979-04-01');
is( $filtered_date, '01/04/1979', 'us: dt_from_string should return the valid date if a DST is given' );
$module_context->mock(
'tz',
sub {
return DateTime::TimeZone->new( name => 'Europe/Paris' );
}
);
$filtered_date = $filter->filter('2014-03-30 02:00:00');
is( $filtered_date, '30/03/2014', 'us: dt_from_string should return a DateTime object if a DST is given' );

Loading…
Cancel
Save