[PATCH 10/10] timegm: add portable implementation (Solaris support)
authorBlake Jones <blakej@foo.net>
Sun, 4 Nov 2012 03:16:02 +0000 (20:16 +1700)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:50:17 +0000 (09:50 -0800)
12/9949d1d52b3c2a2b1655df3ca7b1311d35a46f [new file with mode: 0644]

diff --git a/12/9949d1d52b3c2a2b1655df3ca7b1311d35a46f b/12/9949d1d52b3c2a2b1655df3ca7b1311d35a46f
new file mode 100644 (file)
index 0000000..59a94c3
--- /dev/null
@@ -0,0 +1,133 @@
+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 AEE03431FB6\r
+       for <notmuch@notmuchmail.org>; Sat,  3 Nov 2012 20:24:19 -0700 (PDT)\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 wmoXqTgaSg+O for <notmuch@notmuchmail.org>;\r
+       Sat,  3 Nov 2012 20:24:19 -0700 (PDT)\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 07590431FAF\r
+       for <notmuch@notmuchmail.org>; Sat,  3 Nov 2012 20:24:18 -0700 (PDT)\r
+Received: from foo.net (localhost [127.0.0.1])\r
+       by foo.net (8.14.5+Sun/8.14.5) with ESMTP id qA43GHLR025715;\r
+       Sat, 3 Nov 2012 20:16:17 -0700 (PDT)\r
+Received: (from blakej@localhost)\r
+       by foo.net (8.14.5+Sun/8.14.5/Submit) id qA43GHCQ025714;\r
+       Sat, 3 Nov 2012 20:16:17 -0700 (PDT)\r
+From: Blake Jones <blakej@foo.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 10/10] timegm: add portable implementation (Solaris support)\r
+Date: Sat,  3 Nov 2012 20:16:02 -0700\r
+Message-Id: <1351998962-25135-11-git-send-email-blakej@foo.net>\r
+X-Mailer: git-send-email 1.7.3.2\r
+In-Reply-To: <1351998962-25135-1-git-send-email-blakej@foo.net>\r
+References: <1351998962-25135-1-git-send-email-blakej@foo.net>\r
+X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-2.0.2\r
+       (foo.net [127.0.0.1]); Sat, 03 Nov 2012 20:16:17 -0700 (PDT)\r
+X-Mailman-Approved-At: Sun, 04 Nov 2012 01:30:19 -0800\r
+Cc: Blake Jones <blakej@foo.net>\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: Sun, 04 Nov 2012 03:24:19 -0000\r
+\r
+The timegm(3) function is a non-standard extension to libc which is\r
+available in GNU libc and on some BSDs.  Although SunOS had this\r
+function in its libc, Solaris (unfortunately) removed it.  This patch\r
+implements a very simple version of timegm() which is good enough for\r
+parse-time-string.c.\r
+\r
+Although notmuch's idiom for portability is to test for native\r
+availability and put alternate versions in compat/, that approach led to\r
+a compilation problem in this case.  libnotmuch.a includes a call to\r
+parse_time_string() from parse-time-vrp.o, and parse_time_string() in\r
+libparse-time-string.a needs to call timegm().  An attempt to create\r
+compat/timegm.c caused the link to fail, because libparse-time-string.a\r
+acquired a dependency on the new timegm.o in libnotmuch.a, and the\r
+linker only does a single pass on each ".a" looking for dependencies.\r
+This seems to be the case both for the GNU linker and the Solaris\r
+linker.  A different possible workaround would have been to include\r
+libnotmuch.a multiple times on the link line, but that seemed like a\r
+brittle way to track this dependency.\r
+---\r
+ parse-time-string/parse-time-string.c |   37 ++++++++++++++++++++++++++++++++-\r
+ 1 file changed, 36 insertions(+), 1 deletion(-)\r
+\r
+diff --git a/parse-time-string/parse-time-string.c b/parse-time-string/parse-time-string.c\r
+index 584067d..28901af 100644\r
+--- a/parse-time-string/parse-time-string.c\r
++++ b/parse-time-string/parse-time-string.c\r
+@@ -1315,6 +1315,41 @@ fixup_ampm (struct state *state)\r
+     return 0;\r
+ }\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 create_output() -- 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
++local_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 + tm->tm_sec);\r
++}\r
++\r
+ /* Combine absolute and relative fields, and round. */\r
+ static int\r
+ create_output (struct state *state, time_t *t_out, const time_t *ref,\r
+@@ -1465,7 +1500,7 @@ create_output (struct state *state, time_t *t_out, const time_t *ref,\r
+     if (is_field_set (state, TM_TZ)) {\r
+       /* tm is in specified TZ, convert to UTC for timegm(3). */\r
+       tm.tm_min -= get_field (state, TM_TZ);\r
+-      t = timegm (&tm);\r
++      t = local_timegm (&tm);\r
+     } else {\r
+       /* tm is in local time. */\r
+       t = mktime (&tm);\r
+-- \r
+1.7.9.2\r
+\r