1 /*
2  * timeval.h    1.0 01/12/19
3  *
4  * Defines gettimeofday, timeval, etc. for Win32
5  *
6  * By Wu Yongwei
7  *
8  */
9 /* visited in Dec 2007*/
10 #ifndef _TIMEVAL_H
11 #define _TIMEVAL_H
12 #include <stdio.h>
13 #if TIME_WITH_SYS_TIME
14 #include <sys/time.h>
15 #include <time.h>
16 #elif HAVE_SYS_TIME_H
17 #include <sys/time.h>
18 #else
19 #include <time.h>
20 #endif
21 
22 #ifdef WINDOWS
23 #include <windows.h>
24 //#include <winsock2.h>
25 #else
26 #include <errno.h>
27 #endif
28 
29 #ifndef __GNUC__
30 #define EPOCHFILETIME (116444736000000000i64)
31 #else
32 #define EPOCHFILETIME (116444736000000000LL)
33 #endif
34 
35 struct timezone {
36   int tz_minuteswest; /* minutes W of Greenwich */
37   int tz_dsttime;     /* type of dst correction */
38 };
39 
40 /*!
41     \brief A Windows gettimeofday implementation.
42 */
43 
gettimeofday(struct timeval * tv,struct timezone * tz)44 static int gettimeofday(struct timeval *tv, struct timezone *tz)
45 {
46 #ifdef WINDOWS
47   FILETIME        ft;
48   LARGE_INTEGER   li;
49   __int64         t;
50   static int      tzflag;
51 
52   if (NULL != tv) {
53     GetSystemTimeAsFileTime(&ft);
54     li.LowPart  = ft.dwLowDateTime;
55     li.HighPart = ft.dwHighDateTime;
56     t  = li.QuadPart;       /* In 100-nanosecond intervals */
57     t -= EPOCHFILETIME;     /* Offset to the Epoch time */
58     t /= 10;                /* In microseconds */
59     printf("------------------- in gettimeofday() t=%li\n",t);
60     tv->tv_sec  = (long)(t / 1000000);
61     tv->tv_usec = (long)(t % 1000000);
62   }
63 
64   if (NULL != tz) {
65     if (!tzflag) {
66       _tzset();
67       tzflag++;
68     }
69     tz->tz_minuteswest = _timezone / 60;
70     tz->tz_dsttime = _daylight;
71   }
72   return 0;
73 #else
74   errno = ENOSYS;
75   printf("------------------- in does not swallow gettimeofday()\n");
76   return -1;
77 #endif
78 
79 }
80 #endif
81 
82