Skip to content

Commit

Permalink
fixes offset style timezone for datefmt_create
Browse files Browse the repository at this point in the history
  • Loading branch information
Tietew committed Apr 3, 2015
1 parent b307d68 commit ed3c91c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/I18n/Time.php
Expand Up @@ -17,6 +17,7 @@
use Carbon\Carbon;
use IntlDateFormatter;
use JsonSerializable;
use RuntimeException;

/**
* Extends the built-in DateTime class to provide handy methods and locale-aware
Expand Down Expand Up @@ -578,11 +579,16 @@ protected function _formatObject($date, $format, $locale)
$key = "{$locale}.{$dateFormat}.{$timeFormat}.{$timezone}.{$calendar}.{$pattern}";

if (!isset(static::$_formatters[$key])) {
if ($timezone === '+00:00') {
$timezone = 'UTC';
} elseif ($timezone[0] === '+' || $timezone[0] === '-') {
$timezone = 'GMT' . $timezone;
}
static::$_formatters[$key] = datefmt_create(
$locale,
$dateFormat,
$timeFormat,
$timezone === '+00:00' ? 'UTC' : $timezone,
$timezone,
$calendar,
$pattern
);
Expand Down
23 changes: 23 additions & 0 deletions tests/TestCase/I18n/TimeTest.php
Expand Up @@ -529,6 +529,29 @@ public function testI18nFormat()
$this->assertTimeFormat($expected, $result, 'DEfault locale should not be used');
}

/**
* test formatting dates with offset style timezone
*
* @return void
*/
public function testI18nFormatWithOffsetTimezone()
{
$time = new Time('2014-01-01T00:00:00+00');
$result = $time->i18nFormat(\IntlDateFormatter::FULL);
$expected = 'Wednesday January 1 2014 12:00:00 AM GMT';
$this->assertTimeFormat($expected, $result);

$time = new Time('2014-01-01T00:00:00+09');
$result = $time->i18nFormat(\IntlDateFormatter::FULL);
$expected = 'Wednesday January 1 2014 12:00:00 AM GMT+09:00';
$this->assertTimeFormat($expected, $result);

$time = new Time('2014-01-01T00:00:00-01:30');
$result = $time->i18nFormat(\IntlDateFormatter::FULL);
$expected = 'Wednesday January 1 2014 12:00:00 AM GMT-01:30';
$this->assertTimeFormat($expected, $result);
}

/**
* testListTimezones
*
Expand Down

0 comments on commit ed3c91c

Please sign in to comment.