From 4f74b9aa4b3822a8a8b9d230e07a53755fb7a0af Mon Sep 17 00:00:00 2001 From: Adam Wegrzynek Date: Fri, 10 Apr 2020 19:36:44 +0200 Subject: [PATCH] Make sure timestamp is read back correctly (#68) * Make sure timestamp is read back correctly * Add fix to parsing timestamp when query --- src/InfluxDB.cxx | 2 +- test/testQuery.cxx | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/InfluxDB.cxx b/src/InfluxDB.cxx index f760b8e5..1ed55b63 100644 --- a/src/InfluxDB.cxx +++ b/src/InfluxDB.cxx @@ -104,7 +104,7 @@ std::vector InfluxDB::query(const std::string& query) std::tm tm = {}; std::stringstream ss; ss << value; - ss >> std::get_time(&tm, "%FT%TZ"); + ss >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%SZ"); point.setTimestamp(std::chrono::system_clock::from_time_t(std::mktime(&tm))); continue; } diff --git a/test/testQuery.cxx b/test/testQuery.cxx index 2b156692..7526ee3c 100644 --- a/test/testQuery.cxx +++ b/test/testQuery.cxx @@ -1,7 +1,6 @@ #define BOOST_TEST_MODULE Test InfluxDB Query #define BOOST_TEST_DYN_LINK #include - #include "../include/InfluxDBFactory.h" #include "../src/InfluxDBException.h" @@ -26,6 +25,21 @@ BOOST_AUTO_TEST_CASE(query1) BOOST_CHECK_EQUAL(points[2].getTags(), "host=localhost"); } +BOOST_AUTO_TEST_CASE(timeStampVerify) +{ + double timeZone = 3600; //+1h + + auto influxdb = influxdb::InfluxDBFactory::Get("http://localhost:8086?db=test"); + Point point = Point{"timestampCheck"}.addField("value", 10); + auto timestamp = point.getTimestamp(); + influxdb->write(std::move(point)); + + auto points = influxdb->query("SELECT * from timestampCheck ORDER BY DESC LIMIT 1"); + std::chrono::duration diff = timestamp - points[0].getTimestamp(); + double diffZone = diff.count() - timeZone; + BOOST_CHECK(diffZone < 1); // 1s +} + BOOST_AUTO_TEST_CASE(failedQuery1) { auto influxdb = influxdb::InfluxDBFactory::Get("http://localhost:8086?db=test");