From bae8917634e50585f411db5bafde828debe51638 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Tue, 17 Mar 2020 10:22:16 +0000 Subject: [PATCH] Bug 24850: Always return 'instance' local time This patch adds handling to ensure we always convert a passed in time to the instance configured timezone.. --- Koha/DateUtils.pm | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Koha/DateUtils.pm b/Koha/DateUtils.pm index 594caad046..e7923fcca5 100644 --- a/Koha/DateUtils.pm +++ b/Koha/DateUtils.pm @@ -54,7 +54,8 @@ sub dt_from_string { return if $date_string and $date_string =~ m|^0000-0|; my $do_fallback = defined($date_format) ? 0 : 1; - $tz = C4::Context->tz unless $tz;; + my $server_tz = C4::Context->tz; + $tz = C4::Context->tz unless $tz; return DateTime->now( time_zone => $tz ) unless $date_string; @@ -120,6 +121,9 @@ sub dt_from_string { (?\d{2}) (\.\d{1,3})?(([Zz]$)|((?[\+|\-])(?[01][0-9]|2[0-3]):(?[0-5][0-9]))) /xms; + + # Default to UTC (when 'Z' is passed) for inbound timezone. + $tz = DateTime::TimeZone->new( name => 'UTC' ); } elsif ( $date_format eq 'iso' or $date_format eq 'sql' ) { # iso or sql format are yyyy-dd-mm[ hh:mm:ss]" @@ -156,6 +160,7 @@ sub dt_from_string { second => $+{second}, ); if ( $+{offset} ) { + # If offset given, set inbound timezone using it. $tz = DateTime::TimeZone->new( name => $+{offset} . $+{hours} . $+{minutes} ); } } elsif ( $do_fallback && $date_string =~ $fallback_re ) { @@ -195,6 +200,7 @@ sub dt_from_string { ( $dt_params{year} < 9999 ? ( time_zone => $tz ) : () ), ); } + $dt->set_time_zone($server_tz); return $dt; } -- 2.20.1