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 "msvc-posix.h" 7 8 #include <errno.h> 9 10 #define FILETIME_1970 \ 11 116444736000000000ull /* seconds between 1/1/1601 and 1/1/1970 */ 12 #define HECTONANOSEC_PER_SEC 10000000ull 13 14 15 // This is a poor resolution timer, but at least it 16 // is available on Win7 and older. System.cpp will install 17 // a better one. 18 static SystemTime getSystemTime = &GetSystemTimeAsFileTime; 19 20 int getntptimeofday(struct timespec*, struct timezone*); 21 getntptimeofday(struct timespec * tp,struct timezone * z)22int getntptimeofday(struct timespec* tp, struct timezone* z) { 23 int res = 0; 24 union { 25 unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */ 26 FILETIME ft; 27 } _now; 28 TIME_ZONE_INFORMATION TimeZoneInformation; 29 DWORD tzi; 30 31 if (z != NULL) { 32 if ((tzi = GetTimeZoneInformation(&TimeZoneInformation)) != 33 TIME_ZONE_ID_INVALID) { 34 z->tz_minuteswest = TimeZoneInformation.Bias; 35 if (tzi == TIME_ZONE_ID_DAYLIGHT) 36 z->tz_dsttime = 1; 37 else 38 z->tz_dsttime = 0; 39 } else { 40 z->tz_minuteswest = 0; 41 z->tz_dsttime = 0; 42 } 43 } 44 45 if (tp != NULL) { 46 getSystemTime(&_now.ft); /* 100-nanoseconds since 1-1-1601 */ 47 /* The actual accuracy on XP seems to be 125,000 nanoseconds = 125 48 * microseconds = 0.125 milliseconds */ 49 _now.ns100 -= FILETIME_1970; /* 100 nano-seconds since 1-1-1970 */ 50 tp->tv_sec = 51 _now.ns100 / HECTONANOSEC_PER_SEC; /* seconds since 1-1-1970 */ 52 tp->tv_nsec = (long)(_now.ns100 % HECTONANOSEC_PER_SEC) * 53 100; /* nanoseconds */ 54 } 55 return res; 56 } 57 gettimeofday(struct timeval * p,struct timezone * z)58int gettimeofday(struct timeval* p, struct timezone* z) { 59 struct timespec tp; 60 61 if (getntptimeofday(&tp, z)) 62 return -1; 63 p->tv_sec = tp.tv_sec; 64 p->tv_usec = (tp.tv_nsec / 1000); 65 return 0; 66 } 67