From cb1cbddaca065e3deba508bf867004e20c10e824 Mon Sep 17 00:00:00 2001 From: Will Dietz Date: Wed, 11 Jul 2018 13:08:22 -0500 Subject: [PATCH] strptime: add basic support for '%s' (seconds since epoch) --- src/time/strptime.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/time/strptime.c b/src/time/strptime.c index c54a0d8c..87fa77a8 100644 --- a/src/time/strptime.c +++ b/src/time/strptime.c @@ -5,6 +5,9 @@ #include #include #include +#include "time_impl.h" + +struct tm *__localtime_r(const time_t *restrict, struct tm *restrict); char *strptime(const char *restrict s, const char *restrict f, struct tm *restrict tm) { @@ -119,6 +122,18 @@ char *strptime(const char *restrict s, const char *restrict f, struct tm *restri min = 0; range = 61; goto numeric_range; + case 's': + if (!isdigit(*s)) return 0; + unsigned long long secs = 0, new_secs; + const unsigned long long max_time = (1ULL<<8*sizeof(time_t)-1)-1; + do { + new_secs = secs*10 + (*s-'0'); + if (new_secs < secs || new_secs > max_time) return 0; + secs = new_secs; + } while(isdigit(*++s)); + time_t t = secs; + if (!__localtime_r(&t, tm)) return 0; + break; case 'T': s = strptime(s, "%H:%M:%S", tm); if (!s) return 0; -- 2.18.0