commit f543185

default  ·  2023-06-27 14:52:29 +0000 UTC
parent 1c6cc98
Backport from xs.
2 files changed,  +44, -4
+43, -3
 1@@ -9,6 +9,7 @@
 2 xs_str *xs_str_time(time_t t, const char *fmt, int local);
 3 #define xs_str_localtime(t, fmt) xs_str_time(t, fmt, 1)
 4 #define xs_str_utctime(t, fmt)   xs_str_time(t, fmt, 0)
 5+time_t xs_parse_iso_date(const char *iso_date, int local);
 6 time_t xs_parse_time(const char *str, const char *fmt, int local);
 7 #define xs_parse_localtime(str, fmt) xs_parse_time(str, fmt, 1)
 8 #define xs_parse_utctime(str, fmt) xs_parse_time(str, fmt, 0)
 9@@ -52,16 +53,55 @@ char *strptime(const char *s, const char *format, struct tm *tm);
10 
11 time_t xs_parse_time(const char *str, const char *fmt, int local)
12 {
13-    struct tm tm;
14+    time_t t = 0;
15+
16+#ifndef WITHOUT_STRPTIME
17+
18+    struct tm tm = {0};
19 
20-    memset(&tm, '\0', sizeof(tm));
21     strptime(str, fmt, &tm);
22 
23     /* try to guess the Daylight Saving Time */
24     if (local)
25         tm.tm_isdst = -1;
26 
27-    return local ? mktime(&tm) : timegm(&tm);
28+    t = local ? mktime(&tm) : timegm(&tm);
29+
30+#endif /* WITHOUT_STRPTIME */
31+
32+    return t;
33+}
34+
35+
36+time_t xs_parse_iso_date(const char *iso_date, int local)
37+/* parses a YYYY-MM-DDTHH:MM:SS date string */
38+{
39+    time_t t = 0;
40+
41+#ifndef WITHOUT_STRPTIME
42+
43+    t = xs_parse_time(iso_date, "%Y-%m-%dT%H:%M:%S", local);
44+
45+#else /* WITHOUT_STRPTIME */
46+
47+    struct tm tm = {0};
48+
49+    if (sscanf(iso_date, "%d-%d-%dT%d:%d:%d",
50+        &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
51+        &tm.tm_hour, &tm.tm_min, &tm.tm_sec) == 6) {
52+
53+        tm.tm_year -= 1900;
54+        tm.tm_mon -= 1;
55+
56+        if (local)
57+            tm.tm_isdst = -1;
58+
59+        t = local ? mktime(&tm) : timegm(&tm);
60+    }
61+
62+#endif /* WITHOUT_STRPTIME */
63+
64+    return t;
65 }
66 
67 
+1, -1
1@@ -1 +1 @@
2-/* bc5b36414b704fe4cd07f2be58133b82330ce435 */
3+/* 567d70ecbe16b2358873b8bc971a6e092c3c0074 */