Browse Source

Fix DateTimeTime breaking on timestamp values.

Since we don't have a separate TimestampType the DateTimeType needs to
handle integer values (timestamps).
mark_story 11 years ago
parent
commit
28432272ed

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

@@ -51,7 +51,7 @@ class DateTimeType extends \Cake\Database\Type {
 /**
  * Convert DateTime instance into strings.
  *
- * @param string|DateTime $value The value to convert.
+ * @param string|integer|DateTime $value The value to convert.
  * @param Driver $driver The driver instance to convert with.
  * @return string
  */
@@ -59,6 +59,9 @@ class DateTimeType extends \Cake\Database\Type {
 		if ($value === null || is_string($value)) {
 			return $value;
 		}
+		if (is_int($value)) {
+			$value = new static::$dateTimeClass('@' . $value);
+		}
 		return $value->format($this->_format);
 	}
 

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

@@ -80,6 +80,10 @@ class DateTimeTypeTest extends TestCase {
 		$date = new Time('2013-08-12 15:16:17');
 		$result = $this->type->toDatabase($date, $this->driver);
 		$this->assertEquals('2013-08-12 15:16:17', $result);
+
+		$date = 1401906995;
+		$result = $this->type->toDatabase($date, $this->driver);
+		$this->assertEquals('2014-06-04 18:36:35', $result);
 	}
 
 /**