Browse Source

Proposed enhancement for issue #9083

Also spelling correction on test method name
lilHermit 9 years ago
parent
commit
e9847b244b
4 changed files with 22 additions and 1 deletions
  1. 4 0
      src/I18n/DateFormatTrait.php
  2. 7 0
      src/I18n/FrozenTime.php
  3. 7 0
      src/I18n/Time.php
  4. 4 1
      tests/TestCase/I18n/TimeTest.php

+ 4 - 0
src/I18n/DateFormatTrait.php

@@ -389,6 +389,10 @@ trait DateFormatTrait
      */
      */
     public function jsonSerialize()
     public function jsonSerialize()
     {
     {
+        if (static::$_jsonEncodeFormat === Time::UNIX_TIMESTAMP_FORMAT) {
+            return $this->getTimestamp();
+        }
+
         return $this->i18nFormat(static::$_jsonEncodeFormat);
         return $this->i18nFormat(static::$_jsonEncodeFormat);
     }
     }
 
 

+ 7 - 0
src/I18n/FrozenTime.php

@@ -98,6 +98,13 @@ class FrozenTime extends Chronos implements JsonSerializable
     public static $wordEnd = '+1 month';
     public static $wordEnd = '+1 month';
 
 
     /**
     /**
+     * serialise the value as a Unix Timestamp
+     *
+     * @var string
+     */
+    const UNIX_TIMESTAMP_FORMAT = 'unixTimestampFormat';
+
+    /**
      * {@inheritDoc}
      * {@inheritDoc}
      */
      */
     public function __construct($time = null, $tz = null)
     public function __construct($time = null, $tz = null)

+ 7 - 0
src/I18n/Time.php

@@ -96,6 +96,13 @@ class Time extends MutableDateTime implements JsonSerializable
     public static $wordEnd = '+1 month';
     public static $wordEnd = '+1 month';
 
 
     /**
     /**
+     * serialise the value as a Unix Timestamp
+     *
+     * @var string
+     */
+    const UNIX_TIMESTAMP_FORMAT = 'unixTimestampFormat';
+
+    /**
      * {@inheritDoc}
      * {@inheritDoc}
      */
      */
     public function __construct($time = null, $tz = null)
     public function __construct($time = null, $tz = null)

+ 4 - 1
tests/TestCase/I18n/TimeTest.php

@@ -707,7 +707,7 @@ class TimeTest extends TestCase
      * @dataProvider classNameProvider
      * @dataProvider classNameProvider
      * @return void
      * @return void
      */
      */
-    public function testJsonEnconde($class)
+    public function testJsonEncode($class)
     {
     {
         if (version_compare(INTL_ICU_VERSION, '50.0', '<')) {
         if (version_compare(INTL_ICU_VERSION, '50.0', '<')) {
             $this->markTestSkipped('ICU 5x is needed');
             $this->markTestSkipped('ICU 5x is needed');
@@ -718,6 +718,9 @@ class TimeTest extends TestCase
 
 
         $class::setJsonEncodeFormat('yyyy-MM-dd HH:mm:ss');
         $class::setJsonEncodeFormat('yyyy-MM-dd HH:mm:ss');
         $this->assertEquals('"2014-04-20 10:10:10"', json_encode($time));
         $this->assertEquals('"2014-04-20 10:10:10"', json_encode($time));
+
+        $class::setJsonEncodeFormat($class::UNIX_TIMESTAMP_FORMAT);
+        $this->assertEquals('1397988610', json_encode($time));
     }
     }
 
 
     /**
     /**