-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Intl] Add Timezones #28831
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
Merged
Merged
[Intl] Add Timezones #28831
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Intl] Add Timezones
- Loading branch information
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
src/Symfony/Component/Intl/Data/Generator/TimezoneDataGenerator.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Intl\Data\Generator; | ||
|
||
use Symfony\Component\Intl\Data\Bundle\Compiler\GenrbCompiler; | ||
use Symfony\Component\Intl\Data\Bundle\Reader\BundleReaderInterface; | ||
use Symfony\Component\Intl\Data\Util\ArrayAccessibleResourceBundle; | ||
use Symfony\Component\Intl\Data\Util\LocaleScanner; | ||
|
||
/** | ||
* The rule for compiling the zone bundle. | ||
* | ||
* @author Roland Franssen <franssen.roland@gmail.com> | ||
* | ||
* @internal | ||
*/ | ||
class TimezoneDataGenerator extends AbstractDataGenerator | ||
{ | ||
/** | ||
* Collects all available zone codes. | ||
* | ||
* @var string[] | ||
*/ | ||
private $zoneCodes = []; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function scanLocales(LocaleScanner $scanner, $sourceDir) | ||
{ | ||
return $scanner->scanLocales($sourceDir.'/zone'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function compileTemporaryBundles(GenrbCompiler $compiler, $sourceDir, $tempDir) | ||
{ | ||
$compiler->compile($sourceDir.'/zone', $tempDir); | ||
$compiler->compile($sourceDir.'/misc/timezoneTypes.txt', $tempDir); | ||
$compiler->compile($sourceDir.'/misc/metaZones.txt', $tempDir); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function preGenerate() | ||
{ | ||
$this->zoneCodes = []; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function generateDataForLocale(BundleReaderInterface $reader, $tempDir, $displayLocale) | ||
{ | ||
$localeBundle = $reader->read($tempDir, $displayLocale); | ||
|
||
if (isset($localeBundle['zoneStrings']) && null !== $localeBundle['zoneStrings']) { | ||
$data = [ | ||
'Version' => $localeBundle['Version'], | ||
'Names' => self::generateZones( | ||
$reader->read($tempDir, 'timezoneTypes'), | ||
$reader->read($tempDir, 'metaZones'), | ||
$reader->read($tempDir, 'root'), | ||
$localeBundle | ||
), | ||
]; | ||
|
||
$this->zoneCodes = array_merge($this->zoneCodes, array_keys($data['Names'])); | ||
|
||
return $data; | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function generateDataForRoot(BundleReaderInterface $reader, $tempDir) | ||
{ | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function generateDataForMeta(BundleReaderInterface $reader, $tempDir) | ||
{ | ||
$rootBundle = $reader->read($tempDir, 'root'); | ||
|
||
$this->zoneCodes = array_unique($this->zoneCodes); | ||
|
||
sort($this->zoneCodes); | ||
|
||
$data = [ | ||
'Version' => $rootBundle['Version'], | ||
'Zones' => $this->zoneCodes, | ||
]; | ||
|
||
return $data; | ||
} | ||
|
||
private static function generateZones(ArrayAccessibleResourceBundle $typeBundle, ArrayAccessibleResourceBundle $metaBundle, ArrayAccessibleResourceBundle $rootBundle, ArrayAccessibleResourceBundle $localeBundle): array | ||
{ | ||
$available = []; | ||
foreach ($typeBundle['typeMap']['timezone'] as $zone => $_) { | ||
if ('Etc:Unknown' === $zone || preg_match('~^Etc:GMT[-+]\d+$~', $zone)) { | ||
continue; | ||
} | ||
|
||
$available[$zone] = true; | ||
} | ||
|
||
$metazones = []; | ||
foreach ($metaBundle['metazoneInfo'] as $zone => $info) { | ||
foreach ($info as $metazone) { | ||
$metazones[$zone] = $metazone->get(0); | ||
} | ||
} | ||
|
||
$zones = []; | ||
foreach (array_keys($available) as $zone) { | ||
// lg: long generic, e.g. "Central European Time" | ||
// ls: long specific (not DST), e.g. "Central European Standard Time" | ||
// ld: long DST, e.g. "Central European Summer Time" | ||
// ec: example city, e.g. "Amsterdam" | ||
$name = $localeBundle['zoneStrings'][$zone]['lg'] ?? $rootBundle['zoneStrings'][$zone]['lg'] ?? $localeBundle['zoneStrings'][$zone]['ls'] ?? $rootBundle['zoneStrings'][$zone]['ls'] ?? null; | ||
$city = $localeBundle['zoneStrings'][$zone]['ec'] ?? $rootBundle['zoneStrings'][$zone]['ec'] ?? null; | ||
|
||
if (null === $name && isset($metazones[$zone])) { | ||
$meta = 'meta:'.$metazones[$zone]; | ||
$name = $localeBundle['zoneStrings'][$meta]['lg'] ?? $rootBundle['zoneStrings'][$meta]['lg'] ?? $localeBundle['zoneStrings'][$meta]['ls'] ?? $rootBundle['zoneStrings'][$meta]['ls'] ?? null; | ||
} | ||
if (null === $city && 0 !== strrpos($zone, 'Etc:') && false !== $i = strrpos($zone, ':')) { | ||
$city = str_replace('_', ' ', substr($zone, $i + 1)); | ||
} | ||
if (null === $name) { | ||
continue; | ||
} | ||
if (null !== $city) { | ||
$name .= ' ('.$city.')'; | ||
} | ||
|
||
$id = str_replace(':', '/', $zone); | ||
$zones[$id] = $name; | ||
} | ||
|
||
return $zones; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.