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

Re: Date handling.

From
David Woodhouse <dwmw2@infradead.org>
Date
Apr 24, 2005, 06:38 UTC
Message-ID
<1114324729.3419.78.camel@localhost.localdomain>
In-Reply-To
<20050424030416.GE16751@delft.aura.cs.cmu.edu>
On Sat, 2005-04-23 at 23:04 -0400, Jan Harkes wrote:
> I noticed that some commit timestamps seemed to be off, looking into it
> a bit more it seems like mktime is influenced by the setting of the
> local TZ environment.

Ewww. I missed that in the documentation. I suppose I should have worked it out having empirically determined that it ignores the tm_gmtoff field.

> The question is, do we want to just calculate the time_t offset
> ourselves without using mktime, or force the TZ environment to UTC.

I don't think we want to be in the business of counting leap seconds; we need to let the system do it. I don't much like setting TZ to UTC though -- how about we use your test case to find the offset and subtract that?

Does this work?
Index: commit-tree.c
===================================================================
--- 31e9af73983d640090508b06784ef7db4816c957/commit-tree.c  (mode:100644 sha1:c0b07f89286c3f6cceae8122b4c3142c8efaf8e1)
+++ uncommitted/commit-tree.c  (mode:100664)
@@ -138,10 +138,14 @@
 	struct tm tm;
 	char *p;
 	int i, offset;
-	time_t then;
+	time_t then, localofs;
 
 	memset(&tm, 0, sizeof(tm));
 
+	tm.tm_mday = 1;
+	tm.tm_year = 70;
+	localofs = mktime(&tm);
+
 	/* Skip day-name */
 	p = skipfws(date);
 	if (!isdigit(*p)) {
@@ -246,7 +250,9 @@
 	if (*(skipfws(p + 5)))
 		return;
 
-	then = mktime(&tm); /* mktime appears to ignore the GMT offset, stupidly */
+	/* No way to convert to a time_t and honour tm_gmtoff; we have to
+	   do the evil trick by subtracting the local offset */
+	then = mktime(&tm) - localofs;
 	if (then == -1)
 		return;
 
-- 
dwmw2
Previous: James PurserNext: Russ Allbery
Message 9 of 13 in “Date handling.”
  1. David WoodhouseApr 14, 2005
  2. Linus TorvaldsApr 14, 2005
  3. Linus TorvaldsApr 14, 2005
  4. David WoodhouseApr 14, 2005
  5. tony.luck@intel.comApr 14, 2005
  6. David WoodhouseApr 14, 2005
  7. Jan HarkesApr 24, 2005
  8. James PurserApr 24, 2005
  9. David WoodhouseApr 24, 2005
  10. Russ AllberyApr 24, 2005
  11. Jan HarkesApr 25, 2005
  12. Russ AllberyApr 25, 2005
  13. David WoodhouseApr 14, 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.