Przeglądaj źródła

[ZF-11992] Fixed issue with first day of year

- Internally, Zend_Date uses the ISO 8601 format "o" for the year. However, this
  is problematic; if the day requested falls in the week number of the last week
  of the previous year, the previous year will be reported. Added a preg_replace
  to change "o" to "Y" when not preceded by an escape.

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24880 44c647ce-9c0f-0410-b52a-842ac1e357ba
matthew 13 lat temu
rodzic
commit
7872615bcf

+ 3 - 5
library/Zend/Date.php

@@ -2644,10 +2644,8 @@ class Zend_Date extends Zend_Date_DateObject
                                 $parsed['day'] = 0;
                             }
 
-                            if (isset($parsed['year'])) {
-                                $parsed['year'] -= 1970;
-                            } else {
-                                $parsed['year'] = 0;
+                            if (!isset($parsed['year'])) {
+                                $parsed['year'] = 1970;
                             }
                         }
 
@@ -2657,7 +2655,7 @@ class Zend_Date extends Zend_Date_DateObject
                             isset($parsed['second']) ? $parsed['second'] : 0,
                             isset($parsed['month']) ? (1 + $parsed['month']) : 1,
                             isset($parsed['day']) ? (1 + $parsed['day']) : 1,
-                            isset($parsed['year']) ? (1970 + $parsed['year']) : 1970,
+                            $parsed['year'],
                             false), $this->getUnixTimestamp(), false);
                     } catch (Zend_Locale_Exception $e) {
                         if (!is_numeric($date)) {

+ 7 - 0
library/Zend/Date/DateObject.php

@@ -312,6 +312,13 @@ abstract class Zend_Date_DateObject {
         }
 
         if (abs($timestamp) <= 0x7FFFFFFF) {
+            // See ZF-11992
+            // "o" will sometimes resolve to the previous year (see 
+            // http://php.net/date ; it's part of the ISO 8601 
+            // standard). However, this is not desired, so replacing 
+            // all occurrences of "o" not preceded by a backslash 
+            // with "Y"
+            $format = preg_replace('/(?<!\\\\)o\b/', 'Y', $format);
             $result = ($gmt) ? @gmdate($format, $timestamp) : @date($format, $timestamp);
             date_default_timezone_set($oldzone);
             return $result;

+ 9 - 0
tests/Zend/DateTest.php

@@ -5689,6 +5689,15 @@ class Zend_DateTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('Etc/GMT-14', $date->getTimezoneFromString('18:00:00+1400'));
     }
 
+    /**
+     * @group ZF-11992
+     */
+    public function testDateShouldMatchOnFirstDayOfYear()
+    {
+        $date = new Zend_Date('01.01.2012');
+        $out  = $date->toString('Y-MM-dd');
+        $this->assertEquals('2012-01-01', $out);
+    }
 }
 
 class Zend_Date_TestHelper extends Zend_Date