1 #pragma once
2 
3 //http://social.msdn.microsoft.com/Forums/en/vcgeneral/thread/430449b3-f6dd-4e18-84de-eebd26a8d668
4 
5 #include <time.h>
6 #include <winsock2.h>
7 
8 #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
9   #define DELTA_EPOCH_IN_MICROSECS  11644473600000000Ui64
10 #else
11   #define DELTA_EPOCH_IN_MICROSECS  11644473600000000ULL
12 #endif
13 
14 struct timespec {
15 
16 time_t tv_sec; /* Seconds since 00:00:00 GMT, */
17 
18 /* 1 January 1970 */
19 
20 long tv_nsec; /* Additional nanoseconds since */
21 
22 /* tv_sec */
23 
24 };
25 
26 struct timezone
27 {
28   int  tz_minuteswest; /* minutes W of Greenwich */
29   int  tz_dsttime;     /* type of dst correction */
30 };
31 
gettimeofday(struct timeval * tv,struct timezone * tz)32 static __inline int gettimeofday(struct timeval *tv, struct timezone *tz)
33 {
34   FILETIME ft;
35   unsigned __int64 tmpres = 0;
36   static int tzflag;
37 
38   if (NULL != tv)
39   {
40     GetSystemTimeAsFileTime(&ft);
41 
42     tmpres |= ft.dwHighDateTime;
43     tmpres <<= 32;
44     tmpres |= ft.dwLowDateTime;
45 
46     /*converting file time to unix epoch*/
47     tmpres -= DELTA_EPOCH_IN_MICROSECS;
48     tv->tv_sec = (long)(tmpres / 1000000UL);
49     tv->tv_usec = (long)(tmpres % 1000000UL);
50   }
51 
52   if (NULL != tz)
53   {
54     if (!tzflag)
55     {
56       _tzset();
57       tzflag++;
58     }
59     tz->tz_minuteswest = _timezone / 60;
60     tz->tz_dsttime = _daylight;
61   }
62 
63   return 0;
64 }