diff options
| author | 2023-06-27 16:52:29 +0200 | |
|---|---|---|
| committer | 2023-06-27 16:52:29 +0200 | |
| commit | f543185c7156bf659fb12880f8e7708c57c2bbfb (patch) | |
| tree | de92ec32185c76b153fc84993118c38b5f323e0f | |
| parent | Also break the og:description in the first period in the bio. (diff) | |
| download | snac2-f543185c7156bf659fb12880f8e7708c57c2bbfb.tar.gz snac2-f543185c7156bf659fb12880f8e7708c57c2bbfb.tar.xz snac2-f543185c7156bf659fb12880f8e7708c57c2bbfb.zip | |
Backport from xs.
| -rw-r--r-- | xs_time.h | 46 | ||||
| -rw-r--r-- | xs_version.h | 2 |
2 files changed, 44 insertions, 4 deletions
| @@ -9,6 +9,7 @@ | |||
| 9 | xs_str *xs_str_time(time_t t, const char *fmt, int local); | 9 | xs_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) |
| 12 | time_t xs_parse_iso_date(const char *iso_date, int local); | ||
| 12 | time_t xs_parse_time(const char *str, const char *fmt, int local); | 13 | time_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 | ||
| 53 | time_t xs_parse_time(const char *str, const char *fmt, int local) | 54 | time_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 | |||
| 76 | time_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 | ||
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 */ | ||