Skip to content

Commit

Permalink
[Calendar] Add FindFromOutlookCities timezone finder
Browse files Browse the repository at this point in the history
  • Loading branch information
Ren Xie Liu committed Apr 25, 2022
1 parent 5e4d88d commit a0f5429
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/TimeZoneUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use DateTimeZone;
use InvalidArgumentException;
use Sabre\VObject\TimezoneGuesser\FindFromOffset;
use Sabre\VObject\TimezoneGuesser\FindFromOutlookCities;
use Sabre\VObject\TimezoneGuesser\FindFromTimezoneIdentifier;
use Sabre\VObject\TimezoneGuesser\FindFromTimezoneMap;
use Sabre\VObject\TimezoneGuesser\GuessFromLicEntry;
Expand Down Expand Up @@ -42,6 +43,7 @@ private function __construct()
$this->addFinder('tzmap', new FindFromTimezoneMap());
$this->addFinder('offset', new FindFromOffset());
$this->addFinder('lowercase', new LowercaseTimezoneIdentifier());
$this->addFinder('outlookCities', new FindFromOutlookCities());
}

private static function getInstance(): self
Expand Down
41 changes: 41 additions & 0 deletions lib/TimezoneGuesser/FindFromOutlookCities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Sabre\VObject\TimezoneGuesser;

use DateTimeZone;

class FindFromOutlookCities implements TimezoneFinder
{
/**
* Example: TZID:(UTC+01:00) Bruxelles\, København\, Madrid\, Paris
*/
public function find(string $tzid, bool $failIfUncertain = false): ?DateTimeZone
{
$values = explode(' ', $tzid);
if (count($values) === 1) {
return null;
}

unset($values[0]);
$cities = implode('', $values);
$cities = explode('\,', $cities);

if (count($cities) === 1) {
return null;
}

$tzIdentifiers = DateTimeZone::listIdentifiers();

foreach ($cities as $city) {
foreach ($tzIdentifiers as $tzIdentifier) {
if (str_contains(strtolower($tzIdentifier), strtolower($city))) {
return new DateTimeZone($tzIdentifier);
}
}
}

return null;
}
}
31 changes: 31 additions & 0 deletions tests/VObject/TimeZoneUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,4 +496,35 @@ public function letterCaseTimeZoneProvider(): iterable
'expected' => 'Asia/Taipei',
];
}

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

public function outlookCitiesProvider(): iterable
{
yield 'case 1' => [
'origin' => 'TZID:(UTC+01:00) Bruxelles\, København\, Madrid\, Paris',
'failIfUncertain' => true,
'expected' => 'Europe/Madrid',
];

yield 'case 2' => [
'origin' => 'TZID:(UTC+01:00) Bruxelles, København, Madrid, Paris',
'failIfUncertain' => false,
'expected' => 'UTC',
];

yield 'case 3' => [
'origin' => 'TZID:(UTC+01:00)Bruxelles\, København\, Madrid\, Paris',
'failIfUncertain' => true,
'expected' => 'Europe/Madrid',
];
}
}

0 comments on commit a0f5429

Please sign in to comment.