From c96a7dff4ba90249e8f182d23d74225bc3f74572 Mon Sep 17 00:00:00 2001 From: schwarze Date: Wed, 11 Aug 2021 13:41:48 +0000 Subject: [PATCH] If the -f argument includes %s, we need to use timegm(3) rather than mktime(3) because the UNIX Epoch is defined in UTC rather than in the local timezone. Combining %s with other format specifiers is usually not useful. But if a user does that, then parsing the whole input as UTC seems better than parsing some of it as UTC and some of it in the local time zone. Bug found by Bryan Vyhmeister. The final patch is joint work with and OK by gerhard@. No objection when shown on tech@. --- bin/date/date.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/bin/date/date.c b/bin/date/date.c index b197648716c..635c3f006d0 100644 --- a/bin/date/date.c +++ b/bin/date/date.c @@ -1,4 +1,4 @@ -/* $OpenBSD: date.c,v 1.56 2019/08/08 02:17:51 cheloha Exp $ */ +/* $OpenBSD: date.c,v 1.57 2021/08/11 13:41:48 schwarze Exp $ */ /* $NetBSD: date.c,v 1.11 1995/09/07 06:21:05 jtc Exp $ */ /* @@ -219,7 +219,11 @@ setthetime(char *p, const char *pformat) } /* convert broken-down time to UTC clock time */ - if ((tval = mktime(lt)) == -1) + if (pformat != NULL && strstr(pformat, "%s") != NULL) + tval = timegm(lt); + else + tval = mktime(lt); + if (tval == -1) errx(1, "specified date is outside allowed range"); if (jflag) -- 2.20.1