build: Finalizing waf building system (removing legacy code)
Change-Id: Ie7e7cc84e19551e3dd5dd0405b14f289ea54e7cd
diff --git a/src/util/time.cpp b/src/util/time.cpp
new file mode 100644
index 0000000..03eb81b
--- /dev/null
+++ b/src/util/time.cpp
@@ -0,0 +1,85 @@
+/**
+ * Copyright (C) 2013 Regents of the University of California.
+ * @author: Jeff Thompson <jefft0@remap.ucla.edu>
+ * See COPYING for copyright and distribution information.
+ */
+
+#include <ndn-cpp-dev/ndn-cpp-config.h>
+#if NDN_CPP_HAVE_TIME_H
+#include <time.h>
+#endif
+#if NDN_CPP_HAVE_SYS_TIME_H
+#include <sys/time.h>
+#endif
+#include <math.h>
+#include <string.h>
+#include <stdio.h>
+#include "time.hpp"
+
+namespace ndn {
+
+MillisecondsSince1970
+ndn_getNowMilliseconds()
+{
+ struct timeval t;
+ // Note: configure.ac requires gettimeofday.
+ gettimeofday(&t, 0);
+ return t.tv_sec * 1000.0 + t.tv_usec / 1000.0;
+}
+
+int
+ndn_toIsoString(MillisecondsSince1970 milliseconds, char *isoString)
+{
+#if NDN_CPP_HAVE_GMTIME_SUPPORT
+ if (milliseconds < 0)
+ return -1;
+ else if (milliseconds > 2e14)
+ // 2e14 is about the year 8300. We don't want to go over a 4-digit year.
+ return -1;
+
+ double secondsSince1970 = milliseconds / 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 = (time_t)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);
+
+ return 0;
+#else
+ return -1;
+#endif
+}
+
+int
+ndn_fromIsoString(const char* isoString, MillisecondsSince1970 *milliseconds)
+{
+#if NDN_CPP_HAVE_GMTIME_SUPPORT
+ // Initialize time zone, etc.
+ time_t dummyTime = 0;
+ struct tm tm1 = *gmtime(&dummyTime);
+
+ sscanf(isoString, "%4d%2d%2dT%2d%2d", &tm1.tm_year, &tm1.tm_mon, &tm1.tm_mday, &tm1.tm_hour, &tm1.tm_min);
+ // Skip the time past minutes and get the float seconds.
+ double seconds;
+ sscanf(isoString + (4 + 2 + 2 + 1 + 2 + 2), "%lf", &seconds);
+
+ // tm_year starts from 1900.
+ tm1.tm_year -= 1900;
+ // tm_mon starts from 0, not 1.
+ tm1.tm_mon -= 1;
+ tm1.tm_sec = 0;
+
+ *milliseconds = (timegm(&tm1) + seconds) * 1000.0;
+ return 0;
+#else
+ return -1;
+#endif
+}
+
+} // namespace ndn