From 9f66d1dac48eb84c6c1f41918161eb0241a229f2 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Wed, 27 Jun 2018 17:52:49 +1200 Subject: [PATCH 1/2] FIX: Datetime takes precedence of Timestamp Not sure if this is intentional, but it seems to be more in line with the docs and the commented-out test. Although Timestamp is still a valid handler, Datetime takes precendence, which means that Datetime objects are returned by the transit reader. --- src/transit/Transit.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transit/Transit.php b/src/transit/Transit.php index f924b7d..8be6167 100644 --- a/src/transit/Transit.php +++ b/src/transit/Transit.php @@ -53,11 +53,11 @@ private function registerDefaultHandlers() { $this->registerHandler(new SymbolHandler()); $this->registerHandler(new SetHandler()); $this->registerHandler(new ListHandler()); + $this->registerHandler(new TimestampHandler()); $this->registerHandler(new DateTimeHandler()); $this->registerHandler(new URIHandler()); $this->registerHandler(new UUIDHandler()); $this->registerHandler(new CharHandler()); - $this->registerHandler(new TimestampHandler()); $this->registerHandler(new ArbitraryPrecisionDecimalHandler()); $this->registerHandler(new ArbitraryPrecisionIntegerHandler()); } From 155d1a1e97526ed6b42f45790647361fb0959100 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sam=20Minn=C3=A9e?= Date: Wed, 27 Jun 2018 17:42:54 +1200 Subject: [PATCH 2/2] FIX: Represent dates as msec not seconds The spec at https://github.com/cognitect/transit-format says that points in time should be represented as "int msecs since 1970". getTimestamp() returns seconds, so we need to multiply by 1000. --- src/transit/handlers/DateTimeHandler.php | 6 +++--- test/test.php | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/transit/handlers/DateTimeHandler.php b/src/transit/handlers/DateTimeHandler.php index 57761a3..5ef693e 100644 --- a/src/transit/handlers/DateTimeHandler.php +++ b/src/transit/handlers/DateTimeHandler.php @@ -13,11 +13,11 @@ public function type() { } public function representation($obj) { - return $obj->getTimestamp(); + return $obj->getTimestamp() * 1000; } public function resolve($obj) { - return new \DateTime('@' . $obj); + return new \DateTime('@' . $obj/1000); } -} \ No newline at end of file +} diff --git a/test/test.php b/test/test.php index 0ce593e..5311f1b 100644 --- a/test/test.php +++ b/test/test.php @@ -414,8 +414,8 @@ function r($input) { //------------------------- // extensions -//Assert::equal('["~m482196050"]', w([new DateTime('1985-04-12T23:20:50.52Z')])); -//Assert::equal([new DateTime('1985-04-12T23:20:50.52Z')], r('["~m482196050"]')); +Assert::equal('["~m482196050000"]', w([new DateTime('1985-04-12T23:20:50.52Z')])); +Assert::equal((new DateTime('1985-04-12T23:20:50.52Z'))->getTimestamp(), r('["~m482196050000"]')[0]->getTimestamp()); Assert::equal('["~bYWJj"]', w([new Bytes('abc')])); Assert::equal([new Bytes('abc')], r('["~bYWJj"]'));