Skip to content

Commit

Permalink
Issue #238: implement the UTC-workaround for PreviousEventGet()
Browse files Browse the repository at this point in the history
Just like in NextEventGet()
  • Loading branch information
bschmalhofer committed Aug 19, 2020
1 parent 4eff83b commit 17fd3ed
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions Kernel/System/CronEvent.pm
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ sub NextEventGet {
}

my $StartDateTime = $Param{StartDateTime} || $Kernel::OM->Create('Kernel::System::DateTime');

return if !$StartDateTime;

# Calculations are only made in UTC time zone to prevent errors with times that
Expand All @@ -106,6 +107,7 @@ sub NextEventGet {
);

return if !$CronObject;

my ( $Sec, $Min, $Hour, $Day, $Month, $Year ) = $CronObject->nextEvent();

my $EventDateTime = $Kernel::OM->Create(
Expand Down Expand Up @@ -162,6 +164,7 @@ sub NextEventList {
}

my $StartDateTime = $Param{StartDateTime} || $Kernel::OM->Create('Kernel::System::DateTime');

return if !$StartDateTime;

if ( $StartDateTime > $Param{StopDateTime} ) {
Expand Down Expand Up @@ -239,8 +242,22 @@ sub PreviousEventGet {
}

my $StartDateTime = $Param{StartDateTime} || $Kernel::OM->Create('Kernel::System::DateTime');

return if !$StartDateTime;

# Calculations are only made in UTC time zone to prevent errors with times that
# would not exist in the given time zone (e. g. on/around daylight saving time switch).
# CPAN DateTime fails if trying to create a object of a non-existing
# time in the given time zone. Converting it to UTC and back has the desired effect.
my $OTOBOTimeZone = $StartDateTime->OTOBOTimeZoneGet();
my $TimeZoneChanged;
if ( $OTOBOTimeZone ne 'UTC' ) {
$StartDateTime->ToTimeZone(
TimeZone => 'UTC'
);
$TimeZoneChanged = 1;
}

# init cron object
my $CronObject = $Self->_Init(
Schedule => $Param{Schedule},
Expand All @@ -254,15 +271,22 @@ sub PreviousEventGet {
my $EventDateTime = $Kernel::OM->Create(
'Kernel::System::DateTime',
ObjectParams => {
Year => $Year + 1900,
Month => $Month + 1,
Day => $Day,
Hour => $Hour,
Minute => $Min,
Second => $Sec,
Year => $Year + 1900,
Month => $Month + 1,
Day => $Day,
Hour => $Hour,
Minute => $Min,
Second => $Sec,
TimeZone => 'UTC'
},
);

if ($TimeZoneChanged) {
$EventDateTime->ToTimeZone(
TimeZone => $OTOBOTimeZone
);
}

return $EventDateTime->ToString();
}

Expand Down

0 comments on commit 17fd3ed

Please sign in to comment.