time support: check and define NDN_CPP_HAVE_GMTIME_SUPPORT. In ndn_toIsoString and ndn_fromIsoString, return an error if not supported.
diff --git a/ndn-cpp/c/errors.c b/ndn-cpp/c/errors.c
index 55ae58d..a77a858 100644
--- a/ndn-cpp/c/errors.c
+++ b/ndn-cpp/c/errors.c
@@ -75,6 +75,8 @@
return "SocketTransport error in close";
case NDN_ERROR_Name_component_does_not_begin_with_the_expected_marker:
return "Name component does not begin with the expected marker";
+ case NDN_ERROR_Time_functions_are_not_supported_by_the_standard_library:
+ return "Time functions are not supported by the standard library";
default:
return "unrecognized ndn_Error code";
}
diff --git a/ndn-cpp/c/errors.h b/ndn-cpp/c/errors.h
index f975c26..396f43d 100644
--- a/ndn-cpp/c/errors.h
+++ b/ndn-cpp/c/errors.h
@@ -45,7 +45,8 @@
NDN_ERROR_SocketTransport_error_in_poll,
NDN_ERROR_SocketTransport_error_in_recv,
NDN_ERROR_SocketTransport_error_in_close,
- NDN_ERROR_Name_component_does_not_begin_with_the_expected_marker
+ NDN_ERROR_Name_component_does_not_begin_with_the_expected_marker,
+ NDN_ERROR_Time_functions_are_not_supported_by_the_standard_library
} ndn_Error;
/**
diff --git a/ndn-cpp/c/util/time.c b/ndn-cpp/c/util/time.c
index 7ffa517..b7de490 100644
--- a/ndn-cpp/c/util/time.c
+++ b/ndn-cpp/c/util/time.c
@@ -14,14 +14,16 @@
ndn_getNowMilliseconds()
{
struct timeval t;
+ // Note: configure.ac requires gettimeofday.
gettimeofday(&t, 0);
return t.tv_sec * 1000.0 + t.tv_usec / 1000.0;
}
-void
-ndn_toIsoString(ndn_MillisecondsSince1970 time, char *isoString)
+ndn_Error
+ndn_toIsoString(ndn_MillisecondsSince1970 milliseconds, char *isoString)
{
- double secondsSince1970 = time / 1000.0;
+#if NDN_CPP_HAVE_GMTIME_SUPPORT
+ double secondsSince1970 = milliseconds / 1000.0;
char fractionBuffer[10];
sprintf(fractionBuffer, "%.06lf", fmod(secondsSince1970, 1.0));
const char *fraction = strchr(fractionBuffer, '.');
@@ -33,24 +35,35 @@
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 NDN_ERROR_success;
+#else
+ return NDN_ERROR_Time_functions_are_not_supported_by_the_standard_library;
+#endif
}
-ndn_MillisecondsSince1970
-ndn_fromIsoString(const char* isoString)
+ndn_Error
+ndn_fromIsoString(const char* isoString, ndn_MillisecondsSince1970 *milliseconds)
{
+#if NDN_CPP_HAVE_GMTIME_SUPPORT
// Initialize time zone, etc.
time_t dummyTime = 0;
- struct tm time = *gmtime(&dummyTime);
+ struct tm tm1 = *gmtime(&dummyTime);
- sscanf(isoString, "%4d%2d%2dT%2d%2d", &time.tm_year, &time.tm_mon, &time.tm_mday, &time.tm_hour, &time.tm_min);
+ 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.
- time.tm_year -= 1900;
+ tm1.tm_year -= 1900;
// tm_mon starts from 0, not 1.
- time.tm_mon -= 1;
- time.tm_sec = 0;
- return (timegm(&time) + seconds) * 1000.0;
-}
\ No newline at end of file
+ tm1.tm_mon -= 1;
+ tm1.tm_sec = 0;
+
+ *milliseconds = (timegm(&tm1) + seconds) * 1000.0;
+ return NDN_ERROR_success;
+#else
+ return NDN_ERROR_Time_functions_are_not_supported_by_the_standard_library;
+#endif
+}
diff --git a/ndn-cpp/c/util/time.h b/ndn-cpp/c/util/time.h
index b2414c7..75f95df 100644
--- a/ndn-cpp/c/util/time.h
+++ b/ndn-cpp/c/util/time.h
@@ -8,6 +8,7 @@
#define NDN_TIME_H
#include <ndn-cpp/c/common.h>
+#include "../errors.h"
#ifdef __cplusplus
extern "C" {
@@ -24,17 +25,19 @@
* Convert the time from milliseconds to an ISO time string, 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.
+ * @return 0 for success, else an error code including if we don't have necessary standard library support.
*/
-void
-ndn_toIsoString(ndn_MillisecondsSince1970 time, char *isoString);
+ndn_Error
+ndn_toIsoString(ndn_MillisecondsSince1970 milliseconds, char *isoString);
/**
* Parse the ISO time string and return the time in milliseconds.
* @param isoString The ISO time string, for example "20131018T184138.423355".
- * @return The time in milliseconds since 1/1/1970, including fractions of a millisecond.
+ * @param milliseconds Return the time in milliseconds since 1/1/1970, including fractions of a millisecond.
+ * @return 0 for success, else an error code including if we don't have necessary standard library support.
*/
-ndn_MillisecondsSince1970
-ndn_fromIsoString(const char* isoString);
+ndn_Error
+ndn_fromIsoString(const char* isoString, ndn_MillisecondsSince1970 *milliseconds);
#ifdef __cplusplus
}