Navigation Menu

Skip to content

Commit

Permalink
Implement outputTimezone in a few methods.
Browse files Browse the repository at this point in the history
Remove the tests for the various test methods. They all rely on string
timestamps which make user timezones more difficult.
  • Loading branch information
markstory committed Jun 19, 2016
1 parent ef5de31 commit a688b04
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 104 deletions.
40 changes: 38 additions & 2 deletions src/View/Helper/TimeHelper.php
Expand Up @@ -32,6 +32,31 @@ class TimeHelper extends Helper

use StringTemplateTrait;

/**
* Config options
*
* @var array
*/
protected $_defaultConfig = [
'outputTimezone' => null
];

/**
* Get a timezone.
*
* Will use the provided timezone, or default output timezone if defined.
*
* @param null|string|DateTimeZone $timezone The override timezone if applicable.
* @return null|string|DateTimeZone The chosen timezone or null.
*/
protected function _timezone($timezone)
{
if ($timezone) {
return $timezone;
}
return $this->config('outputTimezone');
}

/**
* Returns a UNIX timestamp, given either a UNIX timestamp or a valid strtotime() date string.
*
Expand All @@ -54,6 +79,7 @@ public function fromString($dateString, $timezone = null)
*/
public function nice($dateString = null, $timezone = null, $locale = null)
{
$timezone = $this->_timezone($timezone);
return (new Time($dateString))->nice($timezone, $locale);
}

Expand Down Expand Up @@ -190,7 +216,7 @@ public function toUnix($dateString, $timezone = null)
*/
public function toAtom($dateString, $timezone = null)
{
$timezone = $timezone ?: date_default_timezone_get();
$timezone = $this->_timezone($timezone) ?: date_default_timezone_get();
return (new Time($dateString))->timezone($timezone)->toAtomString();
}

Expand All @@ -203,7 +229,7 @@ public function toAtom($dateString, $timezone = null)
*/
public function toRss($dateString, $timezone = null)
{
$timezone = $timezone ?: date_default_timezone_get();
$timezone = $this->_timezone($timezone) ?: date_default_timezone_get();
return (new Time($dateString))->timezone($timezone)->toRssString();
}

