Browse Source

Adding locale aware parsing to the TimeType class

Jose Lorenzo Rodriguez 11 years ago
parent
commit
02906c9671
2 changed files with 24 additions and 0 deletions
  1. 9 0
      src/Database/Type/TimeType.php
  2. 15 0
      tests/TestCase/Database/Type/TimeTypeTest.php

+ 9 - 0
src/Database/Type/TimeType.php

@@ -28,4 +28,13 @@ class TimeType extends \Cake\Database\Type\DateTimeType
      * @var string
      */
     protected $_format = 'H:i:s';
+
+    /**
+     * {@inheritDoc}
+     */
+    protected function _parseValue($value)
+    {
+        $class = static::$dateTimeClass;
+        return $class::parseTime($value, $this->_localeFormat);
+    }
 }

+ 15 - 0
tests/TestCase/Database/Type/TimeTypeTest.php

@@ -167,4 +167,19 @@ class TimeTypeTest extends TestCase
             $this->assertSame($expected, $result);
         }
     }
+
+    /**
+     * Tests marshalling dates using the locale aware parser
+     *
+     * @return void
+     */
+    public function testMarshalWithLocaleParsing()
+    {
+        $this->type->useLocaleParser();
+        $expected = new Time('23:23:00');
+        $result = $this->type->marshal('11:23pm');
+        $this->assertEquals($expected->format('H:i'), $result->format('H:i'));
+
+        $this->assertNull($this->type->marshal('derp:23'));
+    }
 }