From d789ad9dbe9fba13cb74b6bbb0b571d57b3f4f3e Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sun, 26 Jan 2025 22:15:48 -0800 Subject: [PATCH] Fix unlikely time zone abbreviation bug * src/timefns.c (Fcurrent_time_zone): Fix double-"-" bug when time zone name is not known and time zone offset is -3600 or less. Also, stop assuming that make_formatted_string works with "*" precisions; this might ease future changes. (cherry picked from commit bcfd4d21b0a62c5c15b919fa28673066c493775a) --- src/timefns.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/timefns.c b/src/timefns.c index 4fb142f2f1b..590d84c4d65 100644 --- a/src/timefns.c +++ b/src/timefns.c @@ -1950,15 +1950,24 @@ the data it can't find. */) /* No local time zone name is available; use numeric zone instead. */ long int hour = offset / 3600; int min_sec = offset % 3600; - int amin_sec = eabs (min_sec); - int min = amin_sec / 60; - int sec = amin_sec % 60; - int min_prec = min_sec ? 2 : 0; - int sec_prec = sec ? 2 : 0; - char buf[sizeof "+0000" + INT_STRLEN_BOUND (long int)]; - zone_name = make_formatted_string (buf, "%c%.2ld%.*d%.*d", - (offset < 0 ? '-' : '+'), - hour, min_prec, min, sec_prec, sec); + char buf[INT_STRLEN_BOUND (long int) + sizeof "5959"]; + int buflen = 0; + buf[buflen++] = offset < 0 ? '-' : '+'; + buflen += sprintf (buf + buflen, "%.2ld", eabs (hour)); + if (min_sec) + { + int amin_sec = eabs (min_sec); + int min = amin_sec / 60; + int sec = amin_sec % 60; + buf[buflen++] = '0' + min / 10; + buf[buflen++] = '0' + min % 10; + if (sec) + { + buf[buflen++] = '0' + sec / 10; + buf[buflen++] = '0' + sec % 10; + } + } + zone_name = make_string (buf, buflen); } } -- 2.39.5