Use strftime("%c") to format timestamps in psql's \watch command.
authorTom Lane <[email protected]>
Wed, 15 Jun 2016 23:31:08 +0000 (19:31 -0400)
committerTom Lane <[email protected]>
Wed, 15 Jun 2016 23:31:13 +0000 (19:31 -0400)
This allows the timestamps to follow local conventions (in particular,
they respond to the LC_TIME environment setting).  In C locale you get
the same results as before.  It seems like a good idea to do this now not
later because we already changed the format of \watch headers for 9.6.

Also, increase the buffer sizes a tad to ensure there's enough space for
translated strings.

Discussion: <20160612145532[email protected]>

src/bin/psql/command.c

index 543fe5f5569a1895b0bb01615116332b49094471..3f2cebf3c6a9bfc19249504bda74b6e2d0c9eed5 100644 (file)
@@ -3066,6 +3066,7 @@ do_watch(PQExpBuffer query_buf, double sleep)
 {
    long        sleep_ms = (long) (sleep * 1000);
    printQueryOpt myopt = pset.popt;
+   const char *strftime_fmt;
    const char *user_title;
    char       *title;
    int         title_len;
@@ -3077,6 +3078,13 @@ do_watch(PQExpBuffer query_buf, double sleep)
        return false;
    }
 
+   /*
+    * Choose format for timestamps.  We might eventually make this a \pset
+    * option.  In the meantime, using a variable for the format suppresses
+    * overly-anal-retentive gcc warnings about %c being Y2K sensitive.
+    */
+   strftime_fmt = "%c";
+
    /*
     * Set up rendering options, in particular, disable the pager, because
     * nobody wants to be prompted while watching the output of 'watch'.
@@ -3085,16 +3093,17 @@ do_watch(PQExpBuffer query_buf, double sleep)
 
    /*
     * If there's a title in the user configuration, make sure we have room
-    * for it in the title buffer.
+    * for it in the title buffer.  Allow 128 bytes for the timestamp plus 128
+    * bytes for the rest.
     */
    user_title = myopt.title;
-   title_len = (user_title ? strlen(user_title) : 0) + 100;
+   title_len = (user_title ? strlen(user_title) : 0) + 256;
    title = pg_malloc(title_len);
 
    for (;;)
    {
        time_t      timer;
-       char        asctimebuf[64];
+       char        timebuf[128];
        long        i;
 
        /*
@@ -3103,18 +3112,14 @@ do_watch(PQExpBuffer query_buf, double sleep)
         * makes for reasonably nicely formatted output in simple cases.
         */
        timer = time(NULL);
-       strlcpy(asctimebuf, asctime(localtime(&timer)), sizeof(asctimebuf));
-       /* strip trailing newline from asctime's output */
-       i = strlen(asctimebuf);
-       while (i > 0 && asctimebuf[--i] == '\n')
-           asctimebuf[i] = '\0';
+       strftime(timebuf, sizeof(timebuf), strftime_fmt, localtime(&timer));
 
        if (user_title)
            snprintf(title, title_len, _("%s\t%s (every %gs)\n"),
-                    user_title, asctimebuf, sleep);
+                    user_title, timebuf, sleep);
        else
            snprintf(title, title_len, _("%s (every %gs)\n"),
-                    asctimebuf, sleep);
+                    timebuf, sleep);
        myopt.title = title;
 
        /* Run the query and print out the results */