c++-util: Add helper to convert time to/from string
Change-Id: I7e9910747368c18c039fd28e2b1076843fc4419e
diff --git a/Makefile.am b/Makefile.am
index 375b49a..8a6aaf2 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -135,7 +135,8 @@
src/util/logging.cpp \
src/util/logging.hpp \
src/util/ndnd-id-fetcher.hpp \
- src/util/string-helper.hpp
+ src/util/string-helper.hpp \
+ src/util/time.hpp
libndn_cpp_la_LIBADD = @OPENSSL_LIBS@ @OSX_SECURITY_LIBS@
libndn_cpp_la_LDFLAGS = @OPENSSL_LDFLAGS@ @SQLITE3_LDFLAGS@ @BOOST_LDFLAGS@
diff --git a/src/util/time.hpp b/src/util/time.hpp
new file mode 100644
index 0000000..16bc4db
--- /dev/null
+++ b/src/util/time.hpp
@@ -0,0 +1,50 @@
+/**
+ * Copyright (C) 2013 Regents of the University of California.
+ * @author: Jeff Thompson <jefft0@remap.ucla.edu>
+ * @author: Yingdi Yu <yingdi@cs.ucla.edu>
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef NDN_TIME_HPP
+#define NDN_TIME_HPP
+
+#include <stdexcept>
+#include <ndn-cpp/c/util/time.h>
+
+namespace ndn {
+
+/**
+ * Convert to the ISO string representation of the time.
+ * @param time Milliseconds since 1/1/1970.
+ * @return The ISO string.
+ */
+inline std::string
+toIsoString(const MillisecondsSince1970& time)
+{
+ char isoString[25];
+ ndn_Error error;
+ if ((error = ndn_toIsoString(time, isoString)))
+ throw std::runtime_error(ndn_getErrorString(error));
+
+ return isoString;
+}
+
+/**
+ * Convert from the ISO string representation to the internal time format.
+ * @param isoString The ISO time formatted string.
+ * @return The time in milliseconds since 1/1/1970.
+ */
+inline MillisecondsSince1970
+fromIsoString(const std::string& isoString)
+{
+ MillisecondsSince1970 milliseconds;
+ ndn_Error error;
+ if ((error = ndn_fromIsoString(isoString.c_str(), &milliseconds)))
+ throw std::runtime_error(ndn_getErrorString(error));
+
+ return milliseconds;
+}
+
+} // namespace ndn
+
+#endif // NDN_TIME_HPP