{"thread":{"id":"1905","subject":"Fix strange timezone handling","startedAt":"2005-09-21T22:50:28Z","lastAt":"2005-09-21T22:50:28Z","messageCount":1,"participants":["Linus Torvalds"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"9078","messageId":"Pine.LNX.4.58.0509211527500.2553@g5.osdl.org","threadId":"1905","inReplyTo":null,"subject":"Fix strange timezone handling","fromName":"Linus Torvalds","fromEmail":"torvalds@osdl.org","sentAt":"2005-09-21T22:50:28Z","receivedAt":"2005-09-21T22:50:28Z","isPatch":false,"sender":{"key":"torvalds@linux-foundation.org","avatar":"https://avatars.githubusercontent.com/u/1024025?v=4"},"body":"\nWe generate the ASCII representation of our internal date representation \n(\"seconds since 1970, UTC + timezone information\") in two different \nplaces.\n\nOne of them uses the stupid and obvious way to make sure that it gets the\nsexagecimal representation right for negative timezones even if they might\nnot be exact hours, and the other one depends on the modulus operator\nalways matching the sign of argument.\n\nHey, the clever one works. And C90 even specifies that behaviour. But I\nhad to think about it for a while when I was re-visiting this area, and\neven if I didn't have to, it's kind of strange to have two different ways\nto print out the same data format.\n\nSo use a common helper for this. And select the stupid and straighforward\nway.\n\nSigned-off-by: Linus Torvalds <torvalds@osdl.org>\n---\n\nYeah. Maybe I should have picked the smart one-liner one. Somebody should\ndouble-check this and make an informed decision.\n\ndiff --git a/date.c b/date.c\n--- a/date.c\n+++ b/date.c\n@@ -386,12 +386,23 @@ static int match_tz(const char *date, in\n \treturn end - date;\n }\n \n+static int date_string(unsigned long date, int offset, char *buf, int len)\n+{\n+\tint sign = '+';\n+\n+\tif (offset < 0) {\n+\t\toffset = -offset;\n+\t\tsign = '-';\n+\t}\n+\treturn snprintf(buf, len, \"%lu %c%02d%02d\", date, sign, offset / 60, offset % 60);\n+}\n+\n /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822\n    (i.e. English) day/month names, and it doesn't work correctly with %z. */\n int parse_date(const char *date, char *result, int maxlen)\n {\n \tstruct tm tm;\n-\tint offset, sign, tm_gmt;\n+\tint offset, tm_gmt;\n \ttime_t then;\n \n \tmemset(&tm, 0, sizeof(tm));\n@@ -435,14 +446,7 @@ int parse_date(const char *date, char *r\n \n \tif (!tm_gmt)\n \t\tthen -= offset * 60;\n-\n-\tsign = '+';\n-\tif (offset < 0) {\n-\t\toffset = -offset;\n-\t\tsign = '-';\n-\t}\n-\n-\treturn snprintf(result, maxlen, \"%lu %c%02d%02d\", then, sign, offset/60, offset % 60);\n+\treturn date_string(then, offset, result, maxlen);\n }\n \n void datestamp(char *buf, int bufsize)\n@@ -455,5 +459,5 @@ void datestamp(char *buf, int bufsize)\n \toffset = my_mktime(localtime(&now)) - now;\n \toffset /= 60;\n \n-\tsnprintf(buf, bufsize, \"%lu %+05d\", now, offset/60*100 + offset%60);\n+\tdate_string(now, offset, buf, bufsize);\n }\n"}]}