Re: [PATCH 10/10] timegm: add portable implementation (Solaris support)
authorBlake Jones <blakej@foo.net>
Mon, 5 Nov 2012 15:47:22 +0000 (07:47 +1600)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:50:20 +0000 (09:50 -0800)
83/ac1bf1602c588fb077f351c9510b37dd6a87d2 [new file with mode: 0644]

diff --git a/83/ac1bf1602c588fb077f351c9510b37dd6a87d2 b/83/ac1bf1602c588fb077f351c9510b37dd6a87d2
new file mode 100644 (file)
index 0000000..0b5cd57
--- /dev/null
@@ -0,0 +1,210 @@
+Return-Path: <blakej@foo.net>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id 4C880431FB6\r
+       for <notmuch@notmuchmail.org>; Mon,  5 Nov 2012 07:47:31 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
+       autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id 0yx8uOLeaIz7 for <notmuch@notmuchmail.org>;\r
+       Mon,  5 Nov 2012 07:47:30 -0800 (PST)\r
+Received: from foo.net (70-36-235-136.dsl.static.sonic.net [70.36.235.136])\r
+       (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 70D62431FAE\r
+       for <notmuch@notmuchmail.org>; Mon,  5 Nov 2012 07:47:30 -0800 (PST)\r
+Received: from foo.net (localhost [127.0.0.1])\r
+       by foo.net (8.14.5+Sun/8.14.5) with ESMTP id qA5FlM1X002593;\r
+       Mon, 5 Nov 2012 07:47:22 -0800 (PST)\r
+To: Tomi Ollila <tomi.ollila@iki.fi>\r
+Subject: Re: [PATCH 10/10] timegm: add portable implementation (Solaris\r
+       support) \r
+In-Reply-To: Your message of "Mon, 05 Nov 2012 14:09:33 +0200."\r
+       <m2txt4jloi.fsf@guru.guru-group.fi> \r
+MIME-Version: 1.0\r
+Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0"\r
+Content-ID: <2591.1352130440.0@foo.net>\r
+Date: Mon, 05 Nov 2012 07:47:22 -0800\r
+Message-ID: <2592.1352130442@foo.net>\r
+From: Blake Jones <blakej@foo.net>\r
+X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-2.0.2\r
+       (foo.net [127.0.0.1]); Mon, 05 Nov 2012 07:47:22 -0800 (PST)\r
+Cc: notmuch@notmuchmail.org\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Mon, 05 Nov 2012 15:47:31 -0000\r
+\r
+------- =_aaaaaaaaaa0\r
+Content-Type: text/plain; charset="us-ascii"\r
+Content-ID: <2591.1352130440.1@foo.net>\r
+\r
+> Yet another idea for an alternative. Compile by entering 'sh xtimegm.c'\r
+> and then run ./xtimegm\r
+> \r
+> Simple cases seems to work. Dst change may (or then may not) give one\r
+> hour difference to the expected. The test "coverage" could be easily\r
+> expanded to that ;)\r
+> \r
+> Hmm, I also found this:\r
+> http://lists.gnu.org/archive/html/bug-gnulib/2003-09/msg00004.html\r
+> which does 2 mktime's and one gmtime_r (without allocating tg!)...\r
+> Pick any of these 3 -- or something different (e.g. less NIH if there is)\r
+\r
+Of these two, I would probably lean slightly toward the latter, in that\r
+it relies more on libc for the time zone handling logic.\r
+\r
+But in general, this seems to me like a case where an explicit\r
+implementation like mine is less prone to failure, because it doesn't\r
+need to worry about time zones at all.  The other approaches rely on\r
+letting libc do all the hard work of time zone manipulation, and then\r
+reading the tea leaves to find a way to undo it.  I would guess that if\r
+some change in the time standards is going to break one of these\r
+implementations, it's going to be some new time zone specification, not\r
+a change in the number of days in a month. :)\r
+\r
+For what it's worth, I used the attached program to test my\r
+implementation, and it passed.\r
+\r
+Blake\r
+\r
+\r
+------- =_aaaaaaaaaa0\r
+Content-Type: text/plain; charset="us-ascii"\r
+Content-ID: <2591.1352130440.2@foo.net>\r
+\r
+#include <time.h>\r
+\r
+static int\r
+leapyear(int year)\r
+{\r
+       return ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0));\r
+}\r
+\r
+/*\r
+ * This is a simple implementation of timegm() which does what is needed\r
+ * by parse-time-string.c -- just turns the "struct tm" into a GMT time_t.\r
+ * It does not normalize any of the fields of the "struct tm", nor does\r
+ * it set tm_wday or tm_yday.\r
+ */\r
+static time_t\r
+timegm(struct tm *tm)\r
+{\r
+       int     monthlen[2][12] = {\r
+               { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },\r
+               { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },\r
+       };\r
+       int     year, month, days;\r
+\r
+       days = 365 * (tm->tm_year - 70);\r
+       for (year = 70; year < tm->tm_year; year++) {\r
+               if (leapyear(1900 + year)) {\r
+                       days++;\r
+               }\r
+       }\r
+       for (month = 0; month < tm->tm_mon; month++) {\r
+               days += monthlen[leapyear(1900 + year)][month];\r
+       }\r
+       days += tm->tm_mday - 1;\r
+\r
+       return ((((days * 24) + tm->tm_hour) * 60 + tm->tm_min) * 60 +\r
+           tm->tm_sec);\r
+}\r
+\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <unistd.h>\r
+\r
+void\r
+tm_test(int niter)\r
+{\r
+       const int       monthlen[2][12] = {\r
+               { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },\r
+               { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },\r
+       };\r
+       int             i;\r
+       struct tm       tm, *tmp;\r
+       time_t          t;\r
+\r
+       for (i = 0; i < niter; i++) {\r
+               tm.tm_year = (lrand48() % 67) + 70;\r
+               tm.tm_mon = lrand48() % 12;\r
+               do {\r
+                       t = (lrand48() % 31) + 1;\r
+               } while (t > monthlen[leapyear(1900 + tm.tm_year)][tm.tm_mon]);\r
+               tm.tm_mday = t;\r
+               tm.tm_hour = lrand48() % 24;\r
+               tm.tm_min = lrand48() % 60;\r
+               tm.tm_sec = lrand48() % 60;\r
+\r
+               t = timegm(&tm);\r
+               tmp = gmtime(&t);\r
+\r
+               if (tmp->tm_sec != tm.tm_sec ||\r
+                   tmp->tm_min != tm.tm_min ||\r
+                   tmp->tm_hour != tm.tm_hour ||\r
+                   tmp->tm_mday != tm.tm_mday ||\r
+                   tmp->tm_mon != tm.tm_mon ||\r
+                   tmp->tm_year != tm.tm_year) {\r
+                       printf("%4d-%02d-%02d %02d:%02d:%02d -> "\r
+                           "%4d-%02d-%02d %02d:%02d:%02d\n",\r
+                           tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,\r
+                           tm.tm_hour, tm.tm_min, tm.tm_sec,\r
+                           tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,\r
+                           tmp->tm_hour, tmp->tm_min, tmp->tm_sec);\r
+\r
+               }\r
+       }\r
+}\r
+\r
+void\r
+time_test(int niter)\r
+{\r
+       int             i;\r
+       time_t          st, et;\r
+       struct tm       *tmp;\r
+\r
+       for (i = 0; i < niter; i++) {\r
+               st = (time_t)lrand48();\r
+\r
+               tmp = gmtime(&st);\r
+               et = timegm(tmp);\r
+\r
+               if (st != et) {\r
+                       printf("%d -> %d (%4d-%02d-%02d %02d:%02d:%02d)\n",\r
+                           st, et,\r
+                           tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,\r
+                           tmp->tm_hour, tmp->tm_min, tmp->tm_sec);\r
+               }\r
+       }\r
+}\r
+\r
+int\r
+main(void)\r
+{\r
+       const int       niter = 10000000;\r
+\r
+       srand48(getpid());\r
+\r
+       tm_test(niter);\r
+       time_test(niter);\r
+\r
+       return (0);\r
+}\r
+\r
+------- =_aaaaaaaaaa0--\r