Skip to content
Permalink
Browse files

bare and minimal default output timezone support for Time/FrozenTime

  • Loading branch information...
Jonas Hartmann authored and markstory committed Apr 30, 2016
1 parent 445e0cc commit 75bcf62cc143dadebe0e8fdfa83c180c68ab2a44
Showing with 83 additions and 1 deletion.
  1. +40 −0 src/I18n/DateFormatTrait.php
  2. +43 −1 tests/TestCase/I18n/TimeTest.php
@@ -34,6 +34,14 @@ trait DateFormatTrait
*/
public static $defaultLocale;
/**
* The \DateTimeZone default output timezone.
* See http://php.net/manual/en/timezones.php.
*
* @var \DateTimeZone|null
*/
protected static $_defaultOutputTimezone;
/**
* In-memory cache of date formatters
*
@@ -64,6 +72,33 @@ trait DateFormatTrait
*/
protected static $_isDateInstance;
/** Gets the default output timezone.
*
* @return \DateTimeZone|null DateTimeZone object in which the date will be displayed or null.
*/
public static function getDefaultOutputTimezone()
{
return static::$_defaultOutputTimezone;
}
/**
* Sets the default output timezone.
*
* @param string|\DateTimeZone $timezone Timezone string or DateTimeZone object
* in which the date will be displayed.
* @return void
*/
public static function setDefaultOutputTimezone($timezone)
{
if (is_string($timezone)) {
static::$_defaultOutputTimezone = new \DateTimeZone($timezone);
} elseif ($timezone instanceof \DateTimeZone) {
static::$_defaultOutputTimezone = $timezone;
} else {
throw new \InvalidArgumentException('Expected valid DateTimeZone string or \DateTimeZone object.');
}
}
/**
* Gets the default locale.
*
@@ -157,6 +192,11 @@ public function i18nFormat($format = null, $timezone = null, $locale = null)
{
$time = $this;
// This is required for testI18nFormatWithOffsetTimezone to pass
if ($time->getTimezone()->getName() === date_default_timezone_get()) {
$timezone = $timezone ?: static::getDefaultOutputTimezone();
}
if ($timezone) {
// Handle the immutable and mutable object cases.
$time = clone $this;
@@ -38,6 +38,10 @@ public function setUp()
$this->locale = Time::getDefaultLocale();
Time::setDefaultLocale('en_US');
FrozenTime::setDefaultLocale('en_US');
date_default_timezone_set('UTC');
Time::setDefaultOutputTimezone('UTC');
FrozenTime::setDefaultOutputTimezone('UTC');
}
/**
@@ -55,8 +59,12 @@ public function tearDown()
FrozenTime::setTestNow($this->frozenNow);
FrozenTime::setDefaultLocale($this->locale);
FrozenTime::resetToStringFormat();
date_default_timezone_set('UTC');
I18n::locale(I18n::DEFAULT_LOCALE);
date_default_timezone_set('UTC');
Time::setDefaultOutputTimezone('UTC');
FrozenTime::setDefaultOutputTimezone('UTC');
}
/**
@@ -481,6 +489,40 @@ public function testI18nFormat($class)
$this->assertTimeFormat($expected, $result);
}
/**
* test formatting dates taking in account default output timezones.
*
* @dataProvider classNameProvider
* @return void
*/
public function testI18nFormatWithDefaultOutputTimezone($class)
{
$time = new $class('Thu Jan 14 13:59:28 2010');
$class::$defaultLocale = 'en-CA';
$class::setDefaultOutputTimezone('America/Vancouver');
$result = $time->i18nFormat();
$expected = '1/14/10 5:59 AM';
$this->assertTimeFormat($expected, $result);
$result = $time->i18nFormat(null, 'America/Toronto');
$expected = '1/14/10 8:59 AM';
$this->assertTimeFormat($expected, $result);
$class::$defaultLocale = 'de-DE';
$class::setDefaultOutputTimezone('Europe/Berlin');
$result = $time->i18nFormat();
$expected = '14.01.10 14:59';
$this->assertTimeFormat($expected, $result);
$result = $time->i18nFormat(null, 'Europe/London');
$expected = '14.01.10 13:59';
$this->assertTimeFormat($expected, $result);
}
/**
* test formatting dates with offset style timezone
*

0 comments on commit 75bcf62

Please sign in to comment.
You can’t perform that action at this time.