summaryrefslogtreecommitdiff
path: root/xs_time.h
diff options
context:
space:
mode:
Diffstat (limited to 'xs_time.h')
-rw-r--r--xs_time.h46
1 files changed, 43 insertions, 3 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 @@
9xs_str *xs_str_time(time_t t, const char *fmt, int local); 9xs_str *xs_str_time(time_t t, const char *fmt, int local);
10#define xs_str_localtime(t, fmt) xs_str_time(t, fmt, 1) 10#define xs_str_localtime(t, fmt) xs_str_time(t, fmt, 1)
11#define xs_str_utctime(t, fmt) xs_str_time(t, fmt, 0) 11#define xs_str_utctime(t, fmt) xs_str_time(t, fmt, 0)
12time_t xs_parse_iso_date(const char *iso_date, int local);
12time_t xs_parse_time(const char *str, const char *fmt, int local); 13time_t xs_parse_time(const char *str, const char *fmt, int local);
13#define xs_parse_localtime(str, fmt) xs_parse_time(str, fmt, 1) 14#define xs_parse_localtime(str, fmt) xs_parse_time(str, fmt, 1)
14#define xs_parse_utctime(str, fmt) xs_parse_time(str, fmt, 0) 15#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);
52 53
53time_t xs_parse_time(const char *str, const char *fmt, int local) 54time_t xs_parse_time(const char *str, const char *fmt, int local)
54{ 55{
55 struct tm tm; 56 time_t t = 0;
57
58#ifndef WITHOUT_STRPTIME
59
60 struct tm tm = {0};
56 61
57 memset(&tm, '\0', sizeof(tm));
58 strptime(str, fmt, &tm); 62 strptime(str, fmt, &tm);
59 63
60 /* try to guess the Daylight Saving Time */ 64 /* try to guess the Daylight Saving Time */
61 if (local) 65 if (local)
62 tm.tm_isdst = -1; 66 tm.tm_isdst = -1;
63 67
64 return local ? mktime(&tm) : timegm(&tm); 68 t = local ? mktime(&tm) : timegm(&tm);
69
70#endif /* WITHOUT_STRPTIME */
71
72 return t;
73}
74
75
76time_t xs_parse_iso_date(const char *iso_date, int local)
77/* parses a YYYY-MM-DDTHH:MM:SS date string */
78{
79 time_t t = 0;
80
81#ifndef WITHOUT_STRPTIME
82
83 t = xs_parse_time(iso_date, "%Y-%m-%dT%H:%M:%S", local);
84
85#else /* WITHOUT_STRPTIME */
86
87 struct tm tm = {0};
88
89 if (sscanf(iso_date, "%d-%d-%dT%d:%d:%d",
90 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
91 &tm.tm_hour, &tm.tm_min, &tm.tm_sec) == 6) {
92
93 tm.tm_year -= 1900;
94 tm.tm_mon -= 1;
95
96 if (local)
97 tm.tm_isdst = -1;
98
99 t = local ? mktime(&tm) : timegm(&tm);
100 }
101
102#endif /* WITHOUT_STRPTIME */
103
104 return t;
65} 105}
66 106
67 107