From 10e078374d3c44edde56a1c53578bfed1c9da94c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Thomas=20Wei=C3=9Fschuh?= Date: Sat, 23 Sep 2023 10:24:58 +0200 Subject: [PATCH] lib/timeutils: don't use glibc strptime extension MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit strptime("%s") is a glibc extension and for example not available on musl. Reimplement the functionality in a cross-platform way. Signed-off-by: Thomas Weißschuh --- lib/timeutils.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/timeutils.c b/lib/timeutils.c index b791c3f482..f53ec8fee3 100644 --- a/lib/timeutils.c +++ b/lib/timeutils.c @@ -161,6 +161,23 @@ static int parse_subseconds(const char *t, usec_t *usec) return 0; } +static const char *parse_epoch_seconds(const char *t, struct tm *tm) +{ + int64_t s; + time_t st; + int f, c; + + f = sscanf(t, "%"SCNd64"%n", &s, &c); + if (f < 1) + return NULL; + st = s; + if ((int64_t) st < s) + return NULL; + if (!localtime_r(&st, tm)) + return NULL; + return t + c; +} + static int parse_timestamp_reference(time_t x, const char *t, usec_t *usec) { static const struct { @@ -254,7 +271,7 @@ static int parse_timestamp_reference(time_t x, const char *t, usec_t *usec) goto finish; } else if (t[0] == '@') { - k = strptime(t + 1, "%s", &tm); + k = parse_epoch_seconds(t + 1, &tm); if (k && *k == 0) goto finish; else if (k && parse_subseconds(k, &ret) == 0) -- 2.47.2