List for cgit developers and users
 help / color / mirror / Atom feed
From: john at keeping.me.uk (John Keeping)
Subject: [PATCH 2/2] compat/timegm: new compat function for systems lacking timegm()
Date: Mon,  1 Apr 2013 14:14:13 +0100	[thread overview]
Message-ID: <75a384665737f5f9a1768641bc673c91bfa7ebca.1364821972.git.john@keeping.me.uk> (raw)
In-Reply-To: <355839ae094873aef82ea1c8b7b7f9f7cc873b9c.1364821972.git.john@keeping.me.uk>

The implementation used here is taken from Samba, since discussions on
their mailing list indicate that the "portable version" in timegm(3)
does not actually work on Solaris.

This commit also adds the infrastructure required for compat functions
in CGit itself.  Mostly we get everything we need from Git, but timegm
is not used in Git so there is no reason for them to carry a
compatibility version.

Signed-off-by: John Keeping <john at keeping.me.uk>
---
I wanted to split this into two patches, one to introduce "compat"
infrastructure and one to add timegm.c but I couldn't really see a clean
way of doing that and I don't think it's too bad as a single patch.

 cgit-compat-util.h | 11 ++++++++
 cgit.h             |  2 +-
 cgit.mk            | 14 ++++++++--
 compat/timegm.c    | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 102 insertions(+), 3 deletions(-)
 create mode 100644 cgit-compat-util.h
 create mode 100644 compat/timegm.c

diff --git a/cgit-compat-util.h b/cgit-compat-util.h
new file mode 100644
index 0000000..2573f32
--- /dev/null
+++ b/cgit-compat-util.h
@@ -0,0 +1,11 @@
+#ifndef CGIT_COMPAT_UTIL_H
+#define CGIT_COMPAT_UTIL_H
+
+#include "git/git-compat-util.h"
+
+#ifdef NO_TIMEGM
+#define timegm cgittimegm
+extern time_t cgittimegm(struct tm *tm);
+#endif
+
+#endif
diff --git a/cgit.h b/cgit.h
index ed5cf14..0a9f7f5 100644
--- a/cgit.h
+++ b/cgit.h
@@ -2,7 +2,7 @@
 #define CGIT_H
 
 
-#include <git-compat-util.h>
+#include "cgit-compat-util.h"
 #include <cache.h>
 #include <grep.h>
 #include <object.h>
diff --git a/cgit.mk b/cgit.mk
index bf3bbb1..b8c7b68 100644
--- a/cgit.mk
+++ b/cgit.mk
@@ -15,12 +15,19 @@ $(CGIT_PREFIX)VERSION: force-version
 -include $(CGIT_PREFIX)VERSION
 .PHONY: force-version
 
+ifdef NO_TIMEGM
+	CGIT_COMPAT_CFLAGS += -DNO_TIMEGM
+	CGIT_COMPAT_OBJ_NAMES += compat/timegm.o
+endif
+
 # CGIT_CFLAGS is a separate variable so that we can track it separately
 # and avoid rebuilding all of Git when these variables change.
 CGIT_CFLAGS += -DCGIT_CONFIG='"$(CGIT_CONFIG)"'
 CGIT_CFLAGS += -DCGIT_SCRIPT_NAME='"$(CGIT_SCRIPT_NAME)"'
 CGIT_CFLAGS += -DCGIT_CACHE_ROOT='"$(CACHE_ROOT)"'
 
+CGIT_CFLAGS += $(CGIT_COMPAT_CFLAGS)
+
 ifdef NO_C99_FORMAT
 	CFLAGS += -DNO_C99_FORMAT
 endif
@@ -52,7 +59,7 @@ CGIT_OBJ_NAMES += ui-tag.o
 CGIT_OBJ_NAMES += ui-tree.o
 CGIT_OBJ_NAMES += vector.o
 
-CGIT_OBJS := $(addprefix $(CGIT_PREFIX),$(CGIT_OBJ_NAMES))
+CGIT_OBJS := $(addprefix $(CGIT_PREFIX),$(CGIT_OBJ_NAMES) $(CGIT_COMPAT_OBJ_NAMES))
 
 # Only cgit.c reference CGIT_VERSION so we only rebuild its objects when the
 # version changes.
@@ -65,8 +72,11 @@ $(CGIT_VERSION_OBJS): EXTRA_CPPFLAGS = \
 ifeq ($(wildcard $(CGIT_PREFIX).depend),)
 missing_dep_dirs += $(CGIT_PREFIX).depend
 endif
+ifeq ($(wildcard $(CGIT_PREFIX)compat/.depend),)
+missing_dep_dirs += $(CGIT_PREFIX)compat/.depend
+endif
 
-$(CGIT_PREFIX).depend:
+$(addprefix $(CGIT_PREFIX),.depend compat/.depend):
 	@mkdir -p $@
 
 $(CGIT_PREFIX)CGIT-CFLAGS: FORCE
diff --git a/compat/timegm.c b/compat/timegm.c
new file mode 100644
index 0000000..1491538
--- /dev/null
+++ b/compat/timegm.c
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 1997 Kungliga Tekniska H?gskolan
+ * (Royal Institute of Technology, Stockholm, Sweden).
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of the Institute nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * Adapted for Samba4 by Andrew Tridgell.
+ * Adapted for CGit by John Keeping.
+ */
+
+#include <time.h>
+
+static int is_leap(unsigned y)
+{
+	y += 1900;
+	return (y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0);
+}
+
+time_t cgittimegm(struct tm *tm)
+{
+	static const unsigned ndays[2][12] ={
+		{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
+		{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
+	time_t res = 0;
+	unsigned i;
+
+	if (tm->tm_mon > 12 ||
+	    tm->tm_mon < 0 ||
+	    tm->tm_mday > 31 ||
+	    tm->tm_min > 60 ||
+	    tm->tm_sec > 60 ||
+	    tm->tm_hour > 24) {
+		/* invalid tm structure */
+		return 0;
+	}
+
+	for (i = 70; i < tm->tm_year; ++i)
+		res += is_leap(i) ? 366 : 365;
+
+	for (i = 0; i < tm->tm_mon; ++i)
+		res += ndays[is_leap(tm->tm_year)][i];
+	res += tm->tm_mday - 1;
+	res *= 24;
+	res += tm->tm_hour;
+	res *= 60;
+	res += tm->tm_min;
+	res *= 60;
+	res += tm->tm_sec;
+	return res;
+}
-- 
1.8.2.540.gf023cfe





  reply	other threads:[~2013-04-01 13:14 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-04-01 13:14 [PATCH 1/2] Makefile: re-include cgit.conf in cgit.mk john
2013-04-01 13:14 ` john [this message]
2013-04-01 17:47   ` [PATCH 2/2 v2] compat/timegm: new compat function for systems lacking timegm() john
2013-04-04 14:13     ` Jason
2013-04-04 14:46       ` john
2013-04-04 14:59         ` Jason
2013-04-04 17:28           ` john

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=75a384665737f5f9a1768641bc673c91bfa7ebca.1364821972.git.john@keeping.me.uk \
    --to=cgit@lists.zx2c4.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).