Skip to content

Commit

Permalink
Merge pull request zendframework#3375 branch 'hotfix/hotfix/iso8601-f…
Browse files Browse the repository at this point in the history
…actrion-of-seconds'
  • Loading branch information
Maks3w committed Jan 9, 2013
2 parents 8390015 + 92a7a63 commit fce9fa3
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 10 deletions.
6 changes: 3 additions & 3 deletions library/Zend/Feed/Reader/Extension/Atom/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@

namespace Zend\Feed\Reader\Extension\Atom;

use DateTime;
use DOMDocument;
use DOMElement;
use stdClass;
use Zend\Feed\Reader;
use Zend\Feed\Reader\Collection;
use Zend\Feed\Reader\Extension;
use Zend\Stdlib\DateTime;
use Zend\Uri;

/**
Expand Down Expand Up @@ -175,7 +175,7 @@ public function getDateCreated()
}

if ($dateCreated) {
$date = DateTime::createFromFormat(DateTime::ISO8601, $dateCreated);
$date = DateTime::createFromISO8601($dateCreated);
}

$this->data['datecreated'] = $date;
Expand Down Expand Up @@ -203,7 +203,7 @@ public function getDateModified()
}

if ($dateModified) {
$date = DateTime::createFromFormat(DateTime::ISO8601, $dateModified);
$date = DateTime::createFromISO8601($dateModified);
}

$this->data['datemodified'] = $date;
Expand Down
6 changes: 3 additions & 3 deletions library/Zend/Feed/Reader/Extension/Atom/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

namespace Zend\Feed\Reader\Extension\Atom;

use DateTime;
use DOMElement;
use Zend\Feed\Reader;
use Zend\Feed\Reader\Collection;
use Zend\Feed\Reader\Extension;
use Zend\Stdlib\DateTime;
use Zend\Uri;

/**
Expand Down Expand Up @@ -125,7 +125,7 @@ public function getDateCreated()
}

if ($dateCreated) {
$date = DateTime::createFromFormat(DateTime::ISO8601, $dateCreated);
$date = DateTime::createFromISO8601($dateCreated);
}

$this->data['datecreated'] = $date;
Expand Down Expand Up @@ -153,7 +153,7 @@ public function getDateModified()
}

if ($dateModified) {
$date = DateTime::createFromFormat(DateTime::ISO8601, $dateModified);
$date = DateTime::createFromISO8601($dateModified);
}

$this->data['datemodified'] = $date;
Expand Down
4 changes: 2 additions & 2 deletions library/Zend/Feed/Reader/Extension/DublinCore/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

namespace Zend\Feed\Reader\Extension\DublinCore;

use DateTime;
use Zend\Feed\Reader;
use Zend\Feed\Reader\Collection;
use Zend\Feed\Reader\Extension;
use Zend\Stdlib\DateTime;

/**
* @category Zend
Expand Down Expand Up @@ -222,7 +222,7 @@ public function getDate()
}

if ($date) {
$d = DateTime::createFromFormat(DateTime::ISO8601, $date);
$d = DateTime::createFromISO8601($date);
}

$this->data['date'] = $d;
Expand Down
4 changes: 2 additions & 2 deletions library/Zend/Feed/Reader/Extension/DublinCore/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

namespace Zend\Feed\Reader\Extension\DublinCore;

use DateTime;
use Zend\Feed\Reader;
use Zend\Feed\Reader\Collection;
use Zend\Feed\Reader\Extension;
use Zend\Stdlib\DateTime;

/**
* @category Zend
Expand Down Expand Up @@ -231,7 +231,7 @@ public function getDate()
}

if ($date) {
$d = DateTime::createFromFormat(DateTime::ISO8601, $date);
$d = DateTime::createFromISO8601($date);
}

$this->data['date'] = $d;
Expand Down
48 changes: 48 additions & 0 deletions library/Zend/Stdlib/DateTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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_Stdlib
*/

namespace Zend\Stdlib;

use DateTimeZone;

/**
* DateTime
*
* An extension of the \DateTime object.
*
* @category Zend
* @package Zend_Stdlib
*/
class DateTime extends \DateTime
{
/**
* The DateTime::ISO8601 constant used by php's native DateTime object does
* not allow for fractions of a second. This function better handles ISO8601
* formatted date strings.
*
* @param string $time
* @param DateTimeZone $timezone
* @return mixed
*/
public static function createFromISO8601($time, DateTimeZone $timezone = null)
{
$format = self::ISO8601;
if (isset($time[19]) && $time[19] === '.') {
$format = 'Y-m-d\TH:i:s.uO';
}

if ($timezone !== null) {
return self::createFromFormat($format, $time, $timezone);
}

return self::createFromFormat($format, $time);
}

}
44 changes: 44 additions & 0 deletions tests/ZendTest/Stdlib/DateTimeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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_Stdlib
*/

namespace ZendTest\Stdlib;

use Zend\Stdlib\DateTime;

/**
* @category Zend
* @package Zend_Stdlib
* @subpackage UnitTests
* @group Zend_Stdlib
*/
class DateTimeTest extends \PHPUnit_Framework_TestCase
{
public $dateTime;

public function testCreatesIS08601WithoutFractionalSeconds()
{
$time = '2009-03-07T08:03:50Z';

$date = DateTime::createFromISO8601($time);

$this->assertEquals( \DateTime::createFromFormat(\DateTime::ISO8601, $time), $date);
}

public function testCreatesIS08601WithFractionalSeconds()
{
$time = '2009-03-07T08:03:50.012Z';

$date = DateTime::createFromISO8601($time);

$standard = \DateTime::createFromFormat('Y-m-d\TH:i:s.uO', $time);

$this->assertEquals( $standard, $date);
}
}

0 comments on commit fce9fa3

Please sign in to comment.