From 1418e6e07b69766ea1e231917601f18fcf0c2624 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Tue, 26 Feb 2013 15:50:22 -0500 Subject: [PATCH] Clean up "stopgap" implementation of timestamptz_to_str(). Use correct type for "result", fix bogus strftime argument, don't use unnecessary static variables, improve comments. Andres Freund and Tom Lane --- contrib/pg_xlogdump/compat.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/contrib/pg_xlogdump/compat.c b/contrib/pg_xlogdump/compat.c index 99ecad7f62..3f3912193a 100644 --- a/contrib/pg_xlogdump/compat.c +++ b/contrib/pg_xlogdump/compat.c @@ -41,25 +41,30 @@ timestamptz_to_time_t(TimestampTz t) /* * Stopgap implementation of timestamptz_to_str that doesn't depend on backend - * infrastructure. + * infrastructure. This will work for timestamps that are within the range + * of the platform time_t type. (pg_time_t is compatible except for possibly + * being wider.) + * + * XXX the return value points to a static buffer, so beware of using more + * than one result value concurrently. * * XXX: The backend timestamp infrastructure should instead be split out and - * moved into src/common. + * moved into src/common. That's a large project though. */ const char * timestamptz_to_str(TimestampTz dt) { static char buf[MAXDATELEN + 1]; - static char ts[MAXDATELEN + 1]; - static char zone[MAXDATELEN + 1]; - pg_time_t result = timestamptz_to_time_t(dt); + char ts[MAXDATELEN + 1]; + char zone[MAXDATELEN + 1]; + time_t result = (time_t) timestamptz_to_time_t(dt); struct tm *ltime = localtime(&result); - strftime(ts, sizeof(zone), "%Y-%m-%d %H:%M:%S", ltime); + strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S", ltime); strftime(zone, sizeof(zone), "%Z", ltime); #ifdef HAVE_INT64_TIMESTAMP - sprintf(buf, "%s.%06d %s", ts, (int)(dt % USECS_PER_SEC), zone); + sprintf(buf, "%s.%06d %s", ts, (int) (dt % USECS_PER_SEC), zone); #else sprintf(buf, "%s.%.6f %s", ts, fabs(dt - floor(dt)), zone); #endif