Skip to content

Commit

Permalink
Fix customized timezone guesser (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
giuseppe-arcuti authored Jul 6, 2022
1 parent 7edff03 commit 665b0ad
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
17 changes: 14 additions & 3 deletions lib/TimezoneGuesser/GuessFromCustomizedTimeZone.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,29 @@ public function guess(VTimeZone $vtimezone, bool $failIfUncertain = false): ?Dat
$timezones = DateTimeZone::listIdentifiers();
$standard = $vtimezone->STANDARD;
$daylight = $vtimezone->DAYLIGHT;
if (!$standard) {
return null;
}

$standardOffset = $standard->TZOFFSETTO;
if (!$standardOffset) {
return null;
}
$standardOffset = $standardOffset->getValue();

$standardOffset = $standard->TZOFFSETTO->getValue();
$standardRRule = $daylight ? $standard->RRULE->getValue() : 'FREQ=DAILY';
$standardRRule = $standard->RRULE ? $standard->RRULE->getValue() : 'FREQ=DAILY';
// The guess will not be perfectly matched since we use the timezone data of the current year
// It might be wrong if the timezone data changed in the past
$year = (new DateTimeImmutable('now'))->format('Y');
$start = new DateTimeImmutable($year . '-01-01');
$standardIterator = new RRuleIterator($standardRRule, $start);
$standardIterator->next();

if ($daylight && !$daylight->TZOFFSETTO) {
$daylight = null;
}
$daylightOffset = $daylight ? $daylight->TZOFFSETTO->getValue() : '';
$daylightRRule = $daylight ? $daylight->RRULE->getValue() : '';
$daylightRRule = $daylight ? ($daylight->RRULE ? $daylight->RRULE->getValue() : 'FREQ=DAILY') : '';
$daylightIterator = $daylight ? new RRuleIterator($daylightRRule, $standardIterator->current()) : null;
$daylightIterator && $daylightIterator->next();

Expand Down
2 changes: 1 addition & 1 deletion tests/VObject/TimeZoneUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ public function testCustomizedTimeZoneWithoutDaylight()
{
$ics = $this->getCustomizedICS();
$tz = TimeZoneUtil::getTimeZone('Customized Time Zone', Reader::read($ics));
$this->assertNotSame('Customized Time Zone', $tz->getName());
$this->assertSame('Asia/Brunei', $tz->getName());
$start = new \DateTimeImmutable('2022-04-25');
$this->assertSame(8 * 60 * 60, $tz->getOffset($start));
}
Expand Down

0 comments on commit 665b0ad

Please sign in to comment.