Skip to content

Commit

Permalink
Making Time::__toString be locale aware. Adding tests for defaultLocale
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Apr 21, 2014
1 parent f67dde6 commit 7a9985d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/Utility/Time.php
Expand Up @@ -497,6 +497,14 @@ public function i18nFormat($format = null, $timezone = null, $locale = null) {
return IntlDateFormatter::formatObject($time, $format, $locale);
}

/**
* {@inheritdoc}
*
*/
public function __toString() {
return $this->i18nFormat();
}

/**
* Get list of timezone identifiers
*
Expand Down
27 changes: 25 additions & 2 deletions tests/TestCase/Utility/TimeTest.php
Expand Up @@ -42,6 +42,7 @@ class TimeTest extends TestCase {
public function setUp() {
parent::setUp();
$this->now = Time::getTestNow();
$this->locale = Time::$defaultLocale;
}

/**
Expand All @@ -51,7 +52,8 @@ public function setUp() {
*/
public function tearDown() {
parent::tearDown();
Time::setTestNow($this->now);
Time::setTestNow($this->now);
Time::$defaultLocale = $this->locale;
}

/**
Expand Down Expand Up @@ -485,7 +487,6 @@ public function testIsWithinNext() {
public function testI18nFormat() {
$time = new Time('Thu Jan 14 13:59:28 2010');
$result = $time->i18nFormat();

$expected = '1/14/10, 1:59 PM';
$this->assertEquals($expected, $result);

Expand All @@ -501,6 +502,15 @@ public function testI18nFormat() {
$result = $time->i18nFormat('HH:mm:ss', 'Australia/Sydney');
$expected = '00:59:28';
$this->assertEquals($expected, $result);

Time::$defaultLocale = 'fr-FR';
$result = $time->i18nFormat(\IntlDateFormatter::FULL);
$expected = 'jeudi 14 janvier 2010 13:59:28 UTC';
$this->assertEquals($expected, $result);

$result = $time->i18nFormat(\IntlDateFormatter::FULL, null, 'es-ES');
$expected = 'jueves, 14 de enero de 2010, 13:59:28 (GMT)';
$this->assertEquals($expected, $result, 'DEfault locale should not be used');
}

/**
Expand Down Expand Up @@ -535,4 +545,17 @@ public function testListTimezones() {
$this->assertFalse(isset($return['Asia/Bangkok']));
}

/**
* Tests that __toString uses the i18n formatter
*
* @return void
*/
public function testToString() {
$time = new Time('2014-04-20 22:10');
$this->assertEquals('4/20/14, 10:10 PM', (string)$time);

Time::$defaultLocale = 'fr-FR';
$this->assertEquals('20/04/2014 22:10', (string)$time);
}

}

0 comments on commit 7a9985d

Please sign in to comment.