Permalink
Browse files

Implemented DateTime::formatDate() [#14]

  • Loading branch information...
1 parent c8c7d13 commit b44a456ff87b7e75e5b382d697e96f8691a69211 @jmalloc jmalloc committed Jan 24, 2013
@@ -48,7 +48,9 @@ public function formatDate(Date $date, $formatSpecifier)
{
$this->typeCheck->formatDate(func_get_args());
- return '';
+ $timeOfDay = new TimeOfDay(0, 0, 0, $date->timeZone());
+
+ return $this->formatDateTime($date->at($timeOfDay), $formatSpecifier);
}
/**
@@ -128,11 +130,11 @@ function ($character) use ($dateTime, $self) {
return sprintf('%03d', ($dateTime->time()->toTimeZone(new TimeZone(3600))->totalSeconds() / 86400) * 1000);
case 'g':
- return $dateTime->hours() % 12;
+ return ($h = $dateTime->hours() % 12) ? $h : 12;
case 'G':
return $dateTime->hours();
case 'h':
- return sprintf('%02d', $dateTime->hours() % 12);
+ return sprintf('%02d', ($h = $dateTime->hours() % 12) ? $h : 12);
case 'H':
return sprintf('%02d', $dateTime->hours());
case 'i':
@@ -163,7 +165,7 @@ function ($character) use ($dateTime, $self) {
return $dateTime->unixTime();
}
- return null;
+ return $character;
}
);
}
@@ -220,12 +222,7 @@ protected function replace($formatSpecifier, $callback)
} elseif (false === strpos(self::SPECIAL_CHARS, $char)) {
$result .= $char;
} else {
- $value = $callback($char);
- if (null === $value) {
- $result .= $char;
- } else {
- $result .= $value;
- }
+ $result .= $callback($char);
}
}
@@ -32,49 +32,64 @@ public function testEscape()
public function testEscapingIsHonoured()
{
- // $this->assertSame($this->_specialChars, $this->_formatter->formatDate($this->_date, $this->_specialChars));
- // $this->assertSame($this->_specialChars, $this->_formatter->formatTimeOfDay($this->_time, $this->_specialChars));
+ $this->assertSame($this->_specialChars, $this->_formatter->formatDate($this->_date, $this->_escapedChars));
+ // do timeOfDay
$this->assertSame($this->_specialChars, $this->_formatter->formatDateTime($this->_dateTime, $this->_escapedChars));
- // $this->assertSame($this->_specialChars, $this->_formatter->formatTimeZone($this->_timeZone, $this->_specialChars));
+ // do timeZone
}
public function testEscapingOfNonSpecialCharacters()
{
- // $this->assertSame($this->_specialChars, $this->_formatter->formatDate($this->_date, $this->_specialChars));
- // $this->assertSame($this->_specialChars, $this->_formatter->formatTimeOfDay($this->_time, $this->_specialChars));
+ $this->assertSame('X', $this->_formatter->formatDate($this->_date, '\X'));
+ // do timeOfDay
$this->assertSame('X', $this->_formatter->formatDateTime($this->_dateTime, '\X'));
- // $this->assertSame($this->_specialChars, $this->_formatter->formatTimeZone($this->_timeZone, $this->_specialChars));
+ // do timeZone
}
public function testEscapingOfBackslash()
{
- // $this->assertSame($this->_specialChars, $this->_formatter->formatDate($this->_date, $this->_specialChars));
- // $this->assertSame($this->_specialChars, $this->_formatter->formatTimeOfDay($this->_time, $this->_specialChars));
+ $this->assertSame('\X', $this->_formatter->formatDate($this->_date, '\\\\X'));
+ // do timeOfDay
$this->assertSame('\X', $this->_formatter->formatDateTime($this->_dateTime, '\\\\X'));
- // $this->assertSame($this->_specialChars, $this->_formatter->formatTimeZone($this->_timeZone, $this->_specialChars));
+ // do timeZone
}
public function testEscapingBackslashAtEnd()
{
- // $this->assertSame($this->_specialChars, $this->_formatter->formatDate($this->_date, $this->_specialChars));
- // $this->assertSame($this->_specialChars, $this->_formatter->formatTimeOfDay($this->_time, $this->_specialChars));
+ $this->assertSame('X\\', $this->_formatter->formatDate($this->_date, 'X\\'));
+ // do timeOfDay
$this->assertSame('X\\', $this->_formatter->formatDateTime($this->_dateTime, 'X\\'));
- // $this->assertSame($this->_specialChars, $this->_formatter->formatTimeZone($this->_timeZone, $this->_specialChars));
+ // do timeZone
+ }
+
+ /**
+ * @dataProvider dateFormats
+ */
+ public function testFormatDate($formatSpecifier, $expected)
+ {
+ $this->assertSame($expected, $this->_formatter->formatDate($this->_date, $formatSpecifier));
+ }
+
+ public function dateFormats()
+ {
+ $formats = array(
+ 'swatch internet time' => array('B', '625'),
+
+ 'unpadded 12hr hours' => array('g', '12'),
+ 'unpadded 24hr hours' => array('G', '0'),
+ 'padded 12hr hours' => array('h', '12'),
+ 'padded 24hr hours' => array('H', '00'),
+ 'padded minutes' => array('i', '00'),
+ 'padded seconds' => array('s', '00'),
+
+ 'ISO-8601 date' => array('c', '2012-06-07T00:00:00+10:00'),
+ 'RFC-2822 date' => array('r', 'Thu, 07 Jun 2012 00:00:00 +1000'),
+ 'unix time' => array('U', '1338991200'),
+ );
+
+ return $formats + $this->dateTimeFormats();
}
- // /**
- // * @dataProvider dateFormats
- // */
- // public function testFormatDate($formatSpecifier, $expected)
- // {
- // $this->assertSame($expected, $this->_formatter->formatDate($this->_date, $formatSpecifier));
- // }
- //
- // public function dateFormats()
- // {
- // return array();
- // }
- //
// /**
// * @dataProvider timeFormats
// */

0 comments on commit b44a456

Please sign in to comment.