Skip to content

Commit 1418e6e

Browse files
committed
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
1 parent c153530 commit 1418e6e

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

contrib/pg_xlogdump/compat.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,30 @@ timestamptz_to_time_t(TimestampTz t)
4141

4242
/*
4343
* Stopgap implementation of timestamptz_to_str that doesn't depend on backend
44-
* infrastructure.
44+
* infrastructure. This will work for timestamps that are within the range
45+
* of the platform time_t type. (pg_time_t is compatible except for possibly
46+
* being wider.)
47+
*
48+
* XXX the return value points to a static buffer, so beware of using more
49+
* than one result value concurrently.
4550
*
4651
* XXX: The backend timestamp infrastructure should instead be split out and
47-
* moved into src/common.
52+
* moved into src/common. That's a large project though.
4853
*/
4954
const char *
5055
timestamptz_to_str(TimestampTz dt)
5156
{
5257
static char buf[MAXDATELEN + 1];
53-
static char ts[MAXDATELEN + 1];
54-
static char zone[MAXDATELEN + 1];
55-
pg_time_t result = timestamptz_to_time_t(dt);
58+
char ts[MAXDATELEN + 1];
59+
char zone[MAXDATELEN + 1];
60+
time_t result = (time_t) timestamptz_to_time_t(dt);
5661
struct tm *ltime = localtime(&result);
5762

58-
strftime(ts, sizeof(zone), "%Y-%m-%d %H:%M:%S", ltime);
63+
strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S", ltime);
5964
strftime(zone, sizeof(zone), "%Z", ltime);
6065

6166
#ifdef HAVE_INT64_TIMESTAMP
62-
sprintf(buf, "%s.%06d %s", ts, (int)(dt % USECS_PER_SEC), zone);
67+
sprintf(buf, "%s.%06d %s", ts, (int) (dt % USECS_PER_SEC), zone);
6368
#else
6469
sprintf(buf, "%s.%.6f %s", ts, fabs(dt - floor(dt)), zone);
6570
#endif

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy