Browse Source

Fix errors reading postgres timestamp columns.

Timestamp columns from postgres often include the milliseconds. Since
that data cannot be stored in DateTime instances, we should chop it off.

Fixes #3087
mark_story 12 years ago
parent
commit
8941f646aa

+ 2 - 2
src/Database/Type/DateTimeType.php

@@ -56,8 +56,8 @@ class DateTimeType extends \Cake\Database\Type {
 		if ($value === null) {
 			return null;
 		}
-		$value = DateTime::createFromFormat($this->_format, $value);
-		return $value;
+		list($value) = explode('.', $value);
+		return DateTime::createFromFormat($this->_format, $value);
 	}
 
 /**

+ 14 - 2
tests/TestCase/Database/Type/DateTimeTypeTest.php

@@ -1,7 +1,5 @@
 <?php
 /**
- * PHP Version 5.4
- *
  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  *
@@ -55,6 +53,20 @@ class DateTimeTypeTest extends TestCase {
 	}
 
 /**
+ * Test datetime parsing when value include milliseconds.
+ *
+ * Postgres includes milliseconds in timestamp columns,
+ * data from those columns should work.
+ *
+ * @return void
+ */
+	public function testToPHPIncludingMilliseconds() {
+		$in = '2014-03-24 20:44:36.315113';
+		$result = $this->type->toPHP($in, $this->driver);
+		$this->assertInstanceOf('\DateTime', $result);
+	}
+
+/**
  * Test converting to database format
  *
  * @return void