util: generalize and simplify time::{to,from}UnixTimestamp()
Change-Id: I1dc94365944b8efc03f32919d32d5df0a652b672
diff --git a/ndn-cxx/util/time.cpp b/ndn-cxx/util/time.cpp
index ba9960a..8ad9310 100644
--- a/ndn-cxx/util/time.cpp
+++ b/ndn-cxx/util/time.cpp
@@ -104,22 +104,10 @@
const system_clock::time_point&
getUnixEpoch()
{
- static constexpr system_clock::time_point epoch(seconds::zero());
+ static constexpr system_clock::time_point epoch;
return epoch;
}
-milliseconds
-toUnixTimestamp(const system_clock::time_point& point)
-{
- return duration_cast<milliseconds>(point - getUnixEpoch());
-}
-
-system_clock::time_point
-fromUnixTimestamp(milliseconds duration)
-{
- return getUnixEpoch() + duration;
-}
-
static boost::posix_time::ptime
convertToPosixTime(const system_clock::time_point& timePoint)
{
diff --git a/ndn-cxx/util/time.hpp b/ndn-cxx/util/time.hpp
index be50c4b..726f792 100644
--- a/ndn-cxx/util/time.hpp
+++ b/ndn-cxx/util/time.hpp
@@ -266,16 +266,23 @@
getUnixEpoch();
/**
- * \brief Convert system_clock::time_point to UNIX timestamp
+ * \brief Convert system_clock::time_point to UNIX timestamp.
*/
-milliseconds
-toUnixTimestamp(const system_clock::time_point& point);
+template<typename Duration = milliseconds>
+constexpr Duration
+toUnixTimestamp(const system_clock::time_point& tp)
+{
+ return duration_cast<Duration>(tp.time_since_epoch());
+}
/**
- * \brief Convert UNIX timestamp to system_clock::time_point
+ * \brief Convert UNIX timestamp to system_clock::time_point.
*/
-system_clock::time_point
-fromUnixTimestamp(milliseconds duration);
+constexpr system_clock::time_point
+fromUnixTimestamp(system_clock::duration d)
+{
+ return system_clock::time_point{d};
+}
/**
* \brief Convert to the ISO 8601 string representation, basic format (`YYYYMMDDTHHMMSS,fffffffff`).
diff --git a/tests/unit/util/time.t.cpp b/tests/unit/util/time.t.cpp
index 9004700..84236d2 100644
--- a/tests/unit/util/time.t.cpp
+++ b/tests/unit/util/time.t.cpp
@@ -88,15 +88,18 @@
BOOST_TEST(value > referenceTime);
- BOOST_CHECK_EQUAL(toIsoString(referenceTime), "20140129T034247.032000");
- BOOST_CHECK_EQUAL(toIsoExtendedString(referenceTime), "2014-01-29T03:42:47.032000");
- BOOST_CHECK_EQUAL(toString(referenceTime), "2014-01-29 03:42:47");
+ BOOST_TEST(toUnixTimestamp(referenceTime) == 1390966967032_ms);
+ BOOST_TEST(toUnixTimestamp<nanoseconds>(referenceTime) == 1390966967032000000_ns);
+ BOOST_TEST(toUnixTimestamp<seconds>(referenceTime) == 1390966967_s);
+
+ BOOST_TEST(toIsoString(referenceTime) == "20140129T034247.032000");
+ BOOST_TEST(toIsoExtendedString(referenceTime) == "2014-01-29T03:42:47.032000");
+ BOOST_TEST(toString(referenceTime) == "2014-01-29 03:42:47");
// Unfortunately, not all systems has lv_LV locale installed :(
- // BOOST_CHECK_EQUAL(toString(referenceTime, "%Y. gada %d. %B", std::locale("lv_LV.UTF-8")),
- // "2014. gada 29. Janvāris");
- BOOST_CHECK_EQUAL(toString(referenceTime, "%Y -- %d -- %B", std::locale("C")),
- "2014 -- 29 -- January");
+ // BOOST_TEST(toString(referenceTime, "%Y. gada %d. %B", std::locale("lv_LV.UTF-8")) ==
+ // "2014. gada 29. Janvāris");
+ BOOST_TEST(toString(referenceTime, "%Y -- %d -- %B", std::locale("C")) == "2014 -- 29 -- January");
BOOST_TEST(fromIsoString("20140129T034247.032000") == referenceTime);
BOOST_TEST(fromIsoString("20140129T034247.032000Z") == referenceTime);
@@ -110,10 +113,10 @@
BOOST_TEST(fromString("2014-01-29 03:42:47") == fromUnixTimestamp(1390966967_s));
// Unfortunately, not all systems has lv_LV locale installed :(
- // BOOST_CHECK_EQUAL(fromString("2014. gada 29. Janvāris", "%Y. gada %d. %B", std::locale("lv_LV.UTF-8")),
- // fromUnixTimestamp(1390953600_s));
- BOOST_CHECK_EQUAL(fromString("2014 -- 29 -- January", "%Y -- %d -- %B", std::locale("C")),
- fromUnixTimestamp(1390953600_s));
+ // BOOST_TEST(fromString("2014. gada 29. Janvāris", "%Y. gada %d. %B", std::locale("lv_LV.UTF-8")) ==
+ // fromUnixTimestamp(1390953600_s));
+ BOOST_TEST(fromString("2014 -- 29 -- January", "%Y -- %d -- %B", std::locale("C")) ==
+ fromUnixTimestamp(1390953600_s));
}
BOOST_AUTO_TEST_CASE(SteadyClock)