diff --git a/C4/Context.pm b/C4/Context.pm index 1eb5f88902..07e81854bd 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -969,6 +969,21 @@ sub get_versions { return %versions; } +=head2 timezone + + my $C4::Context->timzone + + Returns a timezone code for the instance of Koha + +=cut + +sub timezone { + my $self = shift; + + my $timezone = C4::Context->config('timezone') || $ENV{TZ} || 'local'; + + return $timezone; +} =head2 tz @@ -981,7 +996,8 @@ sub get_versions { sub tz { my $self = shift; if (!defined $context->{tz}) { - $context->{tz} = DateTime::TimeZone->new(name => 'local'); + my $timezone = $self->timezone; + $context->{tz} = DateTime::TimeZone->new(name => $timezone); } return $context->{tz}; } diff --git a/Koha/Database.pm b/Koha/Database.pm index e97c794a45..802bdc5111 100644 --- a/Koha/Database.pm +++ b/Koha/Database.pm @@ -70,7 +70,7 @@ sub _new_schema { my ( %encoding_attr, $encoding_query, $tz_query, $sql_mode_query ); - my $tz = $ENV{TZ}; + my $tz = C4::Context->timezone; if ( $db_driver eq 'mysql' ) { %encoding_attr = ( mysql_enable_utf8 => 1 ); $encoding_query = "set NAMES 'utf8mb4'"; diff --git a/t/timezones.t b/t/timezones.t new file mode 100644 index 0000000000..c5463dab90 --- /dev/null +++ b/t/timezones.t @@ -0,0 +1,25 @@ +use Modern::Perl; + +use C4::Context; + +use Test::More tests => 3; +use t::lib::Mocks; + +$ENV{TZ} = q{}; +t::lib::Mocks::mock_config( 'timezone', q{} ); +is( C4::Context->timezone, 'local', + 'Got local timezone with no env or config timezone set' ); + +$ENV{TZ} = 'Antarctica/Macquarie'; +is( + C4::Context->timezone, + 'Antarctica/Macquarie', + 'Got correct timezone using ENV, overrides local time' +); + +t::lib::Mocks::mock_config( 'timezone', 'Antarctica/South_Pole' ); +is( + C4::Context->timezone, + 'Antarctica/South_Pole', + 'Got correct timezone using config, overrides env' +);