Skip to content

Commit

Permalink
Implemented DefaultFormatter::formatTimeZone [#14]
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalloc committed Jan 24, 2013
1 parent 9eb0e13 commit 949d0f8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 14 deletions.
22 changes: 21 additions & 1 deletion lib/Icecave/Chrono/Format/DefaultFormatter.php
Expand Up @@ -222,7 +222,27 @@ public function formatTimeZone(TimeZone $timeZone, $formatSpecifier)
{
$this->typeCheck->formatTimeZone(func_get_args());

return '';
return $this->replace(
$formatSpecifier,
function ($character) use ($timeZone) {
switch ($character) {
case 'e':
break;
case 'I':
return $timeZone->isDst() ? '1' : '0';
case 'O':
return str_replace(':', '', $timeZone->isoString());
case 'P':
return $timeZone->isoString();
case 'T':
break;
case 'Z':
return $timeZone->offset();
}

return $character;
}
);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Icecave/Chrono/TimeZone.php
Expand Up @@ -91,8 +91,8 @@ public function isoString()
$this->typeCheck->isoString(func_get_args());

$seconds = abs($this->offset);
$hours = $seconds / 3600;
$minutes = ($seconds % 3600) / 60;
$hours = $seconds / 3600;

return sprintf(
'%s%02d:%02d',
Expand Down
44 changes: 32 additions & 12 deletions test/suite/Icecave/Chrono/Format/DefaultFormatterTest.php
@@ -1,6 +1,7 @@
<?php
namespace Icecave\Chrono\Format;

use Eloquent\Liberator\Liberator;
use Icecave\Chrono\Date;
use Icecave\Chrono\DateTime;
use Icecave\Chrono\TimeOfDay;
Expand All @@ -11,6 +12,8 @@ class DefaultFormatterTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
Liberator::liberateClass(__NAMESPACE__ . '\DefaultFormatter')->instance = null;

$this->_formatter = new DefaultFormatter;
$this->_specialChars = 'dDjlNSwzWFmMntLoYyaABgGhHisueIOPTZcrU';
$this->_escapedChars = '\\d\\D\\j\\l\\N\\S\\w\\z\\W\\F\\m\\M\\n\\t\\L\\o\\Y\\y\\a\\A\\B\\g\\G\\h\\H\\i\\s\\u\\e\\I\\O\\P\\T\\Z\\c\\r\\U';
Expand Down Expand Up @@ -180,16 +183,33 @@ public function dateTimeFormats()
);
}

// /**
// * @dataProvider timeFormats
// */
// public function testFormatTimeZone($formatSpecifier, $expected)
// {
// $this->assertSame($expected, $this->_formatter->formatTimeZone($this->_timeZone, $formatSpecifier));
// }
//
// public function timeZoneFormats()
// {
// return array();
// }
/**
* @dataProvider timeZoneFormats
*/
public function testFormatTimeZone($formatSpecifier, $expected)
{
$timeZone = new TimeZone(34200, true);
$this->assertSame($expected, $this->_formatter->formatTimeZone($timeZone, $formatSpecifier));
}

public function timeZoneFormats()
{
return array(
// 'timezone identifier' => array('e', '???'), // not currently supported
'daylight savings' => array('I', '1'), // need to test DST
'timezone offset' => array('O', '+0930'), // need to test negative
'timezone offset colon' => array('P', '+09:30'), // need to test negative
// 'timezone abbreviation' => array('T', '???'), // not currently supported
'timezone seconds' => array('Z', '34200'), // need to test negative
);
}

public function testInstance()
{
$a = DefaultFormatter::instance();
$b = DefaultFormatter::instance();

$this->assertInstanceOf(__NAMESPACE__ . '\DefaultFormatter', $a);
$this->assertSame($a, $b);
}
}

0 comments on commit 949d0f8

Please sign in to comment.