Skip to content

Commit 0ea11e4

Browse files
committed
bug symfony#15489 Implement the support of timezone objects in the stub IntlDateFormatter (stof)
This PR was merged into the 2.3 branch. Discussion ---------- Implement the support of timezone objects in the stub IntlDateFormatter | Q | A | ------------- | --- | Bug fix? | yes | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a As of PHP 5.5, the IntlDateFormatter accepts to use DateTimeZone or IntlTimeZone objects as timezone in the constructor (and in the new setTimeZone method) rather than timezone ids. This is even the proper way to pass a timezone from a DateTime object as DateTimeZone names are not all valid ICU identifiers (and there is a PR on the Twig-extensions repo to use such feature to support things properly: twigphp/Twig-extensions#148). I'm considering this as a bugfix because it is a mismatch between the stub implementation and the real class. Note that for simplicity, these objects are accepted on all PHP versions, as reproducing the behavior of older versions is not possible in the stub anyway (triggering a warning and making the instantiating with ``new`` return ``null``). We already have such differences anyway (the ``setTimeZone`` method exists in all PHP versions in the stub) Commits ------- 2856abe Implement the support of timezone objects in the stub IntlDateFormatter
2 parents d82cd8f + 2856abe commit 0ea11e4

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/Symfony/Component/Intl/DateFormatter/IntlDateFormatter.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class IntlDateFormatter
132132
* @param string $locale The locale code. The only currently supported locale is "en" (or null using the default locale, i.e. "en").
133133
* @param int $datetype Type of date formatting, one of the format type constants
134134
* @param int $timetype Type of time formatting, one of the format type constants
135-
* @param string $timezone Timezone identifier
135+
* @param mixed $timezone Timezone identifier
136136
* @param int $calendar Calendar to use for formatting or parsing. The only currently
137137
* supported value is IntlDateFormatter::GREGORIAN.
138138
* @param string $pattern Optional pattern to use when formatting
@@ -157,7 +157,7 @@ public function __construct($locale, $datetype, $timetype, $timezone = null, $ca
157157
$this->timetype = $timetype;
158158

159159
$this->setPattern($pattern);
160-
$this->setTimeZoneId($timezone);
160+
$this->setTimeZone($timezone);
161161
}
162162

163163
/**
@@ -588,6 +588,19 @@ public function setTimeZoneId($timeZoneId)
588588
*/
589589
public function setTimeZone($timeZone)
590590
{
591+
if ($timeZone instanceof \IntlTimeZone) {
592+
$timeZone = $timeZone->getID();
593+
}
594+
595+
if ($timeZone instanceof \DateTimeZone) {
596+
$timeZone = $timeZone->getName();
597+
598+
// DateTimeZone returns the GMT offset timezones without the leading GMT, while our parsing requires it.
599+
if (!empty($timeZone) && ('+' === $timeZone[0] || '-' === $timeZone[0])) {
600+
$timeZone = 'GMT'.$timeZone;
601+
}
602+
}
603+
591604
return $this->setTimeZoneId($timeZone);
592605
}
593606

src/Symfony/Component/Intl/Tests/DateFormatter/AbstractIntlDateFormatterTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,36 @@ public function testFormatWithConstructorTimezone()
387387
);
388388
}
389389

390+
public function testFormatWithDateTimeZone()
391+
{
392+
if (PHP_VERSION_ID < 50500) {
393+
$this->markTestSkipped('Only in PHP 5.5+ IntlDateFormatter allows to use DateTimeZone objects.');
394+
}
395+
396+
if (defined('HHVM_VERSION_ID')) {
397+
$this->markTestSkipped('This test cannot work on HHVM. See https://github.com/facebook/hhvm/issues/5875 for the issue.');
398+
}
399+
400+
$formatter = $this->getDateFormatter('en', IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT, new \DateTimeZone('GMT+03:00'), IntlDateFormatter::GREGORIAN, 'zzzz');
401+
402+
$this->assertEquals('GMT+03:00', $formatter->format(0));
403+
}
404+
405+
public function testFormatWithIntlTimeZone()
406+
{
407+
if (PHP_VERSION_ID < 50500) {
408+
$this->markTestSkipped('Only in PHP 5.5+ IntlDateFormatter allows to use DateTimeZone objects.');
409+
}
410+
411+
if (!class_exists('IntlTimeZone')) {
412+
$this->markTestSkipped('This test requires the IntlTimeZone class from the Intl extension.');
413+
}
414+
415+
$formatter = $this->getDateFormatter('en', IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT, \IntlTimeZone::createTimeZone('GMT+03:00'), IntlDateFormatter::GREGORIAN, 'zzzz');
416+
417+
$this->assertEquals('GMT+03:00', $formatter->format(0));
418+
}
419+
390420
public function testFormatWithTimezoneFromEnvironmentVariable()
391421
{
392422
if (PHP_VERSION_ID >= 50500) {

0 commit comments

Comments
 (0)