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

[PATCH v2 7/7] Do not use date.c:tm_to_time_t() from compat/mingw.c

From
Johannes Sixt <j6t@kdbg.org>
Date
Jan 15, 2010, 20:12 UTC
Message-ID
<8878e74e4071102dd7c7830d549ed28c5cc1b266.1263584975.git.j6t@kdbg.org>
In-Reply-To
<cover.1262895936.git.j6t@kdbg.org>

To implement gettimeofday(), a broken-down UTC time was requested from the system using GetSystemTime(), then tm_to_time_t() was used to convert it to a time_t because it does not look at the current timezone, which mktime() would do.

Use GetSystemTimeAsFileTime() and a different conversion path to avoid this back-reference from the compatibility layer to the generic code.

Signed-off-by: Johannes Sixt <j6t@kdbg.org>
---
 This is new. It was triggered by Junio's "mark function static" series.
 The patch that I sent out in response there was too simple to be correct.
 compat/mingw.c |   36 +++++++++++++++++++-----------------
 1 files changed, 19 insertions(+), 17 deletions(-)
diff --git a/compat/mingw.c b/compat/mingw.c
index 74ffc18..ab65f77 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -140,12 +140,20 @@ int mingw_open (const char *filename, int oflags, ...)
 	return fd;
 }
 
-static inline time_t filetime_to_time_t(const FILETIME *ft)
+/*
+ * The unit of FILETIME is 100-nanoseconds since January 1, 1601, UTC.
+ * Returns the 100-nanoseconds ("hekto nanoseconds") since the epoch.
+ */
+static inline long long filetime_to_hnsec(const FILETIME *ft)
 {
 	long long winTime = ((long long)ft->dwHighDateTime << 32) + ft->dwLowDateTime;
-	winTime -= 116444736000000000LL; /* Windows to Unix Epoch conversion */
-	winTime /= 10000000;		 /* Nano to seconds resolution */
-	return (time_t)winTime;
+	/* Windows to Unix Epoch conversion */
+	return winTime - 116444736000000000LL;
+}
+
+static inline time_t filetime_to_time_t(const FILETIME *ft)
+{
+	return (time_t)(filetime_to_hnsec(ft) / 10000000);
 }
 
 /* We keep the do_lstat code in a separate function to avoid recursion.
@@ -281,19 +289,13 @@ int mkstemp(char *template)
 
 int gettimeofday(struct timeval *tv, void *tz)
 {
-	SYSTEMTIME st;
-	struct tm tm;
-	GetSystemTime(&st);
-	tm.tm_year = st.wYear-1900;
-	tm.tm_mon = st.wMonth-1;
-	tm.tm_mday = st.wDay;
-	tm.tm_hour = st.wHour;
-	tm.tm_min = st.wMinute;
-	tm.tm_sec = st.wSecond;
-	tv->tv_sec = tm_to_time_t(&tm);
-	if (tv->tv_sec < 0)
-		return -1;
-	tv->tv_usec = st.wMilliseconds*1000;
+	FILETIME ft;
+	long long hnsec;
+
+	GetSystemTimeAsFileTime(&ft);
+	hnsec = filetime_to_hnsec(&ft);
+	tv->tv_sec = hnsec / 10000000;
+	tv->tv_usec = (hnsec % 10000000) / 10;
 	return 0;
 }
 
-- 
1.6.6.218.g3e6eb
Previous: Johannes Sixt
Message 24 of 24 in “Miscellaneous improvements on Windows”
  1. 0/5 Miscellaneous improvements on WindowsJohannes Sixt, Jan 7, 2010
  2. 1/5 MSVC: Windows-native implementation for subset of Pthreads APIJohannes Sixt, Jan 7, 2010
  3. Dmitry PotapovJan 8, 2010
  4. Erik Faye-LundJan 8, 2010
  5. Johannes SixtJan 8, 2010
  6. Dmitry PotapovJan 8, 2010
  7. Johannes SixtJan 12, 2010
  8. Dmitry PotapovJan 13, 2010
  9. Johannes SixtJan 13, 2010
  10. Dmitry PotapovJan 14, 2010
  11. Peter HarrisJan 14, 2010
  12. Johannes SixtJan 14, 2010
  13. 2/5 MinGW: enable pthreadsJohannes Sixt, Jan 7, 2010
  14. 3/5 Windows: boost startup by avoiding a static dependency on shell32.dllJohannes Sixt, Jan 7, 2010
  15. 4/5 Windows: simplify the pipe(2) implementationJohannes Sixt, Jan 7, 2010
  16. 5/5 Windows: avoid the "dup dance" when spawning a child processJohannes Sixt, Jan 7, 2010
  17. 0/7 Miscellaneous improvements on WindowsJohannes Sixt, Jan 15, 2010
  18. 1/7 Windows: disable PythonJohannes Sixt, Jan 15, 2010
  19. 2/7 Windows: boost startup by avoiding a static dependency on shell32.dllJohannes Sixt, Jan 15, 2010
  20. 3/7 Windows: simplify the pipe(2) implementationJohannes Sixt, Jan 15, 2010
  21. 4/7 Windows: avoid the "dup dance" when spawning a child processJohannes Sixt, Jan 15, 2010
  22. 5/7 MSVC: Fix an "incompatible pointer types" compiler warningJohannes Sixt, Jan 15, 2010
  23. 6/7 MSVC: Windows-native implementation for subset of Pthreads APIJohannes Sixt, Jan 15, 2010
  24. 7/7 Do not use date.c:tm_to_time_t() from compat/mingw.cJohannes Sixt, Jan 15, 2010

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.