]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/timeutils: don't use glibc strptime extension
authorThomas Weißschuh <thomas@t-8ch.de>
Sat, 23 Sep 2023 08:24:58 +0000 (10:24 +0200)
committerThomas Weißschuh <thomas@t-8ch.de>
Wed, 27 Sep 2023 17:33:30 +0000 (19:33 +0200)
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 <thomas@t-8ch.de>
lib/timeutils.c

index b791c3f4826cc417d3c3c3d2ab59d7b93548d206..f53ec8fee32ef4b7f0e92f273c079ded7317f511 100644 (file)
@@ -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)