Skip to content
Permalink
Browse files

fixed nice, added tests, added switch to disable timezone outputforma…

…tting for date/frozendate
  • Loading branch information...
Jonas Hartmann authored and markstory committed May 13, 2016
1 parent 24b228c commit b950c11ffa91d772606a982ae276bdcb8a1856d8
Showing with 120 additions and 9 deletions.
  1. +18 −7 src/I18n/DateFormatTrait.php
  2. +6 −2 src/I18n/RelativeTimeFormatter.php
  3. +96 −0 tests/TestCase/I18n/TimeTest.php
@@ -17,7 +17,9 @@
use Cake\Chronos\Date as ChronosDate;
use Cake\Chronos\MutableDate;
use IntlDateFormatter;
use \NotImplementedException;
use \DateTimeZone;
use \InvalidArgumentException;
use \RuntimeException;
/**
* Trait for date formatting methods shared by both Time & Date.
@@ -79,8 +81,8 @@ trait DateFormatTrait
*/
public static function getDefaultOutputTimezone()
{
if (static::$_isDateInstance === true) {
throw new NotImplementedException('Timezone conversion is not supported by Date/FrozenDate.');
if (is_subclass_of(static::class, ChronosDate::class) || is_subclass_of(static::class, MutableDate::class)) {
throw new \RuntimeException('Timezone conversion is not supported by Date/FrozenDate.');
}
return static::$_defaultOutputTimezone;
}
@@ -94,8 +96,8 @@ public static function getDefaultOutputTimezone()
*/
public static function setDefaultOutputTimezone($timezone)
{
if (static::$_isDateInstance === true) {
throw new NotImplementedException('Timezone conversion is not supported by Date/FrozenDate.');
if (is_subclass_of(static::class, ChronosDate::class) || is_subclass_of(static::class, MutableDate::class)) {
throw new \RuntimeException('Timezone conversion is not supported by Date/FrozenDate.');
}
if (is_string($timezone)) {
static::$_defaultOutputTimezone = new \DateTimeZone($timezone);
@@ -199,13 +201,22 @@ public function i18nFormat($format = null, $timezone = null, $locale = null)
{
$time = $this;
if (static::$_isDateInstance === false) {
if ($time instanceof Time || $time instanceof FrozenTime) {
// Remove before merge:
// if ((is_string($time->timezone) && $time->timezone === 'UTC')
// || ($time->timezone instanceof \DateTimeZone && $time->timezone->getName() === '+0:00')
// ) {
// }
// if ($time->getOffset() !== 0) {
// $time = clone $time;
// $time->add(new \DateInterval('PT' . $time->getOffset() / 60 . 'M'));
// }
$timezone = $timezone ?: static::getDefaultOutputTimezone();
}
if ($timezone) {
// Handle the immutable and mutable object cases.
$time = clone $this;
$time = clone $time;
$time = $time->timezone($timezone);
}
@@ -93,8 +93,12 @@ public function diffForHumans(ChronosInterface $date, ChronosInterface $other =
public function timeAgoInWords(DatetimeInterface $time, array $options = [])
{
$options = $this->_options($options, FrozenTime::class);
$timezone = null;
if ($options['timezone']) {
$time = $time->timezone($options['timezone']);
$timezone = $options['timezone'];
} elseif ($time instanceof Time || $time instanceof FrozenTime) {
$timezone = $time->getDefaultOutputTimezone();
}
$now = $options['from']->format('U');
@@ -114,7 +118,7 @@ public function timeAgoInWords(DatetimeInterface $time, array $options = [])
}
if ($diff > abs($now - (new FrozenTime($options['end']))->format('U'))) {
return sprintf($options['absoluteString'], $time->i18nFormat($options['format']));
return sprintf($options['absoluteString'], $time->i18nFormat($options['format'], $timezone));
}
$diffData = $this->_diffData($futureTime, $pastTime, $backwards, $options);
@@ -200,6 +200,7 @@ public function timeAgoEndProvider()
],
];
}
/**
* test the timezone option for timeAgoInWords
*
@@ -219,6 +220,36 @@ public function testTimeAgoInWordsTimezone($class)
$this->assertEquals('on 31-07-1990 13:33:00', $result);
}
/**
* test the timezone option for timeAgoInWords
*
* @dataProvider classNameProvider
* @return void
*/
public function testTimeAgoInWordsTimezoneOutputDefaultTimezone($class)
{
$class::setDefaultOutputTimezone('Europe/Paris');
$time = new $class('1990-07-31 20:33:00 UTC');
$result = $time->timeAgoInWords(
[
'end' => '+1month',
'format' => 'dd-MM-YYYY HH:mm:ss'
]
);
$this->assertEquals('on 31-07-1990 22:33:00', $result);
$class::setDefaultOutputTimezone('Europe/Berlin');
$time = new $class('1990-07-31 20:33:00 UTC');
$result = $time->timeAgoInWords(
[
'timezone' => 'America/Vancouver',
'end' => '+1month',
'format' => 'dd-MM-YYYY HH:mm:ss'
]
);
$this->assertEquals('on 31-07-1990 13:33:00', $result);
}
/**
* test the end option for timeAgoInWords
*
@@ -429,6 +460,29 @@ public function testNice($class)
$this->assertTimeFormat('20 avr. 2014 16:00', $time->nice('America/New_York', 'fr-FR'));
}
/**
* testNiceWithDefaultOutputTimezone method
*
* @dataProvider classNameProvider
* @return void
*/
public function testNiceWithDefaultOutputTimezone($class)
{
$class::setDefaultOutputTimezone('America/Vancouver');
$time = new $class('2014-04-20 20:00', 'UTC');
$this->assertTimeFormat('Apr 20, 2014, 1:00 PM', $time->nice());
$result = $time->nice('America/New_York');
$this->assertTimeFormat('Apr 20, 2014, 4:00 PM', $result);
$this->assertEquals('UTC', $time->getTimezone()->getName());
$class::setDefaultOutputTimezone('Europe/Paris');
$time = new $class('2014-04-20 20:00', 'UTC');
$this->assertTimeFormat('20 avr. 2014 22:00', $time->nice(null, 'fr-FR'));
$this->assertTimeFormat('20 avr. 2014 16:00', $time->nice('America/New_York', 'fr-FR'));
}
/**
* test formatting dates taking in account preferred i18n locale file
*
@@ -548,6 +602,33 @@ public function testI18nFormatWithOffsetTimezone($class)
$this->assertTimeFormat($expected, $result);
}
/**
* test formatting dates with offset style timezone and defaultOutputTimezone
*
* @dataProvider classNameProvider
* @see https://github.com/facebook/hhvm/issues/3637
* @return void
*/
public function testI18nFormatWithOffsetTimezoneWithDefaultOutputTimezone($class)
{
$class::setDefaultOutputTimezone('America/Vancouver');
$time = new $class('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 $class('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 $class('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
*
@@ -595,6 +676,21 @@ public function testListTimezones($class)
$this->assertFalse(isset($return['Asia/Bangkok']));
}
/**
* Tests that __toString uses the i18n formatter and works with OutputTimezones
*
* @dataProvider classNameProvider
* @return void
*/
public function testToStringWithdefaultOutputTimezone($class)
{
$class::setDefaultOutputTimezone('America/Vancouver');
$time = new $class('2014-04-20 22:10');
$class::setDefaultLocale('fr-FR');
$class::setToStringFormat(\IntlDateFormatter::FULL);
$this->assertTimeFormat('dimanche 20 avril 2014 15:10:00 UTC', (string)$time);
}
/**
* Tests that __toString uses the i18n formatter
*

0 comments on commit b950c11

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