Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Merge pull request #2335 from dlondero/issue-2335
Browse files Browse the repository at this point in the history
Pass offset instead of timeZoneName to IntlDateFormatter
  • Loading branch information
dlondero authored Sep 16, 2016
2 parents dd6b79a + cbe7150 commit 2ffeda2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
14 changes: 13 additions & 1 deletion library/CM/Frontend/Render.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ public function clearTemplates() {
* @param string|null $pattern
* @param DateTimeZone|null $timeZone
* @return IntlDateFormatter
* @throws CM_Exception
*/
public function getFormatterDate($dateType, $timeType, $pattern = null, DateTimeZone $timeZone = null) {
if (null === $timeZone) {
Expand All @@ -402,7 +403,18 @@ public function getFormatterDate($dateType, $timeType, $pattern = null, DateTime
if (in_array(substr($timeZoneName, 0, 1), ['+', '-'])) {
$timeZoneName = 'GMT' . $timeZoneName;
}
return new IntlDateFormatter($this->getLocale(), $dateType, $timeType, $timeZoneName, null, $pattern);

$formatter = new IntlDateFormatter($this->getLocale(), $dateType, $timeType, $timeZoneName, null, $pattern);
if (null === $formatter) {
throw new CM_Exception('Cannot create date formatter', null, [
'locale' => $this->getLocale(),
'dateType' => $dateType,
'timeType' => $timeType,
'timeZoneName' => $timeZoneName,
'pattern' => $pattern,
]);
}
return $formatter;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion library/CM/Model/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ public function getTimeZone() {
if (null === $pointCurrent) {
return null;
}
$timezoneNameList = DateTimeZone::listIdentifiers();
$timezoneNameList = \Functional\reject(DateTimeZone::listIdentifiers(), function ($timeZoneName) {
return null === IntlTimeZone::fromDateTimeZone(new DateTimeZone($timeZoneName));
});

$distanceList = Functional\map($timezoneNameList, function ($timezoneName) use ($pointCurrent) {
$timezoneLocation = (new DateTimeZone($timezoneName))->getLocation();
$pointTimeZone = new CM_Geo_Point($timezoneLocation['latitude'], $timezoneLocation['longitude']);
Expand Down
18 changes: 18 additions & 0 deletions tests/library/CM/Frontend/RenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,24 @@ public function testGetFormatterDateNumericalOverrideTimeZone() {
}
}

public function testGetFormatterDateException() {
$timezoneNameList = \Functional\reject(DateTimeZone::listIdentifiers(), function ($timeZoneName) {
return IntlTimeZone::fromDateTimeZone(new DateTimeZone($timeZoneName));
});
if (empty($timezoneNameList)) {
$this->markTestSkipped('No unsupported timezones');
}

try {
$timeZoneName = \Functional\first($timezoneNameList);
$render = new CM_Frontend_Render();
$render->getFormatterDate(IntlDateFormatter::SHORT, IntlDateFormatter::SHORT, null, new DateTimeZone($timeZoneName));
$this->fail('Date formatter created with unsupported timezone');
} catch (CM_Exception $ex) {
$this->assertSame('Cannot create date formatter', $ex->getMessage());
}
}

public function testGetLayoutPath() {
$render = new CM_Frontend_Render();
$this->assertSame(
Expand Down

0 comments on commit 2ffeda2

Please sign in to comment.