GEOS  3.8.0dev
timeval.h
1 /**********************************************************************
2  *
3  * GEOS - Geometry Engine Open Source
4  * http://geos.osgeo.org
5  *
6  * Copyright (C) 2006 Wu Yongwei
7  *
8  * This is free software; you can redistribute and/or modify it under
9  * the terms of the GNU Lesser General Public Licence as published
10  * by the Free Software Foundation.
11  * See the COPYING file for more information.
12  *
13  * Note: This code is in the public domain, see
14  * http://wyw.dcweb.cn/time.htm
15  *
16  **********************************************************************/
17 
18 #ifndef GEOS_TIMEVAL_H
19 #define GEOS_TIMEVAL_H
20 
21 #if !defined(_WIN32)
22 #error This header is dedicated to Windows platform only
23 #endif
24 
25 #ifndef WIN32_LEAN_AND_MEAN
26 #define WIN32_LEAN_AND_MEAN
27 #endif
28 
29 #ifndef NOMINMAX
30 #define NOMINMAX
31 #endif
32 
33 #ifndef STRICT
34 #define STRICT
35 #endif
36 
37 #include <winsock2.h>
38 #include <time.h>
39 
40 #if defined(_MSC_VER) || defined(__BORLANDC__)
41 #define EPOCHFILETIME (116444736000000000i64)
42 #else
43 #define EPOCHFILETIME (116444736000000000LL)
44 #endif
45 
46 struct timezone {
47  int tz_minuteswest; /* minutes W of Greenwich */
48  int tz_dsttime; /* type of dst correction */
49 };
50 
51 
52 #if !defined(_WIN32_WCE)
53 
54 __inline int
55 gettimeofday(struct timeval* tv, struct timezone* tz)
56 {
57  FILETIME ft;
58  LARGE_INTEGER li;
59  __int64 t;
60  static int tzflag;
61 
62  if(tv) {
63  GetSystemTimeAsFileTime(&ft);
64  li.LowPart = ft.dwLowDateTime;
65  li.HighPart = ft.dwHighDateTime;
66  t = li.QuadPart; /* In 100-nanosecond intervals */
67  t -= EPOCHFILETIME; /* Offset to the Epoch time */
68  t /= 10; /* In microseconds */
69  tv->tv_sec = (long)(t / 1000000);
70  tv->tv_usec = (long)(t % 1000000);
71  }
72 
73  if(tz) {
74  if(!tzflag) {
75  _tzset();
76  tzflag++;
77  }
78  tz->tz_minuteswest = _timezone / 60;
79  tz->tz_dsttime = _daylight;
80  }
81 
82  return 0;
83 }
84 
85 #else
86 
87 __inline int
88 gettimeofday(struct timeval* tv, struct timezone* tz)
89 {
90  SYSTEMTIME st;
91  FILETIME ft;
92  LARGE_INTEGER li;
93  TIME_ZONE_INFORMATION tzi;
94  __int64 t;
95  static int tzflag;
96 
97  if(tv) {
98  GetSystemTime(&st);
99  SystemTimeToFileTime(&st, &ft);
100  li.LowPart = ft.dwLowDateTime;
101  li.HighPart = ft.dwHighDateTime;
102  t = li.QuadPart; /* In 100-nanosecond intervals */
103  t -= EPOCHFILETIME; /* Offset to the Epoch time */
104  t /= 10; /* In microseconds */
105  tv->tv_sec = (long)(t / 1000000);
106  tv->tv_usec = (long)(t % 1000000);
107  }
108 
109  if(tz) {
110  GetTimeZoneInformation(&tzi);
111 
112  tz->tz_minuteswest = tzi.Bias;
113  if(tzi.StandardDate.wMonth != 0) {
114  tz->tz_minuteswest += tzi.StandardBias * 60;
115  }
116 
117  if(tzi.DaylightDate.wMonth != 0) {
118  tz->tz_dsttime = 1;
119  }
120  else {
121  tz->tz_dsttime = 0;
122  }
123  }
124 
125  return 0;
126 }
127 
128 #endif /* _WIN32_WCE */
129 
130 #endif /* GEOS_TIMEVAL_H */