Expand All @@ -226,6 +252,15 @@ public function toRss($dateString, $timezone = null)
public function timeAgoInWords($dateTime, array $options = [])
{
$element = null;
$options += [
'element' => null,
'timezone' => null
];
$options['timezone'] = $this->_timezone($options['timezone']);
if ($options['timezone']) {
$dateTime = $dateTime->timezone($options['timezone']);
unset($options['timezone']);
}

if (!empty($options['element'])) {
$element = [
Expand Down Expand Up @@ -332,6 +367,7 @@ public function i18nFormat($date, $format = null, $invalid = false, $timezone =
if (!isset($date)) {
return $invalid;
}
$timezone = $this->_timezone($timezone);

try {
$time = new Time($date);
Expand Down
149 changes: 47 additions & 102 deletions tests/TestCase/View/Helper/TimeHelperTest.php
Expand Up @@ -165,6 +165,18 @@ public function testNice()
$this->assertTimeFormat('Apr 20, 2014, 4:00 PM', $result);
}

/**
* test nice with outputTimezone
*
* @return void
*/
public function testNiceOutputTimezone()
{
$this->Time->config('outputTimezone', 'America/Vancouver');
$time = '2014-04-20 20:00';
$this->assertTimeFormat('Apr 20, 2014, 1:00 PM', $this->Time->nice($time));
}

/**
* testToUnix method
*
Expand Down Expand Up @@ -227,7 +239,12 @@ public function testToRss()
*/
public function testToRssOutputTimezone()
{
$this->markTestIncomplete();
$this->Time->config('outputTimezone', 'America/Vancouver');
$dateTime = new Time;
$vancouver = clone $dateTime;
$vancouver->timezone('America/Vancouver');

$this->assertEquals($vancouver->format('r'), $this->Time->toRss($vancouver));
}

/**
Expand All @@ -249,24 +266,17 @@ public function testIsToday()
{
$result = $this->Time->isToday('+1 day');
$this->assertFalse($result);

$result = $this->Time->isToday('+1 days');
$this->assertFalse($result);

$result = $this->Time->isToday('+0 day');
$this->assertTrue($result);

$result = $this->Time->isToday('-1 day');
$this->assertFalse($result);
}

/**
* test isToday with outputTimezone
*
* @return void
*/
public function testIsTodayOutputTimezone()
{
$this->markTestIncomplete();
}

/**
* testIsFuture method
*
Expand All @@ -285,16 +295,6 @@ public function testIsFuture()
$this->assertFalse($this->Time->isFuture('-1 month'));
}

/**
* test isFuture with outputTimezone
*
* @return void
*/
public function testIsFutureOutputTimezone()
{
$this->markTestIncomplete();
}

/**
* testIsPast method
*
Expand All @@ -313,16 +313,6 @@ public function testIsPast()
$this->assertTrue($this->Time->isPast('-1 month'));
}

/**
* test isPast with outputTimezone
*
* @return void
*/
public function testIsPastOutputTimezone()
{
$this->markTestIncomplete();
}

/**
* testIsThisWeek method
*
Expand All @@ -345,16 +335,6 @@ public function testIsThisWeek()
$this->assertFalse($this->Time->isThisWeek('+' . $days[1] . ' days'));
}

/**
* test isThisWeek with outputTimezone
*
* @return void
*/
public function testIsThisWeekOutputTimezone()
{
$this->markTestIncomplete();
}

/**
* testIsThisMonth method
*
Expand All @@ -372,16 +352,6 @@ public function testIsThisMonth()
$this->assertFalse($result);
}

/**
* test isThisMonth with outputTimezone
*
* @return void
*/
public function testIsThisMonthOutputTimezone()
{
$this->markTestIncomplete();
}

/**
* testIsThisYear method
*
Expand All @@ -395,16 +365,6 @@ public function testIsThisYear()
$this->assertTrue($result);
}

/**
* test isThisYear with outputTimezone
*
* @return void
*/
public function testIsThisYearOutputTimezone()
{
$this->markTestIncomplete();
}

/**
* testWasYesterday method
*
Expand All @@ -426,16 +386,6 @@ public function testWasYesterday()
$this->assertFalse($result);
}

/**
* test wasYesterday with outputTimezone
*
* @return void
*/
public function testWasYesterdayOutputTimezone()
{
$this->markTestIncomplete();
}

/**
* testIsTomorrow method
*
Expand All @@ -453,16 +403,6 @@ public function testIsTomorrow()
$this->assertFalse($result);
}

/**
* test isTomorrow with outputTimezone
*
* @return void
*/
public function testIsTomorrowOutputTimezone()
{
$this->markTestIncomplete();
}

/**
* testWasWithinLast method
*
Expand Down Expand Up @@ -509,16 +449,6 @@ public function testWasWithinLast()
$this->assertTrue($this->Time->wasWithinLast('1 ', '-23 hours -59 minutes -59 seconds'));
}

/**
* test wasWithinLast with outputTimezone
*
* @return void
*/
public function testWasWithinLastOutputTimezone()
{
$this->markTestIncomplete();
}

/**
* testWasWithinLast method
*
Expand Down Expand Up @@ -565,16 +495,6 @@ public function testIsWithinNext()
$this->assertTrue($this->Time->isWithinNext('1 month', '+1 month'));
}

/**
* test isWithinNext with outputTimezone
*
* @return void
*/
public function testIsWithinNextOutputTimezone()
{
$this->markTestIncomplete();
}

/**
* test formatting dates taking in account preferred i18n locale file
*
Expand Down Expand Up @@ -611,7 +531,32 @@ public function testFormat()
*/
public function testFormatOutputTimezone()
{
$this->markTestIncomplete();
$this->Time->config('outputTimezone', 'America/Vancouver');

$time = strtotime('Thu Jan 14 8:59:28 2010 UTC');
$result = $this->Time->format($time);
$expected = '1/14/10, 12:59 AM';
$this->assertTimeFormat($expected, $result);

$time = new Time('Thu Jan 14 8:59:28 2010', 'UTC');
$result = $this->Time->format($time);
$expected = '1/14/10, 12:59 AM';
$this->assertTimeFormat($expected, $result);
}

/**
* test i18nFormat with outputTimezone
*
* @return void
*/
public function testI18nFormatOutputTimezone()
{
$this->Time->config('outputTimezone', 'America/Vancouver');

$time = strtotime('Thu Jan 14 8:59:28 2010 UTC');
$result = $this->Time->i18nFormat($time, \IntlDateFormatter::SHORT);
$expected = '1/14/10, 12:59:28 AM';
$this->assertStringStartsWith($expected, $result);
}

/**
Expand Down

0 comments on commit a688b04

Please sign in to comment.