Hi, here're 2 patches about cleanup and optimize gettimeofday (and related
stuff).
This change is a bit huge, and I'm not very sure about some parts, e.g.
1. Originally, we provide `int gettimeofday(struct timeval*, void*)`, while
POSIX expects `int gettimeofday(struct timeval*, struct timezone*)`. It's
noteworthy that switching to POSIX will breaks GCC without special handling.
because libiberty.h bundled by GCC specially checked for __MINGW32__, and
declares gettimeofday with `void*` arg type, which fails with
-Wstrict-prototypes. But keeping `void*` is semantically wrong, as we actually
set timezone if nonnull.
2. Should we conditionally expose gettimeofday? It is marked as obsolete in
POSIX.1-2008 and removed in POSIX.1-2024.
3. Originally, gettimeofday assumed "struct timeval*" to be nonnull, and didn't
check this. Should we keep this as is, or check it and return -1 if NULL?
From a079f2eed19364267f8d541865082b4956540fa6 Mon Sep 17 00:00:00 2001
From: Zhongteng Gui <[email protected]>
Date: Fri, 4 Sep 2026 22:02:53 +0800
Subject: [PATCH 1/2] headers: crt: Clean up time.h and sys/time.h
sys/time.h always include time.h, so no need to define timezone and
mingw_gettimeofday itself.
Adjust prototype of gettimeofday following POSIX. Note that we may add
additional conditions for it later, as it's removed in POSIX.1-2024.
Use reserved parameter name for mingw_gettimeofday, and making it an
alias to gettimeofday as they are actually identical in misc/gettimeofday.c
Remove _TIMEZONE_DEFINED and _GETTIMEOFDAY_DEFINED.
Signed-off-by: Zhongteng Gui <[email protected]>
---
mingw-w64-headers/crt/sys/time.h | 37 +++++---------------------------
mingw-w64-headers/crt/time.h | 5 +----
2 files changed, 6 insertions(+), 36 deletions(-)
diff --git a/mingw-w64-headers/crt/sys/time.h b/mingw-w64-headers/crt/sys/time.h
index 9ef3fd3dd..bcad16c4e 100644
--- a/mingw-w64-headers/crt/sys/time.h
+++ b/mingw-w64-headers/crt/sys/time.h
@@ -11,40 +11,13 @@
_CRT_BEGIN_C_HEADER
-#include <_timeval.h>
-
-#ifndef _TIMEZONE_DEFINED /* also in sys/time.h */
-#define _TIMEZONE_DEFINED
-/* Provided for compatibility with code that assumes that
- the presence of gettimeofday function implies a definition
- of struct timezone. */
-struct timezone
-{
- int tz_minuteswest; /* of Greenwich */
- int tz_dsttime; /* type of dst correction to apply */
-};
-
- extern int __cdecl mingw_gettimeofday (struct timeval *p, struct timezone
*z);
-
-#endif /* _TIMEZONE_DEFINED */
-
-/*
- Implementation as per:
- The Open Group Base Specifications, Issue 6
- IEEE Std 1003.1, 2004 Edition
-
- The timezone pointer arg is ignored. Errors are ignored.
-*/
-#ifndef _GETTIMEOFDAY_DEFINED
-#define _GETTIMEOFDAY_DEFINED
-int __cdecl gettimeofday(struct timeval *__restrict__,
- void *__restrict__ /* tzp (unused) */);
+/* TODO: POSIX.1-2008 marked it as obsolete, and POSIX.1-2024 removed it. */
+/* libiberty.h provides "extern int gettimeofday (struct timeval *, void *);"
if __MINGW32__ */
+/* which fails to compile if -Wstrict-prototypes */
+#ifndef LIBIBERTY_H
+int __cdecl gettimeofday(struct timeval *__restrict__ _tv, struct timezone
*__restrict__ _tz);
#endif
_CRT_END_C_HEADER
-/* Adding timespec definition. */
-#include <sys/timeb.h>
-
-
#endif /* _SYS_TIME_H_ */
diff --git a/mingw-w64-headers/crt/time.h b/mingw-w64-headers/crt/time.h
index 2a8e39223..044ecc020 100644
--- a/mingw-w64-headers/crt/time.h
+++ b/mingw-w64-headers/crt/time.h
@@ -163,15 +163,12 @@ time_t __CRTDECL _mkgmtime(struct tm *_Tm)
__MINGW_ASM_CALL(_mkgmtime64);
#include <_timeval.h>
-#ifndef _TIMEZONE_DEFINED /* also in sys/time.h */
-#define _TIMEZONE_DEFINED
struct timezone {
int tz_minuteswest;
int tz_dsttime;
};
- extern int __cdecl mingw_gettimeofday (struct timeval *p, struct timezone
*z);
-#endif /* _TIMEZONE_DEFINED */
+extern int __cdecl mingw_gettimeofday(struct timeval *_tp, struct timezone
*_tz) __MINGW_ASM_CALL(gettimeofday);
#if defined(_POSIX_C_SOURCE) && !defined(_POSIX_THREAD_SAFE_FUNCTIONS)
#define _POSIX_THREAD_SAFE_FUNCTIONS 200112L
--
2.55.0.windows.5
From e3e3172ad90e0d7480f6352d9e5188666fed98f7 Mon Sep 17 00:00:00 2001
From: Zhongteng Gui <[email protected]>
Date: Fri, 4 Sep 2026 22:10:10 +0800
Subject: [PATCH 2/2] crt: gettimeofday.c: Optimize implemention
Remove mingw_gettimeofday, as it's now an alias to gettimeofday.
Merge getntptimeofday into gettimeofday and remove redundant conditions.
Signed-off-by: Zhongteng Gui <[email protected]>
---
mingw-w64-crt/misc/gettimeofday.c | 77 ++++++++++---------------------
1 file changed, 24 insertions(+), 53 deletions(-)
diff --git a/mingw-w64-crt/misc/gettimeofday.c
b/mingw-w64-crt/misc/gettimeofday.c
index 968f3ed33..af63ec018 100644
--- a/mingw-w64-crt/misc/gettimeofday.c
+++ b/mingw-w64-crt/misc/gettimeofday.c
@@ -5,23 +5,18 @@
*/
#include <time.h>
#include <sys/time.h>
-#include <sys/timeb.h>
-#include <errno.h>
#include <windows.h>
#define FILETIME_1970 116444736000000000ull /* seconds between 1/1/1601 and
1/1/1970 */
#define HECTONANOSEC_PER_SEC 10000000ull
-int getntptimeofday (struct timespec *, struct timezone *);
-
-int getntptimeofday (struct timespec *tp, struct timezone *z)
+int __cdecl gettimeofday (struct timeval *__restrict__ p, struct timezone
*__restrict__ z)
{
- int res = 0;
union {
unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */
FILETIME ft;
} _now;
- TIME_ZONE_INFORMATION TimeZoneInformation;
+ TIME_ZONE_INFORMATION TimeZoneInformation;
DWORD tzi;
if (z != NULL)
@@ -33,56 +28,32 @@ int getntptimeofday (struct timespec *tp, struct timezone
*z)
else
z->tz_dsttime = 0;
}
- else
- {
- z->tz_minuteswest = 0;
- z->tz_dsttime = 0;
- }
+ else
+ {
+ z->tz_minuteswest = 0;
+ z->tz_dsttime = 0;
+ }
}
- if (tp != NULL) {
- typedef void (WINAPI * GetSystemTimeAsFileTime_t)(LPFILETIME);
- static GetSystemTimeAsFileTime_t GetSystemTimeAsFileTime_p /* = 0 */;
+ typedef void (WINAPI * GetSystemTimeAsFileTime_t)(LPFILETIME);
+ static GetSystemTimeAsFileTime_t GetSystemTimeAsFileTime_p /* = 0 */;
- /* Set function pointer during first call */
- GetSystemTimeAsFileTime_t get_time =
- __atomic_load_n (&GetSystemTimeAsFileTime_p, __ATOMIC_RELAXED);
- if (get_time == NULL) {
- /* Use GetSystemTimePreciseAsFileTime() if available (Windows 8 or
later) */
- get_time = (GetSystemTimeAsFileTime_t)(intptr_t) GetProcAddress (
- GetModuleHandle ("kernel32.dll"),
- "GetSystemTimePreciseAsFileTime"); /* <1us precision on Windows 10 */
- if (get_time == NULL)
- get_time = GetSystemTimeAsFileTime; /* >15ms precision on Windows 10 */
- __atomic_store_n (&GetSystemTimeAsFileTime_p, get_time,
__ATOMIC_RELAXED);
- }
-
- get_time (&_now.ft); /* 100 nano-seconds since 1-1-1601 */
- _now.ns100 -= FILETIME_1970; /* 100 nano-seconds since 1-1-1970 */
- tp->tv_sec = _now.ns100 / HECTONANOSEC_PER_SEC; /* seconds since
1-1-1970 */
- tp->tv_nsec = (long) (_now.ns100 % HECTONANOSEC_PER_SEC) * 100; /*
nanoseconds */
+ /* Set function pointer during first call */
+ GetSystemTimeAsFileTime_t get_time =
+ __atomic_load_n (&GetSystemTimeAsFileTime_p, __ATOMIC_RELAXED);
+ if (get_time == NULL) {
+ /* Use GetSystemTimePreciseAsFileTime() if available (Windows 8 or later)
*/
+ get_time = (GetSystemTimeAsFileTime_t)(intptr_t) GetProcAddress (
+ GetModuleHandle ("kernel32.dll"),
+ "GetSystemTimePreciseAsFileTime"); /* <1us precision on Windows 10 */
+ if (get_time == NULL)
+ get_time = GetSystemTimeAsFileTime; /* >15ms precision on Windows 10 */
+ __atomic_store_n (&GetSystemTimeAsFileTime_p, get_time, __ATOMIC_RELAXED);
}
- return res;
-}
-int __cdecl gettimeofday (struct timeval *p, void *z)
-{
- struct timespec tp;
-
- if (getntptimeofday (&tp, (struct timezone *) z))
- return -1;
- p->tv_sec=tp.tv_sec;
- p->tv_usec=(tp.tv_nsec/1000);
- return 0;
-}
-
-int __cdecl mingw_gettimeofday (struct timeval *p, struct timezone *z)
-{
- struct timespec tp;
-
- if (getntptimeofday (&tp, z))
- return -1;
- p->tv_sec=tp.tv_sec;
- p->tv_usec=(tp.tv_nsec/1000);
+ get_time (&_now.ft); /* 100 nano-seconds since 1-1-1601 */
+ _now.ns100 -= FILETIME_1970; /* 100 nano-seconds since 1-1-1970 */
+ p->tv_sec = _now.ns100 / HECTONANOSEC_PER_SEC; /* seconds since
1-1-1970 */
+ p->tv_usec = (long) (_now.ns100 % HECTONANOSEC_PER_SEC) / 10; /* 100ns ->
1us */
return 0;
}
--
2.55.0.windows.5
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public