NeoPZ
pz_gettime.h
Go to the documentation of this file.
1 #ifndef PZ_GETTIME_H
2 #define PZ_GETTIME_H
3 
4 #include <pz_config.h>
5 
6 #ifdef VC
7  //We need to implement gettimeofday function on windows environment.
8  #include <time.h>
9  #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
10  #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
11  #else
12  #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
13  #endif
14 
15  struct timezone
16  {
17  int tz_minuteswest; /* minutes W of Greenwich */
18  int tz_dsttime; /* type of dst correction */
19  };
20 
21  inline int gettimeofday(struct timeval *tv, struct timezone *tz)
22  {
23  FILETIME ft;
24  unsigned __int64 tmpres = 0;
25  static int tzflag;
26 
27  if (NULL != tv)
28  {
29  GetSystemTimeAsFileTime(&ft);
30 
31  tmpres |= ft.dwHighDateTime;
32  tmpres <<= 32;
33  tmpres |= ft.dwLowDateTime;
34 
35  /*converting file time to unix epoch*/
36  tmpres -= DELTA_EPOCH_IN_MICROSECS;
37  tmpres /= 10; /*convert into microseconds*/
38  tv->tv_sec = (int64_t)(tmpres / 1000000UL);
39  tv->tv_usec = (int64_t)(tmpres % 1000000UL);
40  }
41 
42  if (NULL != tz)
43  {
44  if (!tzflag)
45  {
46  _tzset();
47  tzflag++;
48  }
49  tz->tz_minuteswest = _timezone / 60;
50  tz->tz_dsttime = _daylight;
51  }
52 
53  return 0;
54  }
55 #else // VC
56 
57 #include<sys/time.h>
58 
59 #endif // VC
60 
61 #endif // PZ_GETTIME_H