diff mbox series

[v1,1/8] util: add optional swupdate_time_iso8601 param

Message ID 20211015082457.6804-2-roland.gaudig-oss@weidmueller.com
State Changes Requested
Headers show
Series suricatta: ipc: add request to get hawkBit server status | expand

Commit Message

Roland Gaudig Oct. 15, 2021, 8:24 a.m. UTC
From: Roland Gaudig <roland.gaudig@weidmueller.com>

This adds an optional parameter to the swupdate_time_iso8601 function,
which allows to pass an older time stamp for conversion to this
function.

    char *swupdate_time_iso8601(struct timeval *tv);

The tv argument is a struct timeval (as specified in <sys/time.h>):

    struct timeval {
         time_t      tv_sec;     /* seconds since the Epoch */
         suseconds_t tv_usec;    /* microseconds */
    };

If the tv argument is NULL pointer, swupdate_time_iso8601 will obtain
the current time itself by calling gettimeofday. (Like it did before
this modification.)

Signed-off-by: Roland Gaudig <roland.gaudig@weidmueller.com>
---

 core/util.c                | 9 +++++++--
 include/util.h             | 2 +-
 suricatta/server_general.c | 2 +-
 3 files changed, 9 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/core/util.c b/core/util.c
index 7e96652..da6bd10 100644
--- a/core/util.c
+++ b/core/util.c
@@ -817,7 +817,7 @@  int swupdate_umount(const char *dir)
  * Date time in SWUpdate
  * @return : date in ISO8601 (it must be freed by caller)
  */
-char *swupdate_time_iso8601(void)
+char *swupdate_time_iso8601(struct timeval *tv)
 {
 	#define DATE_SIZE_ISO8601	128
 	struct timeval now;
@@ -830,7 +830,12 @@  char *swupdate_time_iso8601(void)
 	if (!buf)
 		return NULL;
 
-	gettimeofday(&now, NULL);
+	if (tv == NULL)
+		gettimeofday(&now, NULL);
+	else {
+		now.tv_sec = tv->tv_sec;
+		now.tv_usec = tv->tv_usec;
+	}
 	ms = now.tv_usec / 1000;
 
 	(void)strftime(buf, DATE_SIZE_ISO8601, "%Y-%m-%dT%T.***%z", localtime(&now.tv_sec));
diff --git a/include/util.h b/include/util.h
index 31f67b1..9c81e62 100644
--- a/include/util.h
+++ b/include/util.h
@@ -238,5 +238,5 @@  int swupdate_mount(const char *device, const char *dir, const char *fstype);
 int swupdate_umount(const char *dir);
 
 /* Date / Time utilities */
-char *swupdate_time_iso8601(void);
+char *swupdate_time_iso8601(struct timeval *tv);
 #endif
diff --git a/suricatta/server_general.c b/suricatta/server_general.c
index e8c3186..d19a3e6 100644
--- a/suricatta/server_general.c
+++ b/suricatta/server_general.c
@@ -197,7 +197,7 @@  static char *server_format_log(const char *event, struct dict *fmtevents,
 	fmt = strdup(tmp);
 	token = strtok_r(fmt, ",", &saveptr);
 
-	fdate = swupdate_time_iso8601();
+	fdate = swupdate_time_iso8601(NULL);
 
 	while (token) {
 		char *field;