GEOS 3.6.2
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
46struct 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 gettimeofday(struct timeval *tv, struct timezone *tz)
55{
56 FILETIME ft;
57 LARGE_INTEGER li;
58 __int64 t;
59 static int tzflag;
60
61 if (tv)
62 {
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 {
75 if (!tzflag)
76 {
77 _tzset();
78 tzflag++;
79 }
80 tz->tz_minuteswest = _timezone / 60;
81 tz->tz_dsttime = _daylight;
82 }
83
84 return 0;
85}
86
87#else
88
89__inline int gettimeofday(struct timeval *tv, struct timezone *tz)
90{
91 SYSTEMTIME st;
92 FILETIME ft;
93 LARGE_INTEGER li;
94 TIME_ZONE_INFORMATION tzi;
95 __int64 t;
96 static int tzflag;
97
98 if (tv)
99 {
100 GetSystemTime(&st);
101 SystemTimeToFileTime(&st, &ft);
102 li.LowPart = ft.dwLowDateTime;
103 li.HighPart = ft.dwHighDateTime;
104 t = li.QuadPart; /* In 100-nanosecond intervals */
105 t -= EPOCHFILETIME; /* Offset to the Epoch time */
106 t /= 10; /* In microseconds */
107 tv->tv_sec = (long)(t / 1000000);
108 tv->tv_usec = (long)(t % 1000000);
109 }
110
111 if (tz)
112 {
113 GetTimeZoneInformation(&tzi);
114
115 tz->tz_minuteswest = tzi.Bias;
116 if (tzi.StandardDate.wMonth != 0)
117 {
118 tz->tz_minuteswest += tzi.StandardBias * 60;
119 }
120
121 if (tzi.DaylightDate.wMonth != 0)
122 {
123 tz->tz_dsttime = 1;
124 }
125 else
126 {
127 tz->tz_dsttime = 0;
128 }
129 }
130
131 return 0;
132}
133
134#endif /* _WIN32_WCE */
135
136#endif /* GEOS_TIMEVAL_H */