So ts(1) with -i or -s prints strange numbers when used with either %T or
%s (the current timezone comes into play and you end up in pain).
This diff cleans up the code, forces UTC for -i or -s and simplifies a lot
of the code.

I think with this things like TZ=Canada/Newfoundland ts -s %.T and
TZ=Canada/Newfoundland ts -s %.s will print values that make sense.

Lets hope this was the last bug in this tool
-- 
:wq Claudio

Index: ts.c
===================================================================
RCS file: /cvs/src/usr.bin/ts/ts.c,v
retrieving revision 1.2
diff -u -p -r1.2 ts.c
--- ts.c        29 Jun 2022 16:01:10 -0000      1.2
+++ ts.c        29 Jun 2022 20:08:59 -0000
@@ -40,8 +40,8 @@ main(int argc, char *argv[])
 {
        int iflag, mflag, sflag;
        int ch, prev;
-       struct timespec rstart, start, now, elapsed;
-       struct tm *lt, tm;
+       struct timespec roff, start, now;
+       struct tm *tm;
        int clock = CLOCK_REALTIME;
 
        if (pledge("stdio", NULL) == -1)
@@ -88,34 +88,27 @@ main(int argc, char *argv[])
        if ((outbuf = calloc(1, bufsize)) == NULL)
                err(1, NULL);
 
-       clock_gettime(CLOCK_REALTIME, &rstart);
-       clock_gettime(CLOCK_MONOTONIC, &start);
+       /* force UTC for interval calculations */
+       if (iflag || sflag)
+               if (setenv("TZ", "UTC", 1) == -1)
+                       err(1, "setenv UTC");
+
+       clock_gettime(CLOCK_REALTIME, &roff);
+       clock_gettime(clock, &start);
+       timespecsub(&roff, &start, &roff);
 
        for (prev = '\n'; (ch = getchar()) != EOF; prev = ch) {
                if (prev == '\n') {
-                       if (clock_gettime(clock, &now))
-                               err(1, "clock_gettime");
-                       if (iflag || sflag) {
-                               timespecsub(&now, &start, &elapsed);
-                               if (gmtime_r(&elapsed.tv_sec, &tm) == NULL)
-                                       err(1, "gmtime_r");
-                               if (iflag)
-                                       if (clock_gettime(clock, &start))
-                                               err(1, "clock_gettime");
-                               fmtfmt(&tm, elapsed.tv_nsec);
-                       } else if (mflag) {
-                               timespecsub(&now, &start, &elapsed);
-                               timespecadd(&rstart, &elapsed, &now);
-                               lt = localtime(&now.tv_sec);
-                               if (lt == NULL)
-                                       err(1, "localtime");
-                               fmtfmt(lt, now.tv_nsec);
-                       } else {
-                               lt = localtime(&now.tv_sec);
-                               if (lt == NULL)
-                                       err(1, "localtime");
-                               fmtfmt(lt, now.tv_nsec);
-                       }
+                       clock_gettime(clock, &now);
+                       if (iflag || sflag)
+                               timespecsub(&now, &start, &now);
+                       else if (mflag)
+                               timespecadd(&now, &roff, &now);
+                       if (iflag)
+                               clock_gettime(clock, &start);
+                       if ((tm = localtime(&now.tv_sec)) == NULL)
+                               err(1, "localtime/gmtime");
+                       fmtfmt(tm, now.tv_nsec);
                }
                if (putchar(ch) == EOF)
                        break;

Reply via email to