From 0c995cb4f9928e51e2e638b5c4c80beaeef00b35 Mon Sep 17 00:00:00 2001 From: Adam Wegrzynek Date: Mon, 4 Nov 2019 02:47:10 +0100 Subject: [PATCH] Allow to set custom timestamp (#36) --- include/Point.h | 3 +++ src/Point.cxx | 6 ++++++ test/testPoint.cxx | 9 +++++++++ 3 files changed, 18 insertions(+) diff --git a/include/Point.h b/include/Point.h index a8a991d1..b59bb5da 100644 --- a/include/Point.h +++ b/include/Point.h @@ -34,6 +34,9 @@ class Point /// Converts point to Influx Line Protocol std::string toLineProtocol() const; + /// Sets custom timestamp + Point&& setTimestamp(std::chrono::time_point timestamp); + protected: /// A value std::variant mValue; diff --git a/src/Point.cxx b/src/Point.cxx index 8277cf78..ff197069 100644 --- a/src/Point.cxx +++ b/src/Point.cxx @@ -44,6 +44,12 @@ Point&& Point::addTag(std::string_view key, std::string_view value) return std::move(*this); } +Point&& Point::setTimestamp(std::chrono::time_point timestamp) +{ + mTimestamp = timestamp; + return std::move(*this); +} + auto Point::getCurrentTimestamp() -> decltype(std::chrono::system_clock::now()) { return std::chrono::system_clock::now(); diff --git a/test/testPoint.cxx b/test/testPoint.cxx index c26772cb..7a660063 100644 --- a/test/testPoint.cxx +++ b/test/testPoint.cxx @@ -51,6 +51,15 @@ BOOST_AUTO_TEST_CASE(test3) BOOST_CHECK_EQUAL(result[1], "value=10i,dvalue=10.1"); } +BOOST_AUTO_TEST_CASE(test4) +{ + auto point = Point{"test"} + .addField("value", 10) + .setTimestamp(std::chrono::time_point(std::chrono::milliseconds(1572830914))); + + auto result = getVector(point); + BOOST_CHECK_EQUAL(result[2], "1572830914000000"); +} } // namespace test } // namespace influxdb