Debian Codegraph @

addons/ooh323c/src/ooDateTime.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2004-2005 by Objective Systems, Inc.
3 *
4 * This software is furnished under an open source license and may be
5 * used and copied only in accordance with the terms of this license.
6 * The text of the license may generally be found in the root
7 * directory of this installation in the LICENSE.txt file. It
8 * can also be viewed online at the following URL:
9 *
10 * http://www.obj-sys.com/open/license.html
11 *
12 * Any redistributions of this file including modified versions must
13 * maintain this copyright notice.
14 *
15 *****************************************************************************/
16
17 #include "ooCommon.h"
18 #include "ooDateTime.h"
19
20 #if defined(_WIN32) || defined(_MSC_VER) || defined(__MINGW32__)
21
22 #ifndef __MINGW32__
23 #include <SYS\TIMEB.H>
24 int gettimeofday (struct timeval *tv, struct timezone *tz)
25 {
26 struct _timeb currSysTime;
27 _ftime(&currSysTime);
28
29 tv->tv_sec = currSysTime.time;
30 tv->tv_usec = currSysTime.millitm * 1000;
31
32 return 0;
33 }
34
35 #else
36 /* Mingw specific logic..probably works with regular winders
37 * too, but cannot test. --Ben
38 */
39 #define uint64 unsigned long long
40 #define uint32 unsigned long
41
42 static uint64 calcEpocOffset() {
43 SYSTEMTIME st;
44 FILETIME ft;
45
46 memset(&st, 0, sizeof(st));
47 memset(&ft, 0, sizeof(ft));
48
49 st.wYear = 1970;
50 st.wMonth = 1;
51 st.wDayOfWeek = 0;
52 st.wDay = 1;
53 st.wHour = 0;
54 st.wMinute = 0;
55 st.wSecond = 0;
56 st.wMilliseconds = 0;
57
58 if (!SystemTimeToFileTime(&st, &ft)) {
59 //cout << "ERROR: SystemTimeToFileTime failed, err: "
60 // << GetLastError() << endl;
61 }
62
63 uint64 t = ft.dwHighDateTime;
64 t = t << 32;
65 t |= ft.dwLowDateTime;
66
67 return t;
68 }
69
70 // Gets high resolution by spinning up to 15ms. Don't call this often!!!
71 static uint64 getRawCurMsSpin() {
72 FILETIME tm;
73 uint64 t_now;
74
75 static uint64 epocOffset = 0;
76 static int do_once = 1;
77 if (do_once) {
78 epocOffset = calcEpocOffset();
79 do_once = 0;
80 }
81
82 GetSystemTimeAsFileTime(&tm);
83 uint64 t_start = tm.dwHighDateTime;
84 t_start = t_start << 32;
85 t_start |= tm.dwLowDateTime;
86 while (1) {
87 GetSystemTimeAsFileTime(&tm);
88 t_now = tm.dwHighDateTime;
89 t_now = t_now << 32;
90 t_now |= tm.dwLowDateTime;
91
92 if (t_now != t_start) {
93 // Hit a boundary...as accurate as we are going to get.
94 break;
95 }
96 }
97
98
99 t_now -= epocOffset;
100
101 // t is in 100ns units, convert to usecs
102 t_now = t_now / 10000; //msecs
103
104 return t_now;
105 }
106
107 static uint64 baselineMs = 0;
108 static uint32 tickBaseline = 0;
109
110 int gettimeofday(struct timeval* tv, void* null) {
111
112 // Get baseline time, in seconds.
113 if (baselineMs == 0) {
114 baselineMs = getRawCurMsSpin();
115 tickBaseline = timeGetTime();
116 }
117
118 uint32 curTicks = timeGetTime();
119 if (curTicks < tickBaseline) {
120 // Wrap!
121 baselineMs = getRawCurMsSpin();
122 tickBaseline = timeGetTime();
123 }
124
125 uint64 now_ms = (baselineMs + (curTicks - tickBaseline));
126 *tv = oo_ms_to_tv(now_ms);
127 return 0;
128 };
129
130
131 /* Correctly synched with the 'real' time/date clock, but only exact to
132 * about 15ms. Set foo to non NULL if you want to recalculate the
133 */
134 uint64 getCurMsFromClock() {
135 // This has resolution of only about 15ms
136 FILETIME tm;
137 // The Windows epoc is January 1, 1601 (UTC).
138 static uint64 offset = 0;
139 static int do_once = 1;
140 if (do_once) {
141 offset = calcEpocOffset();
142 do_once = 0;
143 }
144
145
146 GetSystemTimeAsFileTime(&tm);
147 uint64 t = tm.dwHighDateTime;
148 t = t << 32;
149 t |= tm.dwLowDateTime;
150
151 /*cout << "file-time: " << t << " offset: " << offset
152 << " normalized: " << (t - offset) << " hi: "
153 << tm.dwHighDateTime << " lo: " << tm.dwLowDateTime
154 << " calc-offset:\n" << calcEpocOffset() << "\n\n";
155 */
156 t -= offset;
157
158 // t is in 100ns units, convert to ms
159 t = t / 10000;
160 return t;
161 }
162 #endif
163 #endif
164
165 int ooGetTimeOfDay (struct timeval *tv, struct timezone *tz)
166 {
167 return gettimeofday (tv, tz);
168 }
169
170
171 long ooGetTimeDiff(struct timeval *tv1, struct timeval *tv2)
172 {
173 return ( ((tv2->tv_sec-tv1->tv_sec)*1000) +
174 ((tv2->tv_usec-tv1->tv_usec)/1000) );
175 }