From adece73b0bc4b3f6e6b2d58151bdbf3c8c0939c1 Mon Sep 17 00:00:00 2001 From: Walter Dal Mut Date: Wed, 10 Jun 2015 15:40:10 +0200 Subject: [PATCH] Fixes invalid saved timestamp for UDP/IP --- src/Adapter/UdpAdapter.php | 8 +++-- tests/ClientTest.php | 63 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 3 deletions(-) diff --git a/src/Adapter/UdpAdapter.php b/src/Adapter/UdpAdapter.php index fb9519dff..06477ed3b 100644 --- a/src/Adapter/UdpAdapter.php +++ b/src/Adapter/UdpAdapter.php @@ -1,6 +1,7 @@ format("U") * 1e9); if (array_key_exists("time", $message)) { - $dt = new \DateTime($message["time"]); - $unixepoch = $dt->format("U"); + $dt = new DateTime($message["time"]); + $unixepoch = (int)($dt->format("U") * 1e9); } $lines = []; diff --git a/tests/ClientTest.php b/tests/ClientTest.php index f868a4cfc..1bfb33db3 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -1,6 +1,7 @@ assertEquals(712423, $body["results"][0]["series"][0]["values"][0][2]); } + /** + * @group udp + * @group date + */ + public function testWriteDirectMessageWillPreserveActualTime() + { + $object = $this->createClientWithUdpAdapter(); + + $object->mark([ + "points" => [ + [ + "measurement" => "vm-serie", + "fields" => [ + "cpu" => 18.12, + "free" => 712423, + ], + ], + ] + ]); + + sleep(2); + + $this->options->setDatabase("udp.test"); + $body = $this->object->query("select * from \"vm-serie\""); + + $this->assertCount(1, $body["results"][0]["series"][0]["values"]); + $this->assertEquals("time", $body["results"][0]["series"][0]["columns"][0]); + $saved = new DateTime($body["results"][0]["series"][0]["values"][0][0]); + $this->assertEquals(date("Y-m-d"), $saved->format("Y-m-d")); + } + + /** + * @group udp + * @group date + */ + public function testWriteDirectMessageWillPreserveDatetime() + { + $object = $this->createClientWithUdpAdapter(); + + $object->mark([ + "time" => "2009-11-10T23:00:00Z", + "points" => [ + [ + "measurement" => "vm-serie", + "fields" => [ + "cpu" => 18.12, + "free" => 712423, + ], + ], + ] + ]); + + sleep(2); + + $this->options->setDatabase("udp.test"); + $body = $this->object->query("select * from \"vm-serie\""); + + $this->assertCount(1, $body["results"][0]["series"][0]["values"]); + $this->assertEquals("time", $body["results"][0]["series"][0]["columns"][0]); + $this->assertEquals("2009-11-10T23:00:00Z", $body["results"][0]["series"][0]["values"][0][0]); + } + /** * @group udp * @group tags