Skip to content

Commit

Permalink
Issue #388: fiddle with support for time offsets in timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
bschmalhofer committed Aug 25, 2020
1 parent fb2b03c commit 1a1793d
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions Kernel/System/DateTime.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1750,7 +1750,7 @@ sub TimeStamp2SystemTime {
# Note: see #391 for a better approach
elsif (
$Param{String}
=~ m/(\d{4})-(\d{1,2})-(\d{1,2})T(\d{1,2}):(\d{1,2}):(\d{1,2})(\+|\-)((\d{1,2}):(\d{1,2}))/i
=~ m/(\d{4})-(\d{1,2})-(\d{1,2})T(\d{1,2}):(\d{1,2}):(\d{1,2})(Z|[+-]\d{1,2}:?(?:\d{2})?)?/i
)
{
%DateTimeParams = (
Expand All @@ -1761,6 +1761,24 @@ sub TimeStamp2SystemTime {
Minute => $5,
Second => $6,
);

# DateTime seems to be picky about the format of the offset, e.g. '+00' is not allowed
if ( $7 ) {
my $Offset = $7;
if ( $Offset eq 'Z' ) {
$DateTimeParams{TimeZone} = $Offset;
}
elsif ( $Offset !~ m/:/ ) {
$DateTimeParams{TimeZone} = sprintf '%+03d:00', $Offset;
}
elsif ( $Offset =~ m/^([+-]\d):(.*)/ ) {
# e.g. +5:45
$DateTimeParams{TimeZone} = sprintf '%+03d:%02d', $1, $2;
}
else {
$DateTimeParams{TimeZone} = $Offset;
}
}
}

# match mail time format
Expand All @@ -1786,19 +1804,6 @@ sub TimeStamp2SystemTime {
Second => $8,
); # + $Self->{TimeSecDiff};
}
elsif ( # match yyyy-mm-ddThh:mm:ssZ
$Param{String} =~ /(\d{4})-(\d{1,2})-(\d{1,2})T(\d{1,2}):(\d{1,2}):(\d{1,2})Z$/
)
{
%DateTimeParams = (
Year => $1,
Month => $2,
Day => $3,
Hour => $4,
Minute => $5,
Second => $6,
);
}

# return error
if ( ! %DateTimeParams ) {
Expand Down Expand Up @@ -2070,15 +2075,17 @@ sub _CPANDateTimeObjectCreate {
);
}

my $TimeZone = $Param{TimeZone} || $Self->OTOBOTimeZoneGet();
my $OffsetOrTZ = $Param{TimeZone} || $Self->OTOBOTimeZoneGet();

if ( !$Self->IsTimeZoneValid( TimeZone => $TimeZone ) ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
'Priority' => 'Error',
'Message' => "Invalid value for TimeZone: $TimeZone.",
);
if ( $OffsetOrTZ ne 'Z' && $OffsetOrTZ !~ m/[+-]\d{2}:?(?:\d{2})?/i ) {
if ( !$Self->IsTimeZoneValid( TimeZone => $OffsetOrTZ ) ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
'Priority' => 'Error',
'Message' => "Invalid value for TimeZone: $OffsetOrTZ.",
);

return;
return;
}
}

# Create object from epoch
Expand All @@ -2096,15 +2103,15 @@ sub _CPANDateTimeObjectCreate {
my $CPANDateTimeObject = eval {
DateTime->from_epoch(
epoch => $Param{Epoch},
time_zone => $TimeZone,
time_zone => $OffsetOrTZ,
locale => $Self->{Locale},
);
};

return $CPANDateTimeObject;
}

$Param{TimeZone} = $TimeZone;
$Param{TimeZone} = $OffsetOrTZ;

# Check if date/time params were given, excluding time zone
my $DateTimeParamsGiven = %Param && ( !defined $Param{TimeZone} || keys %Param > 1 );
Expand Down Expand Up @@ -2147,7 +2154,7 @@ sub _CPANDateTimeObjectCreate {
# Create object with current date/time.
my $CPANDateTimeObject = eval {
DateTime->now(
time_zone => $TimeZone,
time_zone => $OffsetOrTZ,
locale => $Self->{Locale},
);
};
Expand Down

0 comments on commit 1a1793d

Please sign in to comment.