Skip to content

Commit 2856abe

Browse files
committed
Implement the support of timezone objects in the stub IntlDateFormatter
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.
1 parent 7ebda1e commit 2856abe

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)