Browse Source

Merge pull request #10587 from cakephp/master-iso-datetime

Allow extended ISO datetime format to be marshalled.
Mark Story 8 years ago
parent
commit
29e13255fb

+ 25 - 4
src/Database/Type/DateTimeType.php

@@ -53,9 +53,12 @@ class DateTimeType extends Type implements TypeInterface
     /**
      * String format to use for DateTime parsing
      *
-     * @var string
+     * @var string|array
      */
-    protected $_format = 'Y-m-d H:i:s';
+    protected $_format = [
+        'Y-m-d H:i:s',
+        'Y-m-d\TH:i:sP',
+    ];
 
     /**
      * Whether dates should be parsed using a locale aware parser
@@ -114,7 +117,9 @@ class DateTimeType extends Type implements TypeInterface
             $value = new $class('@' . $value);
         }
 
-        return $value->format($this->_format);
+        $format = (array)$this->_format;
+
+        return $value->format(array_shift($format));
     }
 
     /**
@@ -165,7 +170,7 @@ class DateTimeType extends Type implements TypeInterface
                 $date = new $class($value);
                 $compare = true;
             }
-            if ($compare && $date && $date->format($this->_format) !== $value) {
+            if ($compare && $date && !$this->_compare($date, $value)) {
                 return $value;
             }
             if ($date) {
@@ -206,6 +211,22 @@ class DateTimeType extends Type implements TypeInterface
     }
 
     /**
+     * @param \Cake\I18n\Time|\DateTime $date DateTime object
+     * @param mixed $value Request data
+     * @return bool
+     */
+    protected function _compare($date, $value)
+    {
+        foreach ((array)$this->_format as $format) {
+            if ($date->format($format) === $value) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+    /**
      * Sets whether or not to parse dates passed to the marshal() function
      * by using a locale aware parser.
      *

+ 1 - 1
src/Database/Type/DateType.php

@@ -34,7 +34,7 @@ class DateType extends DateTimeType
     /**
      * Date format for DateTime object
      *
-     * @var string
+     * @var string|array
      */
     protected $_format = 'Y-m-d';
 

+ 1 - 1
src/Database/Type/TimeType.php

@@ -25,7 +25,7 @@ class TimeType extends DateTimeType
     /**
      * Time format for DateTime object
      *
-     * @var string
+     * @var string|array
      */
     protected $_format = 'H:i:s';
 

+ 1 - 0
tests/TestCase/Database/Type/DateTimeTypeTest.php

@@ -151,6 +151,7 @@ class DateTimeTypeTest extends TestCase
             [1392387900, new Time('@1392387900')],
             ['2014-02-14 00:00:00', new Time('2014-02-14 00:00:00')],
             ['2014-02-14 13:14:15', new Time('2014-02-14 13:14:15')],
+            ['2017-04-05T17:18:00+00:00', new Time('2017-04-05T17:18:00+00:00')],
 
             // valid array types
             [