Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Calendar] Handle version timezone #58

Merged
merged 1 commit into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/TimeZoneUtil.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
valentinbonneaud marked this conversation as resolved.
Show resolved Hide resolved
{
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
Original file line number Diff line number Diff line change
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',
];
}
}