Subject: | Calculations fail for certain timezones and dates |
Hello,
We've hit an issue with the date calculations and certain time zones. For following example script:
use DateTime;
use DateTime::BusinessHours;
my $timeZoneID = 'America/Sao_Paulo';
my $datetimeReached = '20181004065112';
my $tsLocal = '20181104120615';
my $reached = DateTime->new( year => 2018, month => 10, day => 4, hour => 6, minute => 51, second => 0, time_zone => $timeZoneID );
my $current = DateTime->new( year => 2018, month => 11, day => 4, hour => 12, minute => 6, second => 0, time_zone => $timeZoneID );
my $delta = DateTime::BusinessHours->new(
datetime1 => $reached,
datetime2 => $current,
worktiming => [ 8, 17 ], # 8am to 5pm
weekends => [ 6, 7 ] # Saturday and Sunday
);
my $deltaHours = $delta->gethours;
Generates the error:
Invalid local time for date in time zone: America/Sao_Paulo
This is because America/Sao_Paulo does not have midnight on it's DST change. Meaning the truncate calls from _calculate fail. A simple fix is to do the calculations from the floating timezone:
< my $start = $d1->clone->truncate( to => 'day' );
< my $end = $d2->clone->truncate( to => 'day' );
Show quoted text
> my $start = $d1->clone->set_time_zone('floating')->truncate( to => 'day' );
> my $end = $d2->clone->set_time_zone('floating')->truncate( to => 'day' );
Please let me know if you have any questions.