From: Ralph Corderoy Date: Sat, 29 Jul 2017 13:29:15 +0000 (+0100) Subject: Simplify dtimezone()'s logic by working with unsigned int. X-Git-Url: https://diplodocus.org/git/nmh/commitdiff_plain/6e1c913f4e0ced806180c7069ca12f79f15e5e23?hp=0f7f36b7ca85aa43f49238e7fed61a2db81e12df Simplify dtimezone()'s logic by working with unsigned int. Character buffer is resized down from arbitrary 64 to suit 32-bit int, a change to the recent a3724ed3. --- diff --git a/sbr/dtime.c b/sbr/dtime.c index d52662bc..3a51d87f 100644 --- a/sbr/dtime.c +++ b/sbr/dtime.c @@ -284,24 +284,23 @@ dasctime (struct tws *tw, int flags) * return the string representation of the numeric offset. */ -char * -dtimezone (int offset, int flags) +char *dtimezone(int offset, int flags) { - int hours, mins; - static char buffer[64]; + static char buffer[sizeof "+3579139459"]; /* 2,147,483,648 / 60 = 35,791,394 */ + bool pos; + unsigned os, hours, mins; - if (offset < 0) { - mins = -((-offset) % 60); - hours = -((-offset) / 60); - } else { - mins = offset % 60; - hours = offset / 60; - } + pos = offset >= 0; + os = pos ? offset : ~offset + 1; + hours = os / 60; + mins = os % 60; + + if (flags & TW_DST) /* Shift towards +infinity. */ + hours += pos ? 1 : -1; + + snprintf(buffer, sizeof(buffer), "%c%02u%02u", + pos ? '+' : '-', hours, mins); - if (flags & TW_DST) - hours++; - snprintf (buffer, sizeof(buffer), "%s%02d%02d", - offset < 0 ? "-" : "+", abs (hours), abs (mins)); return buffer; }