• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * This file has no copyright assigned and is placed in the Public Domain.
3  * This file is part of the mingw-w64 runtime package.
4  * No warranty is given; refer to the file DISCLAIMER.PD within this package.
5  */
6 #include <time.h>
7 #include <sys/time.h>
8 #include <sys/timeb.h>
9 #include <errno.h>
10 #include <windows.h>
11 
12 #define FILETIME_1970 116444736000000000ull /* seconds between 1/1/1601 and 1/1/1970 */
13 #define HECTONANOSEC_PER_SEC 10000000ull
14 
15 int getntptimeofday (struct timespec *, struct timezone *);
16 
getntptimeofday(struct timespec * tp,struct timezone * z)17 int getntptimeofday (struct timespec *tp, struct timezone *z)
18 {
19   int res = 0;
20   union {
21     unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */
22     FILETIME ft;
23   }  _now;
24   TIME_ZONE_INFORMATION  TimeZoneInformation;
25   DWORD tzi;
26 
27   if (z != NULL)
28     {
29       if ((tzi = GetTimeZoneInformation(&TimeZoneInformation)) != TIME_ZONE_ID_INVALID) {
30 	z->tz_minuteswest = TimeZoneInformation.Bias;
31 	if (tzi == TIME_ZONE_ID_DAYLIGHT)
32 	  z->tz_dsttime = 1;
33 	else
34 	  z->tz_dsttime = 0;
35       }
36     else
37       {
38 	z->tz_minuteswest = 0;
39 	z->tz_dsttime = 0;
40       }
41     }
42 
43   if (tp != NULL) {
44     GetSystemTimeAsFileTime (&_now.ft);	 /* 100-nanoseconds since 1-1-1601 */
45     /* The actual accuracy on XP seems to be 125,000 nanoseconds = 125 microseconds = 0.125 milliseconds */
46     _now.ns100 -= FILETIME_1970;	/* 100 nano-seconds since 1-1-1970 */
47     tp->tv_sec = _now.ns100 / HECTONANOSEC_PER_SEC;	/* seconds since 1-1-1970 */
48     tp->tv_nsec = (long) (_now.ns100 % HECTONANOSEC_PER_SEC) * 100; /* nanoseconds */
49   }
50   return res;
51 }
52 
gettimeofday(struct timeval * p,void * z)53 int __cdecl gettimeofday (struct timeval *p, void *z)
54 {
55  struct timespec tp;
56 
57  if (getntptimeofday (&tp, (struct timezone *) z))
58    return -1;
59  p->tv_sec=tp.tv_sec;
60  p->tv_usec=(tp.tv_nsec/1000);
61  return 0;
62 }
63 
mingw_gettimeofday(struct timeval * p,struct timezone * z)64 int __cdecl mingw_gettimeofday (struct timeval *p, struct timezone *z)
65 {
66   struct timespec tp;
67 
68   if (getntptimeofday (&tp, z))
69     return -1;
70   p->tv_sec=tp.tv_sec;
71   p->tv_usec=(tp.tv_nsec/1000);
72   return 0;
73 }
74