From 5aef52b59ecd185d54604557ac8d664c74e76a09 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Fri, 13 Nov 2026 10:58:53 +0100 Subject: [PATCH 1/5] Remove strftime workarounds for -Wformat-y2k Ancient gcc versions warned about some strftime placeholders (possibly) producing two-digit years. This warning was included in -Wformat, which was included in -Wall. It was not possible to turn this off without turning off all of -Wformat. As a consequence, PostgreSQL code has accumulated a bunch of workarounds to silence this warning by contorting the code to confuse the compiler. But this is long obsolete. gcc 3.0 added the option -Wno-format-y2k that allowed turning off this specific warning. gcc 3.4 turned this warning off by default. We require at least gcc 4.9 now. So undo all these workarounds and remove any commentary about it. --- src/bin/initdb/initdb.c | 9 +-------- src/bin/pg_controldata/pg_controldata.c | 8 ++------ src/bin/psql/command.c | 10 +--------- src/interfaces/ecpg/pgtypeslib/timestamp.c | 10 ++-------- 4 files changed, 6 insertions(+), 31 deletions(-) diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c index 86d42a98a27..f2797ebedc1 100644 --- a/src/bin/initdb/initdb.c +++ b/src/bin/initdb/initdb.c @@ -2137,13 +2137,6 @@ check_ok(void) } } -/* Hack to suppress a warning about %x from some versions of gcc */ -static inline size_t -my_strftime(char *s, size_t max, const char *fmt, const struct tm *tm) -{ - return strftime(s, max, fmt, tm); -} - /* * Determine likely date order from locale */ @@ -2170,7 +2163,7 @@ locale_date_order(const char *locale) testtime.tm_mon = 10; /* November, should come out as "11" */ testtime.tm_year = 133; /* 2033 */ - res = my_strftime(buf, sizeof(buf), "%x", &testtime); + res = strftime(buf, sizeof(buf), "%x", &testtime); restore_global_locale(LC_TIME, save); diff --git a/src/bin/pg_controldata/pg_controldata.c b/src/bin/pg_controldata/pg_controldata.c index 6a0f848d8d0..97722bbe410 100644 --- a/src/bin/pg_controldata/pg_controldata.c +++ b/src/bin/pg_controldata/pg_controldata.c @@ -101,7 +101,6 @@ main(int argc, char *argv[]) char pgctime_str[128]; char ckpttime_str[128]; char mock_auth_nonce_str[MOCK_AUTH_NONCE_LEN * 2 + 1]; - const char *strftime_fmt = "%c"; const char *progname; char xlogfilename[MAXFNAMELEN]; int c; @@ -199,15 +198,12 @@ main(int argc, char *argv[]) * timestamps are within the range of time_t; that should be the case in * all foreseeable circumstances, so we don't bother importing the * backend's timezone library into pg_controldata. - * - * Use variable for format to suppress overly-anal-retentive gcc warning - * about %c */ time_tmp = (time_t) ControlFile->time; tm_tmp = localtime(&time_tmp); if (tm_tmp != NULL) - strftime(pgctime_str, sizeof(pgctime_str), strftime_fmt, tm_tmp); + strftime(pgctime_str, sizeof(pgctime_str), "%c", tm_tmp); else snprintf(pgctime_str, sizeof(pgctime_str), _("???")); @@ -215,7 +211,7 @@ main(int argc, char *argv[]) tm_tmp = localtime(&time_tmp); if (tm_tmp != NULL) - strftime(ckpttime_str, sizeof(ckpttime_str), strftime_fmt, tm_tmp); + strftime(ckpttime_str, sizeof(ckpttime_str), "%c", tm_tmp); else snprintf(ckpttime_str, sizeof(ckpttime_str), _("???")); diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index e0c4d7a3562..2fc3aa4413f 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -5928,7 +5928,6 @@ do_watch(PQExpBuffer query_buf, double sleep, int iter, int min_rows) { long sleep_ms = (long) (sleep * 1000); printQueryOpt myopt = pset.popt; - const char *strftime_fmt; const char *user_title; char *title; const char *pagerprog = NULL; @@ -6008,13 +6007,6 @@ do_watch(PQExpBuffer query_buf, double sleep, int iter, int min_rows) restore_sigpipe_trap(); } - /* - * 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 unless * PSQL_WATCH_PAGER was successfully launched. @@ -6043,7 +6035,7 @@ do_watch(PQExpBuffer query_buf, double sleep, int iter, int min_rows) * makes for reasonably nicely formatted output in simple cases. */ timer = time(NULL); - strftime(timebuf, sizeof(timebuf), strftime_fmt, localtime(&timer)); + strftime(timebuf, sizeof(timebuf), "%c", localtime(&timer)); if (user_title) snprintf(title, title_len, _("%s\t%s (every %gs)\n"), diff --git a/src/interfaces/ecpg/pgtypeslib/timestamp.c b/src/interfaces/ecpg/pgtypeslib/timestamp.c index 46cd20974eb..98f8a98d028 100644 --- a/src/interfaces/ecpg/pgtypeslib/timestamp.c +++ b/src/interfaces/ecpg/pgtypeslib/timestamp.c @@ -439,11 +439,8 @@ dttofmtasc_replace(timestamp * ts, date dDate, int dow, struct tm *tm, */ case 'g': { - const char *fmt = "%g"; /* Keep compiler quiet about - * 2-digit year */ - tm->tm_mon -= 1; - i = strftime(q, *pstr_len, fmt, tm); + i = strftime(q, *pstr_len, "%g", tm); if (i == 0) return -1; while (*q) @@ -641,11 +638,8 @@ dttofmtasc_replace(timestamp * ts, date dDate, int dow, struct tm *tm, */ case 'x': { - const char *fmt = "%x"; /* Keep compiler quiet about - * 2-digit year */ - tm->tm_mon -= 1; - i = strftime(q, *pstr_len, fmt, tm); + i = strftime(q, *pstr_len, "%x", tm); if (i == 0) return -1; while (*q) base-commit: 1fe0cfddf0e9a658161d03ad256bec0b2a30231d -- 2.56.0