]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
util-time: add function to create a UTC time string
authorMats Klepsland <mats.klepsland@gmail.com>
Mon, 14 Mar 2016 09:31:16 +0000 (10:31 +0100)
committerVictor Julien <victor@inliniac.net>
Sun, 25 Sep 2016 20:35:34 +0000 (22:35 +0200)
Add function CreateUtcIsoTimeString to create a UTC time string.

src/util-time.c
src/util-time.h

index 992650e317d6056086f61596b82577487608c911..6670a0157add47924efc5b562739c9af470662cc 100644 (file)
@@ -64,6 +64,7 @@ static SCSpinlock current_time_spinlock;
 static char live = TRUE;
 
 struct tm *SCLocalTime(time_t timep, struct tm *result);
+struct tm *SCUtcTime(time_t timep, struct tm *result);
 
 void TimeInit(void)
 {
@@ -190,6 +191,26 @@ void CreateIsoTimeString (const struct timeval *ts, char *str, size_t size)
     }
 }
 
+void CreateUtcIsoTimeString (const struct timeval *ts, char *str, size_t size)
+{
+    time_t time = ts->tv_sec;
+    struct tm local_tm;
+    struct tm *t = (struct tm*)SCUtcTime(time, &local_tm);
+    char time_fmt[64] = { 0 };
+
+    if (likely(t != NULL)) {
+        strftime(time_fmt, sizeof(time_fmt), "%Y-%m-%dT%H:%M:%S", t);
+        snprintf(str, size, time_fmt, ts->tv_usec);
+    } else {
+        snprintf(str, size, "ts-error");
+    }
+}
+
+struct tm *SCUtcTime(time_t timep, struct tm *result)
+{
+    return gmtime_r(&timep, result);
+}
+
 /*
  * Time Caching code
  */
index c2cbd5a22bebfe4b8ff4ee337c29165515bffc5f..fa71cf565b4b468945faa6f21fb8f96acd08d27a 100644 (file)
@@ -53,6 +53,7 @@ int TimeModeIsLive(void);
 struct tm *SCLocalTime(time_t timep, struct tm *result);
 void CreateTimeString (const struct timeval *ts, char *str, size_t size);
 void CreateIsoTimeString (const struct timeval *ts, char *str, size_t size);
+void CreateUtcIsoTimeString (const struct timeval *ts, char *str, size_t size);
 time_t SCMkTimeUtc (struct tm *tp);
 int SCStringPatternToTime (char *string, char **patterns,
                            int num_patterns, struct tm *time);