ZF-12023: Zend_Feed_Writer_Feed does not accept UNIX timestamps which are not 10 digits

Description

There are 3 methods in Zend_Feed_Writer_Feed_FeedAbstract which try to build a Zend_Date object by timestamp. The problem lies within the recognition of the timestamp value. There is a disputable presumption about the proper length of a timestamp string. Only strings which are 10 characters long (and containing only digits) are considered to be an timestamp. That leads to problems with dates before 2001-09-10 because their timestamps only have the string length of 9 characters.

The methods are:

setDateCreated() setDateModified() setLastBuildDate()

Comments

Confirmed. Example: (http://framework.zend.com/code/blame.php/…)


    public function setDateCreated($date = null)
    {
        $zdate = null;
        if ($date === null) {
            $zdate = new Zend_Date;
        } elseif (ctype_digit($date) && strlen($date) == 10) {
            $zdate = new Zend_Date($date, Zend_Date::TIMESTAMP);
        } elseif ($date instanceof Zend_Date) {
            $zdate = $date;
        } else {
            require_once 'Zend/Feed/Exception.php';
            throw new Zend_Feed_Exception('Invalid Zend_Date object or UNIX Timestamp passed as parameter');
        }
        $this->_data['dateCreated'] = $zdate;
    }

Tests:


Index: tests/Zend/Feed/Writer/FeedTest.php
===================================================================
--- tests/Zend/Feed/Writer/FeedTest.php (revision 24636)
+++ tests/Zend/Feed/Writer/FeedTest.php (working copy)
@@ -195,6 +195,17 @@
         $myDate = new Zend_Date('1234567890', Zend_Date::TIMESTAMP);
         $this->assertTrue($myDate->equals($writer->getDateCreated()));
     }
+
+    /**
+     * @group ZF-12023
+     */
+    public function testSetDateCreatedUsesGivenUnixTimestampThatIsLessThanTenDigits()
+    {
+        $writer = new Zend_Feed_Writer_Feed;
+        $writer->setDateCreated(123456789);
+        $myDate = new Zend_Date('123456789', Zend_Date::TIMESTAMP);
+        $this->assertTrue($myDate->equals($writer->getDateCreated()));
+    }

     public function testSetDateCreatedUsesZendDateObject()
     {
@@ -220,6 +231,17 @@
         $this->assertTrue($myDate->equals($writer->getDateModified()));
     }

+    /**
+     * @group ZF-12023
+     */
+    public function testSetDateModifiedUsesGivenUnixTimestampThatIsLessThanTenDigits()
+    {
+        $writer = new Zend_Feed_Writer_Feed;
+        $writer->setDateModified(123456789);
+        $myDate = new Zend_Date('123456789', Zend_Date::TIMESTAMP);
+        $this->assertTrue($myDate->equals($writer->getDateModified()));
+    }
+
     public function testSetDateModifiedUsesZendDateObject()
     {
         $writer = new Zend_Feed_Writer_Feed;
@@ -276,6 +298,17 @@
         $this->assertTrue($myDate->equals($writer->getLastBuildDate()));
     }

+    /**
+     * @group ZF-12023
+     */
+    public function testSetLastBuildDateUsesGivenUnixTimestampThatIsLessThanTenDigits()
+    {
+        $writer = new Zend_Feed_Writer_Feed;
+        $writer->setLastBuildDate(123456789);
+        $myDate = new Zend_Date('123456789', Zend_Date::TIMESTAMP);
+        $this->assertTrue($myDate->equals($writer->getLastBuildDate()));
+    }
+
     public function testSetLastBuildDateUsesZendDateObject()
     {
         $writer = new Zend_Feed_Writer_Feed;

Result is a bunch of these:


1) Zend_Feed_Writer_FeedTest::testSetXXXXXUsesGivenUnixTimestampNotTenDigits
Zend_Feed_Exception: Invalid Zend_Date object or UNIX Timestamp passed as parameter

Fix:


Index: library/Zend/Feed/Writer/Feed/FeedAbstract.php
===================================================================
--- library/Zend/Feed/Writer/Feed/FeedAbstract.php      (revision 24636)
+++ library/Zend/Feed/Writer/Feed/FeedAbstract.php      (working copy)
@@ -176,7 +176,7 @@
         $zdate = null;
         if ($date === null) {
             $zdate = new Zend_Date;
-        } elseif (ctype_digit($date) && strlen($date) == 10) {
+        } elseif (ctype_digit($date)) {
             $zdate = new Zend_Date($date, Zend_Date::TIMESTAMP);
         } elseif ($date instanceof Zend_Date) {
             $zdate = $date;
@@ -197,7 +197,7 @@
         $zdate = null;
         if ($date === null) {
             $zdate = new Zend_Date;
-        } elseif (ctype_digit($date) && strlen($date) == 10) {
+        } elseif (ctype_digit($date)) {
             $zdate = new Zend_Date($date, Zend_Date::TIMESTAMP);
         } elseif ($date instanceof Zend_Date) {
             $zdate = $date;
@@ -218,7 +218,7 @@
         $zdate = null;
         if ($date === null) {
             $zdate = new Zend_Date;
-        } elseif (ctype_digit($date) && strlen($date) == 10) {
+        } elseif (ctype_digit($date)) {
             $zdate = new Zend_Date($date, Zend_Date::TIMESTAMP);
         } elseif ($date instanceof Zend_Date) {
             $zdate = $date;

NOTE: The above fix will fail for REALLY small dates (ie: < 256s after epoch) due to ZF-11610 (which I will fix nowish)

Fixed in trunk r24639 Merged to release-1.11 in r24640 ZF2 PR #840: https://github.com/zendframework/zf2/pull/840