tech-toolchain archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

make: time stamps



In my build at work, we like to have timestamps at various places,
to help track where all the time is going.
This is currently done by running date(1), but it would be more
efficient to do it from make itself.
The patch below adds :tL (localtime) and :tU (UTC) as modifiers to cause
us to treat the current value as a fmt string for strftime.

So:

$ bmake -r -V '${The time is %c:L:tU}' -V '${Which is %Y-%m-%d %T %Z:L:tL}'
The time is Fri Apr  8 23:31:01 2011
Which is 2011-04-08 16:31:01 PDT
or
TIME_FMT='@ %s [%Y-%m-%d %T]'
bmake -V '${TIME_FMT:tL}'                               <
@ 1302305562 [2011-04-08 16:32:42]



Index: var.c
===================================================================
RCS file: /cvs/juniper/src/buildtools/bmake/var.c,v
retrieving revision 1.37
diff -u -p -r1.37 var.c
--- var.c       7 Mar 2011 17:22:18 -0000       1.37
+++ var.c       8 Apr 2011 23:21:53 -0000
@@ -2291,6 +2291,21 @@ VarChangeCase(char *str, int upper)
    return Buf_Destroy(&buf, FALSE);
 }
 
+static char *
+VarStrftime(const char *fmt, int zulu)
+{
+    char buf[BUFSIZ];
+    time_t now;
+
+    time(&now);
+    if (!*fmt)
+       fmt = "%c";
+    strftime(buf, sizeof(buf), fmt, zulu ? gmtime(&now) : localtime(&now));
+    
+    buf[sizeof(buf) - 1] = '\0';
+    return bmake_strdup(buf);
+}
+
 /*
  * Now we need to apply any modifiers the user wants applied.
  * These are:
@@ -2902,6 +2917,10 @@ ApplyModifiers(char *nstr, const char *t
                                               VarRealpath, NULL);
                            cp = tstr + 2;
                            termc = *cp;
+                       } else if (tstr[1] == 'L' || tstr[1] == 'U') {
+                           newStr = VarStrftime(nstr, (tstr[1] == 'U'));
+                           cp = tstr + 2;
+                           termc = *cp;
                        } else if (tstr[1] == 'u' || tstr[1] == 'l') {
                            newStr = VarChangeCase(nstr, (tstr[1] == 'u'));
                            cp = tstr + 2;


Home | Main Index | Thread Index | Old Index