cgit: [PATCH] timegm() compat for non-Linux and non-BSD

Jason A. Donenfeld Jason at zx2c4.com
Tue Feb 23 03:15:28 CET 2016


>From 34e1cfd7f56e20c577cebba31e8235e101f95972 Mon Sep 17 00:00:00 2001
From: Brad Forschinger <bnjf at users.noreply.github.com>
Date: Tue, 23 Feb 2016 10:18:34 +1100
Subject: [PATCH] timegm() compat for non-Linux and non-BSD

---
 cgit.mk    |  9 +++++++++
 ui-stats.c | 19 +++++++++++++++++++
 2 files changed, 28 insertions(+)

diff --git a/cgit.mk b/cgit.mk
index 1b50307..4f75264 100644
--- a/cgit.mk
+++ b/cgit.mk
@@ -65,6 +65,15 @@ ifdef HAVE_LINUX_SENDFILE
  CGIT_CFLAGS += -DHAVE_LINUX_SENDFILE
 endif

+# from timegm(3): These functions are non-standard GNU extensions that
+# are also present on the BSDs. Avoid their use; see NOTES.
+ifeq ($(findstring BSD,$(uname_S)),BSD)
+ CGIT_CFLAGS += -DHAVE_TIMEGM
+endif
+ifeq ($(uname_S),Linux)
+ CGIT_CFLAGS += -DHAVE_TIMEGM
+endif
+
 CGIT_OBJ_NAMES += cgit.o
 CGIT_OBJ_NAMES += cache.o
 CGIT_OBJ_NAMES += cmd.o
diff --git a/ui-stats.c b/ui-stats.c
index 8cd9178..67b99aa 100644
--- a/ui-stats.c
+++ b/ui-stats.c
@@ -11,6 +11,25 @@ struct authorstat {
 #define DAY_SECS (60 * 60 * 24)
 #define WEEK_SECS (DAY_SECS * 7)

+#ifndef HAVE_TIMEGM
+time_t timegm(struct tm *tm)
+{
+ time_t ret;
+ char *tz;
+
+ tz = getenv("TZ");
+ setenv("TZ", "", 1);
+ tzset();
+ ret = mktime(tm);
+ if (tz)
+ setenv("TZ", tz, 1);
+ else
+ unsetenv("TZ");
+ tzset();
+ return ret;
+}
+#endif
+
 static void trunc_week(struct tm *tm)
 {
  time_t t = timegm(tm);
-- 
2.4.3


More information about the CGit mailing list