Use strftime("%c") to format timestamps in psql's \watch command.

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.GA22965@postgresql.kr>
This commit is contained in:
Tom Lane 2016-06-15 19:31:08 -04:00
parent 12f862099d
commit 9901d8ac2e
1 changed files with 15 additions and 10 deletions

View 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 */