Skip to content

Commit 9901d8a

Browse files
committed
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>
1 parent 12f8620 commit 9901d8a

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/bin/psql/command.c

+15-10
Original file line numberDiff line numberDiff line change
@@ -3066,6 +3066,7 @@ do_watch(PQExpBuffer query_buf, double sleep)
30663066
{
30673067
long sleep_ms = (long) (sleep * 1000);
30683068
printQueryOpt myopt = pset.popt;
3069+
const char *strftime_fmt;
30693070
const char *user_title;
30703071
char *title;
30713072
int title_len;
@@ -3077,6 +3078,13 @@ do_watch(PQExpBuffer query_buf, double sleep)
30773078
return false;
30783079
}
30793080

3081+
/*
3082+
* Choose format for timestamps. We might eventually make this a \pset
3083+
* option. In the meantime, using a variable for the format suppresses
3084+
* overly-anal-retentive gcc warnings about %c being Y2K sensitive.
3085+
*/
3086+
strftime_fmt = "%c";
3087+
30803088
/*
30813089
* Set up rendering options, in particular, disable the pager, because
30823090
* nobody wants to be prompted while watching the output of 'watch'.
@@ -3085,16 +3093,17 @@ do_watch(PQExpBuffer query_buf, double sleep)
30853093

30863094
/*
30873095
* If there's a title in the user configuration, make sure we have room
3088-
* for it in the title buffer.
3096+
* for it in the title buffer. Allow 128 bytes for the timestamp plus 128
3097+
* bytes for the rest.
30893098
*/
30903099
user_title = myopt.title;
3091-
title_len = (user_title ? strlen(user_title) : 0) + 100;
3100+
title_len = (user_title ? strlen(user_title) : 0) + 256;
30923101
title = pg_malloc(title_len);
30933102

30943103
for (;;)
30953104
{
30963105
time_t timer;
3097-
char asctimebuf[64];
3106+
char timebuf[128];
30983107
long i;
30993108

31003109
/*
@@ -3103,18 +3112,14 @@ do_watch(PQExpBuffer query_buf, double sleep)
31033112
* makes for reasonably nicely formatted output in simple cases.
31043113
*/
31053114
timer = time(NULL);
3106-
strlcpy(asctimebuf, asctime(localtime(&timer)), sizeof(asctimebuf));
3107-
/* strip trailing newline from asctime's output */
3108-
i = strlen(asctimebuf);
3109-
while (i > 0 && asctimebuf[--i] == '\n')
3110-
asctimebuf[i] = '\0';
3115+
strftime(timebuf, sizeof(timebuf), strftime_fmt, localtime(&timer));
31113116

31123117
if (user_title)
31133118
snprintf(title, title_len, _("%s\t%s (every %gs)\n"),
3114-
user_title, asctimebuf, sleep);
3119+
user_title, timebuf, sleep);
31153120
else
31163121
snprintf(title, title_len, _("%s (every %gs)\n"),
3117-
asctimebuf, sleep);
3122+
timebuf, sleep);
31183123
myopt.title = title;
31193124

31203125
/* Run the query and print out the results */

0 commit comments

Comments
 (0)