Hi,

Bryan Vyhmeister found a strange behavior in date(1):

        # date -f %s -j 1627519989
        Thu Jul 29 01:53:09 PDT 2021
        # date -u -f %s -j 1627519989
        Thu Jul 29 00:53:09 UTC 2021

Looks like PDT is GMT-1, which of course is wrong.

The problem arises from the -f option. The argument of date(1) is passed
to strptime(3). Normally, this will return a broken down time in the
local timezone. But the '%s' format makes an exception and returns a
date in UTC.

The patch below isn't very beautiful, but fixes the problem:

        # date -f %s -j 1627519989
        Wed Jul 28 17:53:09 PDT 2021


Gerhard


Index: bin/date/date.c
===================================================================
RCS file: /cvs/src/bin/date/date.c,v
retrieving revision 1.56
diff -u -p -r1.56 date.c
--- bin/date/date.c     8 Aug 2019 02:17:51 -0000       1.56
+++ bin/date/date.c     2 Aug 2021 07:56:15 -0000
@@ -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 && strcmp(pformat, "%s") == 0)
+               tval = timegm(lt);
+       else
+               tval = mktime(lt);
+       if (tval == -1)
                errx(1, "specified date is outside allowed range");
        if (jflag)

Reply via email to