aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2020-08-27 07:10:33 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2020-09-29 16:13:03 -0700
commit8f898477cbaf3a1c8972ef4ad67321e27d543f5c (patch)
treeed864949710e97188372b8a91f6432f67ee164a9 /core
parentf9721fce4b12faee87398d23fd2f8b4d1b9a0309 (diff)
downloadsubsurface-8f898477cbaf3a1c8972ef4ad67321e27d543f5c.tar.gz
core: add helper functions to format / parse timestamps
To save datetime-based filter constraints to git or XML, it is preferrable to use human-readable representations. Therefore, add helper functions to format / parse timestamp_t 64-bit values in the "YYYY-MM-DD hh:mm:ss" format. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'core')
-rw-r--r--core/subsurface-time.h4
-rw-r--r--core/time.c50
2 files changed, 54 insertions, 0 deletions
diff --git a/core/subsurface-time.h b/core/subsurface-time.h
index ce2821d07..8d564753f 100644
--- a/core/subsurface-time.h
+++ b/core/subsurface-time.h
@@ -14,6 +14,10 @@ extern void utc_mkdate(timestamp_t, struct tm *tm);
extern int utc_year(timestamp_t timestamp);
extern int utc_weekday(timestamp_t timestamp);
+/* parse and format date times of the form YYYY-MM-DD hh:mm:ss */
+extern timestamp_t parse_datetime(const char *s); /* returns 0 on error */
+extern char *format_datetime(timestamp_t timestamp); /* ownership of string passed to caller */
+
#ifdef __cplusplus
}
#endif
diff --git a/core/time.c b/core/time.c
index 94149342a..d5196e868 100644
--- a/core/time.c
+++ b/core/time.c
@@ -1,6 +1,9 @@
// SPDX-License-Identifier: GPL-2.0
#include "subsurface-time.h"
+#include "subsurface-string.h"
#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
/*
* The date handling internally works in seconds since
@@ -163,3 +166,50 @@ int utc_weekday(timestamp_t timestamp)
utc_mkdate(timestamp, &tm);
return tm.tm_wday;
}
+
+/*
+ * Try to parse datetime of the form "YYYY-MM-DD hh:mm:ss" or as
+ * an 64-bit decimal and return 64-bit timestamp. On failure or
+ * if passed an empty string, return 0.
+ */
+extern timestamp_t parse_datetime(const char *s)
+{
+ int y, m, d;
+ int hr, min, sec;
+ struct tm tm;
+
+ if (empty_string(s))
+ return 0;
+ if (sscanf(s, "%d-%d-%d %d:%d:%d", &y, &m, &d, &hr, &min, &sec) != 6) {
+ char *endptr;
+ timestamp_t res = strtoull(s, &endptr, 10);
+ return *endptr == '\0' ? res : 0;
+ }
+
+ tm.tm_year = y;
+ tm.tm_mon = m - 1;
+ tm.tm_mday = d;
+ tm.tm_hour = hr;
+ tm.tm_min = min;
+ tm.tm_sec = sec;
+ return utc_mktime(&tm);
+}
+
+/*
+ * Format 64-bit timestamp in the form "YYYY-MM-DD hh:mm:ss".
+ * Returns the empty string for timestamp = 0
+ */
+extern char *format_datetime(timestamp_t timestamp)
+{
+ char buf[32];
+ struct tm tm;
+
+ if (!timestamp)
+ return strdup("");
+
+ utc_mkdate(timestamp, &tm);
+ snprintf(buf, sizeof(buf), "%04u-%02u-%02u %02u:%02u:%02u",
+ tm.tm_year, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
+
+ return strdup(buf);
+}