Skip to content

Commit

Permalink
[Calendar] Handle version timezone (#58)
Browse files Browse the repository at this point in the history
Co-authored-by: Ren Xie Liu <ren.xie@proton.ch>
  • Loading branch information
liurxliu and Ren Xie Liu committed May 26, 2022
1 parent c3ac3c8 commit 623bd9a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/TimeZoneUtil.php
Expand Up @@ -4,6 +4,7 @@

use DateTimeZone;
use InvalidArgumentException;
use Sabre\VObject\TimezoneGuesser\FindFromMzVersionTimezone;
use Sabre\VObject\TimezoneGuesser\FindFromOffset;
use Sabre\VObject\TimezoneGuesser\FindFromOutlookCities;
use Sabre\VObject\TimezoneGuesser\FindFromTimezoneIdentifier;
Expand Down Expand Up @@ -44,6 +45,7 @@ private function __construct()
$this->addFinder('offset', new FindFromOffset());
$this->addFinder('lowercase', new LowercaseTimezoneIdentifier());
$this->addFinder('outlookCities', new FindFromOutlookCities());
$this->addFinder('version', new FindFromMzVersionTimezone());
}

private static function getInstance(): self
Expand Down
35 changes: 35 additions & 0 deletions lib/TimezoneGuesser/FindFromMzVersionTimezone.php
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Sabre\VObject\TimezoneGuesser;

use DateTimeZone;
use Sabre\VObject\TimeZoneUtil;

/**
* Try to ignore the trailing number of the Microsoft timezone.
*
* For example: Eastern Standard Time 1 => Eastern Standard Time
*/
class FindFromMzVersionTimezone implements TimezoneFinder
{
public function find(string $tzid, bool $failIfUncertain = false): ?DateTimeZone
{
if (strlen($tzid) < 1) {
return null;
}

$trailingChar = (int) $tzid[strlen($tzid)-1];
if ($trailingChar <= 9 && $trailingChar >= 1) {
$tz = TimeZoneUtil::getTimeZone(substr($tzid, 0, strrpos($tzid, ' ')));
if ($tz->getName() === 'UTC') {
return null;
}

return $tz;
}

return null;
}
}
25 changes: 25 additions & 0 deletions tests/VObject/TimeZoneUtilTest.php
Expand Up @@ -533,4 +533,29 @@ public function outlookCitiesProvider(): iterable
'expected' => 'UTC',
];
}

/**
* @dataProvider versionTzProvider
*/
public function testVersionTz(string $origin, bool $failIfUncertain, string $expected)
{
$tz = TimeZoneUtil::getTimeZone($origin, null, $failIfUncertain);
$ex = new \DateTimeZone($expected);
$this->assertEquals($ex->getName(), $tz->getName());
}

public function versionTzProvider(): iterable
{
yield 'case 1' => [
'origin' => 'Eastern Standard Time 1',
'failIfUncertain' => true,
'expected' => 'America/New_York',
];

yield 'case 2' => [
'origin' => 'Eastern Standard Time 2',
'failIfUncertain' => true,
'expected' => 'America/New_York',
];
}
}

0 comments on commit 623bd9a

Please sign in to comment.