summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordefault <nobody@localhost>2023-06-27 16:52:29 +0200
committerdefault <nobody@localhost>2023-06-27 16:52:29 +0200
commitf543185c7156bf659fb12880f8e7708c57c2bbfb (patch)
treede92ec32185c76b153fc84993118c38b5f323e0f
parent1c6cc98f076b03233ab4bd43c32705c71de5e811 (diff)
Backport from xs.
-rw-r--r--xs_time.h46
-rw-r--r--xs_version.h2
2 files changed, 44 insertions, 4 deletions
diff --git a/xs_time.h b/xs_time.h
index 970f255..d07352a 100644
--- a/xs_time.h
+++ b/xs_time.h
@@ -9,6 +9,7 @@
xs_str *xs_str_time(time_t t, const char *fmt, int local);
#define xs_str_localtime(t, fmt) xs_str_time(t, fmt, 1)
#define xs_str_utctime(t, fmt) xs_str_time(t, fmt, 0)
+time_t xs_parse_iso_date(const char *iso_date, int local);
time_t xs_parse_time(const char *str, const char *fmt, int local);
#define xs_parse_localtime(str, fmt) xs_parse_time(str, fmt, 1)
#define xs_parse_utctime(str, fmt) xs_parse_time(str, fmt, 0)
@@ -52,16 +53,55 @@ char *strptime(const char *s, const char *format, struct tm *tm);
time_t xs_parse_time(const char *str, const char *fmt, int local)
{
- struct tm tm;
+ time_t t = 0;
+
+#ifndef WITHOUT_STRPTIME
+
+ struct tm tm = {0};
- memset(&tm, '\0', sizeof(tm));
strptime(str, fmt, &tm);
/* try to guess the Daylight Saving Time */
if (local)
tm.tm_isdst = -1;
- return local ? mktime(&tm) : timegm(&tm);
+ t = local ? mktime(&tm) : timegm(&tm);
+
+#endif /* WITHOUT_STRPTIME */
+
+ return t;
+}
+
+
+time_t xs_parse_iso_date(const char *iso_date, int local)
+/* parses a YYYY-MM-DDTHH:MM:SS date string */
+{
+ time_t t = 0;
+
+#ifndef WITHOUT_STRPTIME
+
+ t = xs_parse_time(iso_date, "%Y-%m-%dT%H:%M:%S", local);
+
+#else /* WITHOUT_STRPTIME */
+
+ struct tm tm = {0};
+
+ if (sscanf(iso_date, "%d-%d-%dT%d:%d:%d",
+ &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
+ &tm.tm_hour, &tm.tm_min, &tm.tm_sec) == 6) {
+
+ tm.tm_year -= 1900;
+ tm.tm_mon -= 1;
+
+ if (local)
+ tm.tm_isdst = -1;
+
+ t = local ? mktime(&tm) : timegm(&tm);
+ }
+
+#endif /* WITHOUT_STRPTIME */
+
+ return t;
}
diff --git a/xs_version.h b/xs_version.h
index 8fd8e70..6d8b8b8 100644
--- a/xs_version.h
+++ b/xs_version.h
@@ -1 +1 @@
-/* bc5b36414b704fe4cd07f2be58133b82330ce435 */
+/* 567d70ecbe16b2358873b8bc971a6e092c3c0074 */