]> diplodocus.org Git - nmh/commitdiff
Simplify dtimezone()'s logic by working with unsigned int.
authorRalph Corderoy <ralph@inputplus.co.uk>
Sat, 29 Jul 2017 13:29:15 +0000 (14:29 +0100)
committerRalph Corderoy <ralph@inputplus.co.uk>
Sat, 29 Jul 2017 13:35:05 +0000 (14:35 +0100)
Character buffer is resized down from arbitrary 64 to suit 32-bit int,
a change to the recent a3724ed3.

sbr/dtime.c

index d52662bc4b457ee2317cee82ec8f1dd8953f77f0..3a51d87ff11145783475c06b76e9ce8b87760c9c 100644 (file)
@@ -284,24 +284,23 @@ dasctime (struct tws *tw, int flags)
  * return the string representation of the numeric offset.
  */
 
  * 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;
 }
 
     return buffer;
 }