build: Fixing build and unit tests
Use only C++ and boost methods (memory copying, comparison, time
operations), which are guaranteed to be present on all platforms
with C++ and boost libraries.
Change-Id: Ia2883555c55909a4f7a877cf6d5fbd49b12c73c8
diff --git a/src/util/time.hpp b/src/util/time.hpp
index 5e5dc7c..7b42b4a 100644
--- a/src/util/time.hpp
+++ b/src/util/time.hpp
@@ -9,17 +9,28 @@
#define NDN_TIME_HPP
#include "ndn-cpp-dev/common.hpp"
+#include <boost/date_time/posix_time/posix_time.hpp>
namespace ndn {
-MillisecondsSince1970
-ndn_getNowMilliseconds();
+const boost::posix_time::ptime UNIX_EPOCH_TIME =
+ boost::posix_time::ptime (boost::gregorian::date (1970, boost::gregorian::Jan, 1));
-int
-ndn_toIsoString(MillisecondsSince1970 milliseconds, char *isoString);
+/**
+ * @brief Get the current time in milliseconds since 1/1/1970, including fractions of a millisecond
+ */
+inline MillisecondsSince1970
+getNowMilliseconds()
+{
+ return (boost::posix_time::microsec_clock::universal_time() - UNIX_EPOCH_TIME).total_milliseconds();
+}
-int
-ndn_fromIsoString(const char* isoString, MillisecondsSince1970 *milliseconds);
+inline MillisecondsSince1970
+ndn_getNowMilliseconds()
+{
+ return getNowMilliseconds();
+}
+
/**
* Convert to the ISO string representation of the time.
@@ -29,12 +40,13 @@
inline std::string
toIsoString(const MillisecondsSince1970& time)
{
- char isoString[25];
- int error;
- if ((error = ndn_toIsoString(time, isoString)))
- throw std::runtime_error("toIsoString");
-
- return isoString;
+ boost::posix_time::ptime boostTime = UNIX_EPOCH_TIME + boost::posix_time::milliseconds(time);
+
+ /// @todo Determine whether this is necessary at all
+ if ((time % 1000) == 0)
+ return boost::posix_time::to_iso_string(boostTime) + ".000000";
+ else
+ return boost::posix_time::to_iso_string(boostTime);
}
/**
@@ -45,12 +57,9 @@
inline MillisecondsSince1970
fromIsoString(const std::string& isoString)
{
- MillisecondsSince1970 milliseconds;
- int error;
- if ((error = ndn_fromIsoString(isoString.c_str(), &milliseconds)))
- throw std::runtime_error("fromIsoString");
+ boost::posix_time::ptime boostTime = boost::posix_time::from_iso_string(isoString);
- return milliseconds;
+ return (boostTime-UNIX_EPOCH_TIME).total_milliseconds();
}
} // namespace ndn