From: Thomas Weißschuh Date: Sat, 23 Sep 2023 08:24:58 +0000 (+0200) Subject: lib/timeutils: don't use glibc strptime extension X-Git-Tag: v2.40-rc1~217^2~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=10e078374d3c44edde56a1c53578bfed1c9da94c;p=thirdparty%2Futil-linux.git lib/timeutils: don't use glibc strptime extension 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 --- 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)