DER ecncoding: implement DerGtime::toIsoString.
diff --git a/ndn-cpp/c/util/time.c b/ndn-cpp/c/util/time.c
index 9da0e1c..41047f8 100644
--- a/ndn-cpp/c/util/time.c
+++ b/ndn-cpp/c/util/time.c
@@ -5,6 +5,9 @@
*/
#include <sys/time.h>
+#include <math.h>
+#include <string.h>
+#include <stdio.h>
#include "time.h"
ndn_MillisecondsSince1970
@@ -14,3 +17,20 @@
gettimeofday(&t, 0);
return t.tv_sec * 1000.0 + t.tv_usec / 1000.0;
}
+
+void
+ndn_toIsoString(ndn_MillisecondsSince1970 time, char *isoString)
+{
+ double secondsSince1970 = time / 1000.0;
+ char fractionBuffer[10];
+ sprintf(fractionBuffer, "%.06lf", fmod(secondsSince1970, 1.0));
+ const char *fraction = strchr(fractionBuffer, '.');
+ if (!fraction)
+ // Don't expect this to happen.
+ fraction = ".000000";
+
+ time_t seconds = secondsSince1970;
+ struct tm* gmt = gmtime(&seconds);
+ sprintf(isoString, "%04d%02d%02dT%02d%02d%02d%s", 1900 + gmt->tm_year, gmt->tm_mon + 1, gmt->tm_mday,
+ gmt->tm_hour, gmt->tm_min, gmt->tm_sec, fraction);
+}
diff --git a/ndn-cpp/c/util/time.h b/ndn-cpp/c/util/time.h
index adcb0b4..454216e 100644
--- a/ndn-cpp/c/util/time.h
+++ b/ndn-cpp/c/util/time.h
@@ -20,6 +20,14 @@
ndn_MillisecondsSince1970
ndn_getNowMilliseconds();
+/**
+ * Convert the time from milliseconds to ISO time, for example "20131018T184138.423355".
+ * @param milliseconds The time in milliseconds since 1/1/1970, including fractions of a millisecond.
+ * @param isoString A buffer of at least 23 bytes to receive the null-terminated ISO time string.
+ */
+void
+ndn_toIsoString(ndn_MillisecondsSince1970 time, char *isoString);
+
#ifdef __cplusplus
}
#endif
diff --git a/ndn-cpp/encoding/der/der.cpp b/ndn-cpp/encoding/der/der.cpp
index e569819..125b9a4 100644
--- a/ndn-cpp/encoding/der/der.cpp
+++ b/ndn-cpp/encoding/der/der.cpp
@@ -11,6 +11,7 @@
#endif
#include "der-exception.hpp"
#include "../../util/logging.hpp"
+#include "../../c/util/time.h"
#include "der.hpp"
INIT_LOGGER("ndn.der.DER");
@@ -159,7 +160,7 @@
case DER_GENERALIZED_TIME:
return shared_ptr<DerGtime>(new DerGtime(start));
default:
- throw DerDecodingException("Unimplemented DER types");
+ throw DerDecodingException("Unimplemented DER type");
}
}
@@ -596,9 +597,9 @@
string DerGtime::toIsoString(const MillisecondsSince1970& time)
{
-#if 1
- throw std::runtime_error("not implemented");
-#endif
+ char isoString[25];
+ ndn_toIsoString(time, isoString);
+ return isoString;
}
MillisecondsSince1970 DerGtime::fromIsoString(const string& isoString)