Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 0 deletions.
82 changes: 82 additions & 0 deletions src/DateTimeFormatter.php
@@ -0,0 +1,82 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Filter;

use DateTime;

class DateTimeFormatter extends AbstractFilter
{
/**
* A valid format string accepted by date()
*
* @var string
*/
protected $format = DateTime::ISO8601;

/**
* Sets filter options
*
* @param array|Traversable $options
*/
public function __construct($options = null)
{
if ($options) {
$this->setOptions($options);
}
}

/**
* Set the format string accepted by date() to use when formatting a string
*
* @param string $format
* @return \Zend\Filter\DateTimeFormatter
*/
public function setFormat($format)
{
$this->format = $format;
return $this;
}

/**
* Filter a datetime string by normalizing it to the filters specified format
*
* @param string $value
* @throws Exception\InvalidArgumentException
* @return string
*/
public function filter($value)
{
try {
$result = $this->normalizeDateTime($value);
} catch (\Exception $ex) {
// DateTime threw an exception, an invalid date string was provided
throw new Exception\InvalidArgumentException($ex);
}

return $result;
}

/**
* Normalize the provided value to a formatted string
*
* @param string|int|DateTime $value
* @returns string
*/
protected function normalizeDateTime($value)
{
if (is_int($value)) {
$dateTime = new DateTime('@' . $value);
} elseif (!$value instanceof DateTime) {
$dateTime = new DateTime($value);
}

return $dateTime->format($this->format);
}
}
86 changes: 86 additions & 0 deletions test/DateTimeFormatterTest.php
@@ -0,0 +1,86 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Filter
*/

namespace ZendTest\Filter;

use DateTime;
use Zend\Filter\DateTimeFormatter;

/**
* @category Zend
* @package Zend_Filter
* @subpackage UnitTests
* @group Zend_Filter
*/
class DateTimeFormatterTest extends \PHPUnit_Framework_TestCase
{
protected $defaultTimezone;

public function setUp()
{
$this->defaultTimezone = date_default_timezone_get();
}

public function tearDown()
{
date_default_timezone_set($this->defaultTimezone);
}

public function testDateTimeFormatted()
{
date_default_timezone_set('UTC');

$filter = new DateTimeFormatter();
$result = $filter->filter('2012-01-01');
$this->assertEquals('2012-01-01T00:00:00+0000', $result);
}

public function testDateTimeFormattedWithAlternateTimezones()
{
$filter = new DateTimeFormatter();

date_default_timezone_set('Europe/Paris');

$resultParis = $filter->filter('2012-01-01');
$this->assertEquals('2012-01-01T00:00:00+0100', $resultParis);

date_default_timezone_set('America/New_York');

$resultNewYork = $filter->filter('2012-01-01');
$this->assertEquals('2012-01-01T00:00:00-0500', $resultNewYork);
}

public function testSetFormat()
{
date_default_timezone_set('UTC');

$filter = new DateTimeFormatter();
$filter->setFormat(DateTime::RFC1036);
$result = $filter->filter('2012-01-01');
$this->assertEquals('Sun, 01 Jan 12 00:00:00 +0000', $result);
}

public function testFormatDateTimeFromTimestamp()
{
date_default_timezone_set('UTC');

$filter = new DateTimeFormatter();
$result = $filter->filter(1359739801);
$this->assertEquals('2013-02-01T17:30:01+0000', $result);
}

public function testInvalidArgumentExceptionThrownOnInvalidInput()
{
$this->setExpectedException('Zend\Filter\Exception\InvalidArgumentException');

$filter = new DateTimeFormatter();
$result = $filter->filter('2013-31-31');
}
}

0 comments on commit 0f73b43

Please sign in to comment.