crt: gettimeofday.c: Optimize implemention

Forward gettimeofday to mingw_gettimeofday.

Merge getntptimeofday into gettimeofday and remove redundant conditions.

Signed-off-by: Zhongteng Gui <dragon-archer@outlook.com>
Signed-off-by: LIU Hao <lh_mouse@126.com>
diff --git a/mingw-w64-crt/misc/gettimeofday.c b/mingw-w64-crt/misc/gettimeofday.c
index 968f3ed..0e1c61b 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;
+	  _Tz->tz_dsttime = 0;
       }
-    else
-      {
-	z->tz_minuteswest = 0;
-	z->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 @@
 
     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);
+}