|
Message-Id: <20180725101142.29159-1-zajec5@gmail.com> Date: Wed, 25 Jul 2018 12:11:42 +0200 From: Rafał Miłecki <zajec5@...il.com> To: musl@...ts.openwall.com Cc: Rafał Miłecki <rafal@...ecki.pl> Subject: [PATCH] strftime: fix %z sign for small negative time zone offsets From: Rafał Miłecki <rafal@...ecki.pl> Using + printf flag for printing plus/minus sign isn't reliable for negative offsets greater than -3600. In such cases the first two digits are zero but offset still should be printed with a leading minus char. Existing implementation results in code: struct tm tm = { .tm_gmtoff = -1800, }; char buf[255]; strftime(buf, sizeof(buf), "%z", &tm); puts(buf); printing +0030 instead of -0030. This patch fixes it by handling sign character manually and checking the __tm_gmtoff value (instead of __tm_gmtoff / 3600). Signed-off-by: Rafał Miłecki <rafal@...ecki.pl> --- src/time/strftime.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/time/strftime.c b/src/time/strftime.c index 0a256970..6716ad4b 100644 --- a/src/time/strftime.c +++ b/src/time/strftime.c @@ -181,7 +181,8 @@ const char *__strftime_fmt_1(char (*s)[100], size_t *l, int f, const struct tm * *l = 0; return ""; } - *l = snprintf(*s, sizeof *s, "%+.2ld%.2d", + *l = snprintf(*s, sizeof *s, "%c%.2ld%.2d", + tm->__tm_gmtoff >= 0 ? '+' : '-', (tm->__tm_gmtoff)/3600, abs(tm->__tm_gmtoff%3600)/60); return *s; -- 2.13.7
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.