Skip to content

Commit

Permalink
Fix customized timezone guesser
Browse files Browse the repository at this point in the history
  • Loading branch information
Ren Xie Liu committed May 8, 2024
1 parent a9edab8 commit d4e758f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/TimezoneGuesser/GuessFromCustomizedTimeZone.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ public function guess(VTimeZone $vtimezone, ?bool $failIfUncertain = false): ?\D
$daylightIterator = $daylight ? new RRuleIterator($daylightRRule, $standardIterator->current()) : null;
$daylightIterator && $daylightIterator->next();

$day = 24 * 60 * 60;
foreach ($timezones as $timezone) {
$tz = new \DateTimeZone($timezone);
// check standard
$timestamp = $standardIterator->current()->getTimestamp();
$transitions = $tz->getTransitions($timestamp, $timestamp + 1);
$transitions = $tz->getTransitions($timestamp + $day, $timestamp + $day + 1);
if (empty($transitions)) {
continue;
}
Expand All @@ -66,7 +67,7 @@ public function guess(VTimeZone $vtimezone, ?bool $failIfUncertain = false): ?\D

// check daylight
$timestamp = $daylightIterator->current()->getTimestamp();
$transitions = $tz->getTransitions($timestamp, $timestamp + 1);
$transitions = $tz->getTransitions($timestamp + $day, $timestamp + $day + 1);
if (empty($transitions)) {
continue;
}
Expand Down
45 changes: 44 additions & 1 deletion tests/VObject/TimeZoneUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -671,11 +671,54 @@ public function testCustomizedTimeZone(): void
self::assertSame(11 * 60 * 60, $tz->getOffset($start));
}

public function testCustomizedTimeZone2(): void
{
$ics = <<<ICS
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//ical.marudot.com//iCal Event Maker
X-WR-CALNAME:test
NAME:test
CALSCALE:GREGORIAN
BEGIN:VTIMEZONE
TZID:Customized Time Zone
BEGIN:STANDARD
DTSTART:16010101T030000
TZOFFSETFROM:+0200
TZOFFSETTO:+0100
RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=-1SU;BYMONTH=10
END:STANDARD
BEGIN:DAYLIGHT
DTSTART:16010101T020000
TZOFFSETFROM:+0100
TZOFFSETTO:+0200
RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=-1SU;BYMONTH=3
END:DAYLIGHT
END:VTIMEZONE
BEGIN:VEVENT
DTSTAMP:20200907T032724Z
UID:[email protected]
DTSTART;TZID=Customized Time Zone:20210611T110000
DTEND;TZID=Customized Time Zone:20210611T113000
SUMMARY:customized time zone
END:VEVENT
END:VCALENDAR
ICS;

$tz = TimeZoneUtil::getTimeZone('Customized Time Zone', Reader::read($ics));
self::assertNotSame('Customized Time Zone', $tz->getName());
$start = new \DateTimeImmutable('2022-04-25');
self::assertSame(2 * 60 * 60, $tz->getOffset($start));

$start = new \DateTimeImmutable('2022-11-10');
self::assertSame(60 * 60, $tz->getOffset($start));
}

public function testCustomizedTimeZoneWithoutDaylight(): void
{
$ics = $this->getCustomizedICS();
$tz = TimeZoneUtil::getTimeZone('Customized Time Zone', Reader::read($ics));
self::assertSame('Asia/Brunei', $tz->getName());
self::assertSame('Antarctica/Casey', $tz->getName());
$start = new \DateTimeImmutable('2022-04-25');
self::assertSame(8 * 60 * 60, $tz->getOffset($start));
}
Expand Down

0 comments on commit d4e758f

Please sign in to comment.