git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 3/4] add xmktime() function that always accounts for the TZ env

From
EWEric Wong <normalperson@yhbt.net>
Date
Dec 24, 2005, 12:13 UTC
Message-ID
<20051224121339.GB3963@mail.yhbt.net>
In-Reply-To
<20051224121007.GA19136@mail.yhbt.net>

This function was added because mktime in dietlibc doesn't seem to account for the TZ env. Also, xmktime() now shares the same always-summer bug TZ parsing elsewhere, so at least we can be wrong about summer consistently.

Signed-off-by: Eric Wong <normalperson@yhbt.net>
---
 cache.h           |    2 ++
 convert-objects.c |    4 ++--
 date.c            |   30 ++++++++++++++++++++++++++----
 3 files changed, 30 insertions(+), 6 deletions(-)
3b0763ae6fce6c69021e1216660f4c0ee301512b
diff --git a/cache.h b/cache.h
index cb87bec..c5ff4b7 100644
--- a/cache.h
+++ b/cache.h
@@ -5,6 +5,7 @@
 
 #include SHA1_HEADER
 #include <zlib.h>
+#include <time.h>
 
 #if ZLIB_VERNUM < 0x1200
 #define deflateBound(c,s)  ((s) + (((s) + 7) >> 3) + (((s) + 63) >> 6) + 11)
@@ -236,6 +237,7 @@ extern void *read_object_with_reference(
 
 const char *show_date(unsigned long time, int timezone);
 int parse_date(const char *date, char *buf, int bufsize);
+time_t xmktime(struct tm *tm);
 void datestamp(char *buf, int bufsize);
 unsigned long approxidate(const char *);
 
diff --git a/convert-objects.c b/convert-objects.c
index b49bce2..0fe1229 100644
--- a/convert-objects.c
+++ b/convert-objects.c
@@ -178,7 +178,7 @@ static unsigned long parse_oldstyle_date
 		const char *next = strptime(buf, *fmt, &tm);
 		if (next) {
 			if (!*next)
-				return mktime(&tm);
+				return xmktime(&tm);
 			buf = next;
 		} else {
 			const char **p = timezones;
@@ -195,7 +195,7 @@ static unsigned long parse_oldstyle_date
 		fmt++;
 	} while (*buf && *fmt);
 	printf("left: %s\n", buf);
-	return mktime(&tm);				
+	return xmktime(&tm);				
 }
 
 static int convert_date_line(char *dst, void **buf, unsigned long *sp)
diff --git a/date.c b/date.c
index 3e11500..5596476 100644
--- a/date.c
+++ b/date.c
@@ -141,6 +141,28 @@ static int match_string(const char *date
 	return i;
 }
 
+time_t xmktime(struct tm *tm)
+{
+	time_t ret = my_mktime(tm);
+	char * tz = getenv("TZ");
+
+	if (tz) {
+		int i;
+		for (i = 0; i < NR_TZ; i++) {
+			int match = match_string(tz, timezone_names[i].name);
+			if (match >= 3) {
+				int off = timezone_names[i].offset;
+
+				/* This is bogus, but we like summer */
+				off += timezone_names[i].dst;
+				
+				ret += 60*off;
+			}
+		}
+	}
+	return ret;
+}
+
 static int skip_alpha(const char *date)
 {
 	int i = 0;
@@ -436,10 +458,10 @@ int parse_date(const char *date, char *r
 		date += match;
 	}
 
-	/* mktime uses local timezone */
+	/* (x)mktime uses local timezone */
 	then = my_mktime(&tm); 
 	if (offset == -1)
-		offset = (then - mktime(&tm)) / 60;
+		offset = (then - xmktime(&tm)) / 60;
 
 	if (then == -1)
 		return -1;
@@ -464,7 +486,7 @@ void datestamp(char *buf, int bufsize)
 
 static void update_tm(struct tm *tm, unsigned long sec)
 {
-	time_t n = mktime(tm) - sec;
+	time_t n = xmktime(tm) - sec;
 	localtime_r(&n, tm);
 }
 
@@ -642,5 +664,5 @@ unsigned long approxidate(const char *da
 		tm.tm_mday = number;
 	if (tm.tm_mon > now.tm_mon)
 		tm.tm_year--;
-	return mktime(&tm);
+	return xmktime(&tm);
 }
-- 
1.0.GIT
Previous: H. Peter AnvinNext: Johannes Schindelin
Message 10 of 24 in “dietlibc compatibility”
  1. 0/4 dietlibc compatibilityEric Wong, Dec 24, 2005
  2. 1/4 git.c: extra #include for dietlibc (and possibly other C libraries)Eric Wong, Dec 24, 2005
  3. Junio C HamanoDec 26, 2005
  4. 2/4 short circuit out of a few places where we would allocate zero bytesEric Wong, Dec 24, 2005
  5. Johannes SchindelinDec 24, 2005
  6. H. Peter AnvinDec 28, 2005
  7. Linus TorvaldsDec 28, 2005
  8. Junio C HamanoDec 28, 2005
  9. H. Peter AnvinDec 28, 2005
  10. 3/4 add xmktime() function that always accounts for the TZ envEric Wong, Dec 24, 2005
  11. Johannes SchindelinDec 24, 2005
  12. Junio C HamanoDec 24, 2005
  13. Eric WongDec 24, 2005
  14. Junio C HamanoDec 24, 2005
  15. 4/4 git-compat-util.h: dietlibc-friendly x{malloc,realloc,calloc}Eric Wong, Dec 24, 2005
  16. Junio C HamanoDec 24, 2005
  17. Eric WongDec 24, 2005
  18. Avoid allocating 0 bytes, was Re: [PATCH 4/4] git-compat-util.h: dietlibc-friendly x{malloc,realloc,calloc}Johannes Schindelin, Dec 26, 2005
  19. Junio C HamanoDec 26, 2005
  20. Johannes SchindelinDec 26, 2005
  21. avoid asking ?alloc() for zero bytes.Junio C Hamano, Dec 26, 2005
  22. Johannes SchindelinDec 26, 2005
  23. Johannes SchindelinDec 28, 2005
  24. Eric WongDec 30, 2005

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.