Skip to content

Commit eab7611

Browse files
committed
feature #28831 [Intl] Add Timezones (ro0NL)
This PR was squashed before being merged into the 4.3-dev branch (closes #28831). Discussion ---------- [Intl] Add Timezones | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | replaces #26851, #17628, #17636 | License | MIT | Doc PR | symfony/symfony-docs#11447 This PR compiles a new `TimezoneBundle` from ICU data, based on the following resources: - https://github.com/unicode-org/icu/tree/master/icu4c/source/data/zone - https://github.com/unicode-org/icu/blob/master/icu4c/source/data/misc/timezoneTypes.txt - https://github.com/unicode-org/icu/blob/master/icu4c/source/data/misc/metaZones.txt The goal is to provide a fixed set of timezones, with a localized human readable name. For inspiration and to double check some data i used the timezone widget from google, e.g. when using Google Calendar. I've only pushed some common compiled locales for review, the rest will follow once finished. cc @jakzal Commits ------- 4bea198 [Intl] Add Timezones
2 parents 98929dc + 4bea198 commit eab7611

File tree

209 files changed

+49335
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

209 files changed

+49335
-0
lines changed

src/Symfony/Component/Intl/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ CHANGELOG
99
* added `Languages` and `Scripts` in favor of `Intl::getLanguageBundle()`
1010
* added `Locales` in favor of `Intl::getLocaleBundle()`
1111
* added `Regions` in favor of `Intl::getRegionBundle()`
12+
* added `Timezones`
1213

1314
4.2.0
1415
-----
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Intl\Data\Generator;
13+
14+
use Symfony\Component\Intl\Data\Bundle\Compiler\GenrbCompiler;
15+
use Symfony\Component\Intl\Data\Bundle\Reader\BundleReaderInterface;
16+
use Symfony\Component\Intl\Data\Util\ArrayAccessibleResourceBundle;
17+
use Symfony\Component\Intl\Data\Util\LocaleScanner;
18+
19+
/**
20+
* The rule for compiling the zone bundle.
21+
*
22+
* @author Roland Franssen <franssen.roland@gmail.com>
23+
*
24+
* @internal
25+
*/
26+
class TimezoneDataGenerator extends AbstractDataGenerator
27+
{
28+
/**
29+
* Collects all available zone codes.
30+
*
31+
* @var string[]
32+
*/
33+
private $zoneCodes = [];
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
protected function scanLocales(LocaleScanner $scanner, $sourceDir)
39+
{
40+
return $scanner->scanLocales($sourceDir.'/zone');
41+
}
42+
43+
/**
44+
* {@inheritdoc}
45+
*/
46+
protected function compileTemporaryBundles(GenrbCompiler $compiler, $sourceDir, $tempDir)
47+
{
48+
$compiler->compile($sourceDir.'/zone', $tempDir);
49+
$compiler->compile($sourceDir.'/misc/timezoneTypes.txt', $tempDir);
50+
$compiler->compile($sourceDir.'/misc/metaZones.txt', $tempDir);
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
protected function preGenerate()
57+
{
58+
$this->zoneCodes = [];
59+
}
60+
61+
/**
62+
* {@inheritdoc}
63+
*/
64+
protected function generateDataForLocale(BundleReaderInterface $reader, $tempDir, $displayLocale)
65+
{
66+
$localeBundle = $reader->read($tempDir, $displayLocale);
67+
68+
if (isset($localeBundle['zoneStrings']) && null !== $localeBundle['zoneStrings']) {
69+
$data = [
70+
'Version' => $localeBundle['Version'],
71+
'Names' => self::generateZones(
72+
$reader->read($tempDir, 'timezoneTypes'),
73+
$reader->read($tempDir, 'metaZones'),
74+
$reader->read($tempDir, 'root'),
75+
$localeBundle
76+
),
77+
];
78+
79+
$this->zoneCodes = array_merge($this->zoneCodes, array_keys($data['Names']));
80+
81+
return $data;
82+
}
83+
}
84+
85+
/**
86+
* {@inheritdoc}
87+
*/
88+
protected function generateDataForRoot(BundleReaderInterface $reader, $tempDir)
89+
{
90+
}
91+
92+
/**
93+
* {@inheritdoc}
94+
*/
95+
protected function generateDataForMeta(BundleReaderInterface $reader, $tempDir)
96+
{
97+
$rootBundle = $reader->read($tempDir, 'root');
98+
99+
$this->zoneCodes = array_unique($this->zoneCodes);
100+
101+
sort($this->zoneCodes);
102+
103+
$data = [
104+
'Version' => $rootBundle['Version'],
105+
'Zones' => $this->zoneCodes,
106+
];
107+
108+
return $data;
109+
}
110+
111+
private static function generateZones(ArrayAccessibleResourceBundle $typeBundle, ArrayAccessibleResourceBundle $metaBundle, ArrayAccessibleResourceBundle $rootBundle, ArrayAccessibleResourceBundle $localeBundle): array
112+
{
113+
$available = [];
114+
foreach ($typeBundle['typeMap']['timezone'] as $zone => $_) {
115+
if ('Etc:Unknown' === $zone || preg_match('~^Etc:GMT[-+]\d+$~', $zone)) {
116+
continue;
117+
}
118+
119+
$available[$zone] = true;
120+
}
121+
122+
$metazones = [];
123+
foreach ($metaBundle['metazoneInfo'] as $zone => $info) {
124+
foreach ($info as $metazone) {
125+
$metazones[$zone] = $metazone->get(0);
126+
}
127+
}
128+
129+
$zones = [];
130+
foreach (array_keys($available) as $zone) {
131+
// lg: long generic, e.g. "Central European Time"
132+
// ls: long specific (not DST), e.g. "Central European Standard Time"
133+
// ld: long DST, e.g. "Central European Summer Time"
134+
// ec: example city, e.g. "Amsterdam"
135+
$name = $localeBundle['zoneStrings'][$zone]['lg'] ?? $rootBundle['zoneStrings'][$zone]['lg'] ?? $localeBundle['zoneStrings'][$zone]['ls'] ?? $rootBundle['zoneStrings'][$zone]['ls'] ?? null;
136+
$city = $localeBundle['zoneStrings'][$zone]['ec'] ?? $rootBundle['zoneStrings'][$zone]['ec'] ?? null;
137+
138+
if (null === $name && isset($metazones[$zone])) {
139+
$meta = 'meta:'.$metazones[$zone];
140+
$name = $localeBundle['zoneStrings'][$meta]['lg'] ?? $rootBundle['zoneStrings'][$meta]['lg'] ?? $localeBundle['zoneStrings'][$meta]['ls'] ?? $rootBundle['zoneStrings'][$meta]['ls'] ?? null;
141+
}
142+
if (null === $city && 0 !== strrpos($zone, 'Etc:') && false !== $i = strrpos($zone, ':')) {
143+
$city = str_replace('_', ' ', substr($zone, $i + 1));
144+
}
145+
if (null === $name) {
146+
continue;
147+
}
148+
if (null !== $city) {
149+
$name .= ' ('.$city.')';
150+
}
151+
152+
$id = str_replace(':', '/', $zone);
153+
$zones[$id] = $name;
154+
}
155+
156+
return $zones;
157+
}
158+
}

src/Symfony/Component/Intl/Intl.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ final class Intl
6464
*/
6565
const REGION_DIR = 'regions';
6666

67+
/**
68+
* The directory name of the zone data.
69+
*/
70+
public const TIMEZONE_DIR = 'timezones';
71+
6772
/**
6873
* @var ResourceBundle\CurrencyBundleInterface
6974
*/

src/Symfony/Component/Intl/Resources/bin/update-data.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Intl\Data\Generator\LocaleDataGenerator;
2121
use Symfony\Component\Intl\Data\Generator\RegionDataGenerator;
2222
use Symfony\Component\Intl\Data\Generator\ScriptDataGenerator;
23+
use Symfony\Component\Intl\Data\Generator\TimezoneDataGenerator;
2324
use Symfony\Component\Intl\Data\Provider\LanguageDataProvider;
2425
use Symfony\Component\Intl\Data\Provider\RegionDataProvider;
2526
use Symfony\Component\Intl\Data\Provider\ScriptDataProvider;
@@ -195,6 +196,7 @@
195196
$targetDir.'/'.Intl::LOCALE_DIR,
196197
$targetDir.'/'.Intl::REGION_DIR,
197198
$targetDir.'/'.Intl::SCRIPT_DIR,
199+
$targetDir.'/'.Intl::TIMEZONE_DIR,
198200
]);
199201
}
200202

@@ -256,6 +258,11 @@
256258
//
257259
//$filesystem->remove($txtDir);
258260

261+
echo "Generating zone data...\n";
262+
263+
$generator = new TimezoneDataGenerator($compiler, Intl::TIMEZONE_DIR);
264+
$generator->generateData($config);
265+
259266
echo "Resource bundle compilation complete.\n";
260267

261268
$gitInfo = <<<GIT_INFO

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy