On 2026/09/09 14:52, LIU Hao wrote:
> 在 2026-9-9 14:49, Zhongteng Gui 写道:
>> This is indeed an issue. Previously I considered to provide
>> `mingw_gettimeofday`, and forward `gettimeofday` to it. But considering that
>> some packages may use autotools to detect `gettimeofday` (including
>> winpthreads), and autotools doesn't use header file, so I finally chose to
>> provide `gettimeofday` and forward `mingw_gettimeofday`.
>>
>> Maybe we could solve this by the following, and removes __MINGW_ASM_CALL.
>>
>> ```
>> // in gettimeofday.c
>> int gettimeofday(struct timeval* tv, struct timezone* tz) {
>>     return mingw_gettimeofday(tv, tz);
>> }
>>
>> int mingw_gettimeofday(struct timeval* tv, struct timezone* tz) {
>>     // Actual logic
>> }
>> ```
>>
>> If this is ok, I'll update this together with the above 2 fixes.
> looks fine; but the second parameter of `gettimeofday` should be `void*`.
> 

Sorry for the typo. Fixed altogether in v3 patches.
From 190a56d14e25c243a94cd15297858719841a03fe Mon Sep 17 00:00:00 2001
From: Zhongteng Gui <[email protected]>
Date: Fri, 4 Sep 2026 22:02:53 +0800
Subject: [PATCH v3 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.

Use reserved parameter name for mingw_gettimeofday.

Remove _TIMEZONE_DEFINED and _GETTIMEOFDAY_DEFINED.

Signed-off-by: Zhongteng Gui <[email protected]>
---
 mingw-w64-headers/crt/sys/time.h | 36 +++-----------------------------
 mingw-w64-headers/crt/time.h     |  5 +----
 2 files changed, 4 insertions(+), 37 deletions(-)

diff --git a/mingw-w64-headers/crt/sys/time.h b/mingw-w64-headers/crt/sys/time.h
index 9ef3fd3dd..56a093156 100644
--- a/mingw-w64-headers/crt/sys/time.h
+++ b/mingw-w64-headers/crt/sys/time.h
@@ -11,40 +11,10 @@
 
 _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) */);
-#endif
+/* TODO: POSIX.1-2008 marked it as obsolete, and POSIX.1-2024 removed it. */
+/* libiberty.h requires the second argument to be void* instead of struct 
timezone* */
+int __cdecl gettimeofday(struct timeval* __restrict__ _Tv, void* __restrict__ 
_Tz);
 
 _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..3d69e7c76 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 */
+int __cdecl mingw_gettimeofday(struct timeval* _Tv, struct timezone* _Tz);
 
 #if defined(_POSIX_C_SOURCE) && !defined(_POSIX_THREAD_SAFE_FUNCTIONS)
 #define _POSIX_THREAD_SAFE_FUNCTIONS 200112L
-- 
2.55.0.windows.5

From ba4a136e0ce276a6f271690c7f3fdf307040890c Mon Sep 17 00:00:00 2001
From: Zhongteng Gui <[email protected]>
Date: Fri, 4 Sep 2026 22:10:10 +0800
Subject: [PATCH v3 2/2] crt: gettimeofday.c: Optimize implemention

Forward gettimeofday to mingw_gettimeofday.

Merge getntptimeofday into gettimeofday and remove redundant conditions.

Signed-off-by: Zhongteng Gui <[email protected]>
---
 mingw-w64-crt/misc/gettimeofday.c | 60 ++++++++++---------------------
 1 file changed, 19 insertions(+), 41 deletions(-)

diff --git a/mingw-w64-crt/misc/gettimeofday.c 
b/mingw-w64-crt/misc/gettimeofday.c
index 968f3ed33..0e1c61b91 100644
--- a/mingw-w64-crt/misc/gettimeofday.c
+++ b/mingw-w64-crt/misc/gettimeofday.c
@@ -5,42 +5,37 @@
  */
 #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 mingw_gettimeofday(struct timeval* __restrict__ _Tv, struct 
timezone* __restrict__ _Tz)
 {
-  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)
+  if (_Tz != NULL)
     {
       if ((tzi = GetTimeZoneInformation(&TimeZoneInformation)) != 
TIME_ZONE_ID_INVALID) {
-       z->tz_minuteswest = TimeZoneInformation.Bias;
+       _Tz->tz_minuteswest = TimeZoneInformation.Bias;
        if (tzi == TIME_ZONE_ID_DAYLIGHT)
-         z->tz_dsttime = 1;
+         _Tz->tz_dsttime = 1;
        else
-         z->tz_dsttime = 0;
-      }
-    else
-      {
-       z->tz_minuteswest = 0;
-       z->tz_dsttime = 0;
+         _Tz->tz_dsttime = 0;
       }
+      else
+       {
+         _Tz->tz_minuteswest = 0;
+         _Tz->tz_dsttime = 0;
+       }
     }
 
-  if (tp != NULL) {
+  if (_Tv != NULL) {
     typedef void (WINAPI * GetSystemTimeAsFileTime_t)(LPFILETIME);
     static GetSystemTimeAsFileTime_t GetSystemTimeAsFileTime_p /* = 0 */;
 
@@ -59,30 +54,13 @@ int getntptimeofday (struct timespec *tp, struct timezone 
*z)
 
     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 */
+    _Tv->tv_sec = _now.ns100 / HECTONANOSEC_PER_SEC;   /* seconds since 
1-1-1970 */
+    _Tv->tv_usec = (long) (_now.ns100 % HECTONANOSEC_PER_SEC) / 10; /* 100ns 
-> 1us */
   }
-  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);
   return 0;
 }
+
+int __cdecl gettimeofday(struct timeval* __restrict__ _Tv, void* __restrict__ 
_Tz)
+{
+  return mingw_gettimeofday(_Tv, (struct timezone*)_Tz);
+}
-- 
2.55.0.windows.5

_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to