time FEATURE datetime print to provided buffer
diff --git a/src/netconf.h b/src/netconf.h
index 4b13cd1..4705aac 100644
--- a/src/netconf.h
+++ b/src/netconf.h
@@ -129,13 +129,14 @@
*
* This is a reverse function to nc_datetime2time().
*
- * @param[in] time time_t type value returned e.g. by time().
- * @param[in] tz timezone name for the result. See tzselect(1) for list of
+ * @param[in] time Time to convert.
+ * @param[in] tz Timezone name for the result. See tzselect(1) for list of
* correct values. If not specified (NULL), the result is provided in UTC (Zulu).
- * @return Printed string in a format compliant to RFC 3339. It is up to the
- * caller to free the returned string.
+ * @param[in] buf Optional buffer to print the datetime into, should be at least 26 characters long!
+ * @return Printed string in a format compliant to RFC 3339 stored in \p buf if provided,
+ * otherwise it is up to the caller to free the returned string.
*/
-char* nc_time2datetime(time_t time, const char* tz);
+char* nc_time2datetime(time_t time, const char* tz, char *buf);
/**
* @brief Transform given string in RFC 3339 compliant format to the time_t
diff --git a/src/time.c b/src/time.c
index b2f0005..4e92d78 100644
--- a/src/time.c
+++ b/src/time.c
@@ -90,7 +90,7 @@
}
API char *
-nc_time2datetime(time_t time, const char *tz)
+nc_time2datetime(time_t time, const char *tz, char *buf)
{
char *date = NULL;
char *zoneshift = NULL;
@@ -144,19 +144,29 @@
}
}
}
- if (asprintf(&date, "%04d-%02d-%02dT%02d:%02d:%02d%s",
- tm.tm_year + 1900,
- tm.tm_mon + 1,
- tm.tm_mday,
- tm.tm_hour,
- tm.tm_min,
- tm.tm_sec,
- (zoneshift == NULL) ? "" : zoneshift) == -1) {
+
+ if (buf) {
+ sprintf(buf, "%04d-%02d-%02dT%02d:%02d:%02d%s",
+ tm.tm_year + 1900,
+ tm.tm_mon + 1,
+ tm.tm_mday,
+ tm.tm_hour,
+ tm.tm_min,
+ tm.tm_sec,
+ (zoneshift == NULL) ? "" : zoneshift);
+ } else if (asprintf(&date, "%04d-%02d-%02dT%02d:%02d:%02d%s",
+ tm.tm_year + 1900,
+ tm.tm_mon + 1,
+ tm.tm_mday,
+ tm.tm_hour,
+ tm.tm_min,
+ tm.tm_sec,
+ (zoneshift == NULL) ? "" : zoneshift) == -1) {
free(zoneshift);
ERRMEM;
return NULL;
}
free(zoneshift);
- return date;
+ return (buf ? buf : date);
}