Skip to content

Commit

Permalink
Add LowercaseTimezoneIdentifier (#50)
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 authored Apr 22, 2022
1 parent 13cbb28 commit f2d3f95
Show file tree
Hide file tree
Showing 3 changed files with 56 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 @@ -9,6 +9,7 @@
use Sabre\VObject\TimezoneGuesser\FindFromTimezoneMap;
use Sabre\VObject\TimezoneGuesser\GuessFromLicEntry;
use Sabre\VObject\TimezoneGuesser\GuessFromMsTzId;
use Sabre\VObject\TimezoneGuesser\LowercaseTimezoneIdentifier;
use Sabre\VObject\TimezoneGuesser\TimezoneFinder;
use Sabre\VObject\TimezoneGuesser\TimezoneGuesser;

Expand Down Expand Up @@ -40,6 +41,7 @@ private function __construct()
$this->addFinder('tzid', new FindFromTimezoneIdentifier());
$this->addFinder('tzmap', new FindFromTimezoneMap());
$this->addFinder('offset', new FindFromOffset());
$this->addFinder('lowercase', new LowercaseTimezoneIdentifier());
}

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

declare(strict_types=1);

namespace Sabre\VObject\TimezoneGuesser;

use DateTimeZone;

class LowercaseTimezoneIdentifier implements TimezoneFinder
{
public function find(string $tzid, bool $failIfUncertain = false): ?DateTimeZone
{
foreach (DateTimeZone::listIdentifiers() as $timezone) {
if (strtolower($tzid) === strtolower($timezone)) {
return new DateTimeZone($timezone);
}
}

return null;
}
}
33 changes: 33 additions & 0 deletions tests/VObject/TimeZoneUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,4 +463,37 @@ public function offsetTimeZoneProvider(): iterable
'expected' => 'America/Lima',
];
}

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

public function letterCaseTimeZoneProvider(): iterable
{
yield 'case 1' => [
'origin' => 'Europe/paris',
'expected' => 'Europe/Paris',
];

yield 'case 2' => [
'origin' => 'europe/paris',
'expected' => 'Europe/Paris',
];

yield 'case 3' => [
'origin' => 'Europe/pAris',
'expected' => 'Europe/Paris',
];

yield 'case 4' => [
'origin' => 'Asia/taipei',
'expected' => 'Asia/Taipei',
];
}
}

0 comments on commit f2d3f95

Please sign in to comment.