]>
Commit | Line | Data |
---|---|---|
e2478fde VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: common/stopwatch.cpp | |
3 | // Purpose: wxStopWatch and other non-GUI stuff from wx/timer.h | |
4 | // Author: | |
5 | // Original version by Julian Smart | |
6 | // Vadim Zeitlin got rid of all ifdefs (11.12.99) | |
7 | // Sylvain Bougnoux added wxStopWatch class | |
8 | // Guillermo Rodriguez <guille@iies.es> rewrote from scratch (Dic/99) | |
9 | // Modified by: | |
10 | // Created: 20.06.2003 (extracted from common/timercmn.cpp) | |
11 | // RCS-ID: $Id$ | |
77ffb593 | 12 | // Copyright: (c) 1998-2003 wxWidgets Team |
0a53b9b8 | 13 | // License: wxWindows license |
e2478fde VZ |
14 | /////////////////////////////////////////////////////////////////////////////// |
15 | ||
16 | // ============================================================================ | |
17 | // declarations | |
18 | // ============================================================================ | |
19 | ||
20 | // ---------------------------------------------------------------------------- | |
21 | // headers | |
22 | // ---------------------------------------------------------------------------- | |
23 | ||
24 | // for compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
31 | #ifndef WX_PRECOMP | |
8df6de97 VS |
32 | #include "wx/intl.h" |
33 | #include "wx/log.h" | |
e2478fde VZ |
34 | #endif //WX_PRECOMP |
35 | ||
e2478fde | 36 | #include "wx/longlong.h" |
d5b12ad9 | 37 | #include "wx/stopwatch.h" |
e2478fde VZ |
38 | |
39 | // ---------------------------------------------------------------------------- | |
40 | // System headers | |
41 | // ---------------------------------------------------------------------------- | |
42 | ||
43 | #if defined(__WIN32__) | |
9ed0d735 | 44 | #include "wx/msw/wrapwin.h" |
e2478fde VZ |
45 | #endif |
46 | ||
1c193821 | 47 | #if defined(__WIN32__) && !defined(HAVE_FTIME) && !defined(__MWERKS__) && !defined(__WXWINCE__) |
e2478fde VZ |
48 | #define HAVE_FTIME |
49 | #endif | |
50 | ||
51 | #if defined(__VISAGECPP__) && !defined(HAVE_FTIME) | |
52 | #define HAVE_FTIME | |
53 | # if __IBMCPP__ >= 400 | |
54 | # define ftime(x) _ftime(x) | |
55 | # endif | |
56 | #endif | |
57 | ||
58 | #if defined(__MWERKS__) && defined(__WXMSW__) | |
59 | # undef HAVE_FTIME | |
60 | # undef HAVE_GETTIMEOFDAY | |
61 | #endif | |
62 | ||
1c193821 | 63 | #ifndef __WXWINCE__ |
e2478fde | 64 | #include <time.h> |
1c193821 JS |
65 | #else |
66 | #include "wx/msw/private.h" | |
67 | #include "wx/msw/wince/time.h" | |
68 | #endif | |
69 | ||
70 | #if !defined(__WXMAC__) && !defined(__WXWINCE__) | |
e2478fde VZ |
71 | #include <sys/types.h> // for time_t |
72 | #endif | |
73 | ||
74 | #if defined(HAVE_GETTIMEOFDAY) | |
75 | #include <sys/time.h> | |
76 | #include <unistd.h> | |
77 | #elif defined(HAVE_FTIME) | |
78 | #include <sys/timeb.h> | |
79 | #endif | |
80 | ||
81 | #ifdef __WXMAC__ | |
768c6e8b | 82 | #ifndef __DARWIN__ |
e2478fde VZ |
83 | #include <Timer.h> |
84 | #include <DriverServices.h> | |
768c6e8b SC |
85 | #else |
86 | #include <Carbon/Carbon.h> | |
87 | #endif | |
e2478fde VZ |
88 | #endif |
89 | ||
20bc5ad8 WS |
90 | #ifdef __WXPALMOS__ |
91 | #include <DateTime.h> | |
92 | #include <TimeMgr.h> | |
93 | #include <SystemMgr.h> | |
94 | #endif | |
95 | ||
e2478fde VZ |
96 | // ---------------------------------------------------------------------------- |
97 | // macros | |
98 | // ---------------------------------------------------------------------------- | |
99 | ||
100 | // on some really old systems gettimeofday() doesn't have the second argument, | |
101 | // define wxGetTimeOfDay() to hide this difference | |
102 | #ifdef HAVE_GETTIMEOFDAY | |
103 | #ifdef WX_GETTIMEOFDAY_NO_TZ | |
104 | struct timezone; | |
105 | #define wxGetTimeOfDay(tv, tz) gettimeofday(tv) | |
106 | #else | |
107 | #define wxGetTimeOfDay(tv, tz) gettimeofday((tv), (tz)) | |
108 | #endif | |
109 | #endif // HAVE_GETTIMEOFDAY | |
110 | ||
111 | // ============================================================================ | |
112 | // implementation | |
113 | // ============================================================================ | |
114 | ||
115 | // ---------------------------------------------------------------------------- | |
116 | // wxStopWatch | |
117 | // ---------------------------------------------------------------------------- | |
118 | ||
119 | #if wxUSE_STOPWATCH | |
120 | ||
121 | void wxStopWatch::Start(long t) | |
122 | { | |
85445d34 RR |
123 | #if 0 |
124 | // __WXMSW__ | |
2555c77a RR |
125 | LARGE_INTEGER frequency_li; |
126 | ::QueryPerformanceFrequency( &frequency_li ); | |
127 | m_frequency = frequency_li.QuadPart; | |
128 | if (m_frequency == 0) | |
129 | { | |
130 | m_t0 = wxGetLocalTimeMillis() - t; | |
131 | } | |
132 | else | |
133 | { | |
134 | LARGE_INTEGER counter_li; | |
135 | ::QueryPerformanceCounter( &counter_li ); | |
136 | wxLongLong counter = counter_li.QuadPart; | |
137 | m_t0 = (counter * 10000 / m_frequency) - t*10; | |
138 | } | |
139 | #else | |
e2478fde | 140 | m_t0 = wxGetLocalTimeMillis() - t; |
2555c77a | 141 | #endif |
e2478fde VZ |
142 | m_pause = 0; |
143 | m_pauseCount = 0; | |
144 | } | |
145 | ||
146 | long wxStopWatch::GetElapsedTime() const | |
147 | { | |
85445d34 RR |
148 | #if 0 |
149 | //__WXMSW__ | |
2555c77a RR |
150 | if (m_frequency == 0) |
151 | { | |
152 | return (wxGetLocalTimeMillis() - m_t0).GetLo(); | |
153 | } | |
154 | else | |
155 | { | |
156 | LARGE_INTEGER counter_li; | |
157 | ::QueryPerformanceCounter( &counter_li ); | |
158 | wxLongLong counter = counter_li.QuadPart; | |
159 | wxLongLong res = (counter * 10000 / m_frequency) - m_t0; | |
160 | return res.GetLo() / 10; | |
161 | } | |
162 | #else | |
e2478fde | 163 | return (wxGetLocalTimeMillis() - m_t0).GetLo(); |
2555c77a | 164 | #endif |
e2478fde VZ |
165 | } |
166 | ||
167 | long wxStopWatch::Time() const | |
168 | { | |
169 | return m_pauseCount ? m_pause : GetElapsedTime(); | |
170 | } | |
171 | ||
172 | #endif // wxUSE_STOPWATCH | |
173 | ||
174 | // ---------------------------------------------------------------------------- | |
175 | // old timer functions superceded by wxStopWatch | |
176 | // ---------------------------------------------------------------------------- | |
177 | ||
178 | #if wxUSE_LONGLONG | |
179 | ||
180 | static wxLongLong wxStartTime = 0l; | |
181 | ||
182 | // starts the global timer | |
183 | void wxStartTimer() | |
184 | { | |
185 | wxStartTime = wxGetLocalTimeMillis(); | |
186 | } | |
187 | ||
188 | // Returns elapsed time in milliseconds | |
189 | long wxGetElapsedTime(bool resetTimer) | |
190 | { | |
191 | wxLongLong oldTime = wxStartTime; | |
192 | wxLongLong newTime = wxGetLocalTimeMillis(); | |
193 | ||
194 | if ( resetTimer ) | |
195 | wxStartTime = newTime; | |
196 | ||
197 | return (newTime - oldTime).GetLo(); | |
198 | } | |
199 | ||
200 | #endif // wxUSE_LONGLONG | |
201 | ||
202 | // ---------------------------------------------------------------------------- | |
203 | // the functions to get the current time and timezone info | |
204 | // ---------------------------------------------------------------------------- | |
205 | ||
206 | // Get local time as seconds since 00:00:00, Jan 1st 1970 | |
207 | long wxGetLocalTime() | |
208 | { | |
209 | struct tm tm; | |
210 | time_t t0, t1; | |
211 | ||
212 | // This cannot be made static because mktime can overwrite it. | |
213 | // | |
214 | memset(&tm, 0, sizeof(tm)); | |
215 | tm.tm_year = 70; | |
216 | tm.tm_mon = 0; | |
217 | tm.tm_mday = 5; // not Jan 1st 1970 due to mktime 'feature' | |
218 | tm.tm_hour = 0; | |
219 | tm.tm_min = 0; | |
220 | tm.tm_sec = 0; | |
221 | tm.tm_isdst = -1; // let mktime guess | |
222 | ||
223 | // Note that mktime assumes that the struct tm contains local time. | |
224 | // | |
225 | t1 = time(&t1); // now | |
226 | t0 = mktime(&tm); // origin | |
227 | ||
228 | // Return the difference in seconds. | |
229 | // | |
230 | if (( t0 != (time_t)-1 ) && ( t1 != (time_t)-1 )) | |
231 | return (long)difftime(t1, t0) + (60 * 60 * 24 * 4); | |
232 | ||
233 | wxLogSysError(_("Failed to get the local system time")); | |
234 | return -1; | |
235 | } | |
236 | ||
237 | // Get UTC time as seconds since 00:00:00, Jan 1st 1970 | |
238 | long wxGetUTCTime() | |
239 | { | |
940b4f07 | 240 | return (long)time(NULL); |
e2478fde VZ |
241 | } |
242 | ||
243 | #if wxUSE_LONGLONG | |
244 | ||
245 | // Get local time as milliseconds since 00:00:00, Jan 1st 1970 | |
246 | wxLongLong wxGetLocalTimeMillis() | |
247 | { | |
248 | wxLongLong val = 1000l; | |
249 | ||
250 | // If possible, use a function which avoids conversions from | |
251 | // broken-up time structures to milliseconds | |
252 | ||
60582201 WS |
253 | #if defined(__WXPALMOS__) |
254 | DateTimeType thenst; | |
255 | thenst.second = 0; | |
256 | thenst.minute = 0; | |
257 | thenst.hour = 0; | |
258 | thenst.day = 1; | |
259 | thenst.month = 1; | |
260 | thenst.year = 1970; | |
261 | thenst.weekDay = 5; | |
262 | uint32_t now = TimGetSeconds(); | |
263 | uint32_t then = TimDateTimeToSeconds (&thenst); | |
264 | return SysTimeToMilliSecs(SysTimeInSecs(now - then)); | |
265 | #elif defined(__WXMSW__) && (defined(__WINE__) || defined(__MWERKS__)) | |
e2478fde VZ |
266 | // This should probably be the way all WXMSW compilers should do it |
267 | // Go direct to the OS for time | |
268 | ||
269 | SYSTEMTIME thenst = { 1970, 1, 4, 1, 0, 0, 0, 0 }; // 00:00:00 Jan 1st 1970 | |
270 | FILETIME thenft; | |
271 | SystemTimeToFileTime( &thenst, &thenft ); | |
272 | wxLongLong then( thenft.dwHighDateTime, thenft.dwLowDateTime ); // time in 100 nanoseconds | |
273 | ||
274 | SYSTEMTIME nowst; | |
275 | GetLocalTime( &nowst ); | |
276 | FILETIME nowft; | |
277 | SystemTimeToFileTime( &nowst, &nowft ); | |
278 | wxLongLong now( nowft.dwHighDateTime, nowft.dwLowDateTime ); // time in 100 nanoseconds | |
279 | ||
280 | return ( now - then ) / 10000.0; // time from 00:00:00 Jan 1st 1970 to now in milliseconds | |
d775fa82 | 281 | |
e2478fde VZ |
282 | #elif defined(HAVE_GETTIMEOFDAY) |
283 | struct timeval tp; | |
284 | if ( wxGetTimeOfDay(&tp, (struct timezone *)NULL) != -1 ) | |
285 | { | |
286 | val *= tp.tv_sec; | |
287 | return (val + (tp.tv_usec / 1000)); | |
288 | } | |
289 | else | |
290 | { | |
291 | wxLogError(_("wxGetTimeOfDay failed.")); | |
292 | return 0; | |
293 | } | |
294 | #elif defined(HAVE_FTIME) | |
295 | struct timeb tp; | |
296 | ||
297 | // ftime() is void and not int in some mingw32 headers, so don't | |
298 | // test the return code (well, it shouldn't fail anyhow...) | |
29468083 | 299 | (void)::ftime(&tp); |
e2478fde VZ |
300 | val *= tp.time; |
301 | return (val + tp.millitm); | |
302 | #elif defined(__WXMAC__) | |
d775fa82 | 303 | |
e2478fde VZ |
304 | static UInt64 gMilliAtStart = 0; |
305 | ||
306 | Nanoseconds upTime = AbsoluteToNanoseconds( UpTime() ); | |
307 | ||
308 | if ( gMilliAtStart == 0 ) | |
309 | { | |
310 | time_t start = time(NULL); | |
311 | gMilliAtStart = ((UInt64) start) * 1000000L; | |
312 | gMilliAtStart -= upTime.lo / 1000 ; | |
313 | gMilliAtStart -= ( ( (UInt64) upTime.hi ) << 32 ) / (1000 * 1000); | |
314 | } | |
315 | ||
316 | UInt64 millival = gMilliAtStart; | |
317 | millival += upTime.lo / (1000 * 1000); | |
318 | millival += ( ( (UInt64) upTime.hi ) << 32 ) / (1000 * 1000); | |
319 | val = millival; | |
320 | ||
321 | return val; | |
322 | #else // no gettimeofday() nor ftime() | |
323 | // We use wxGetLocalTime() to get the seconds since | |
324 | // 00:00:00 Jan 1st 1970 and then whatever is available | |
325 | // to get millisecond resolution. | |
326 | // | |
327 | // NOTE that this might lead to a problem if the clocks | |
328 | // use different sources, so this approach should be | |
329 | // avoided where possible. | |
330 | ||
331 | val *= wxGetLocalTime(); | |
332 | ||
333 | // GRG: This will go soon as all WIN32 seem to have ftime | |
1c193821 | 334 | // JACS: unfortunately not. WinCE doesn't have it. |
e2478fde VZ |
335 | #if defined (__WIN32__) |
336 | // If your platform/compiler needs to use two different functions | |
337 | // to get ms resolution, please do NOT just shut off these warnings, | |
338 | // drop me a line instead at <guille@iies.es> | |
1c193821 JS |
339 | |
340 | // FIXME | |
341 | #ifndef __WXWINCE__ | |
e2478fde | 342 | #warning "Possible clock skew bug in wxGetLocalTimeMillis()!" |
1c193821 | 343 | #endif |
e2478fde VZ |
344 | |
345 | SYSTEMTIME st; | |
346 | ::GetLocalTime(&st); | |
347 | val += st.wMilliseconds; | |
348 | #else // !Win32 | |
349 | // If your platform/compiler does not support ms resolution please | |
350 | // do NOT just shut off these warnings, drop me a line instead at | |
351 | // <guille@iies.es> | |
352 | ||
353 | #if defined(__VISUALC__) || defined (__WATCOMC__) | |
354 | #pragma message("wxStopWatch will be up to second resolution!") | |
355 | #elif defined(__BORLANDC__) | |
356 | #pragma message "wxStopWatch will be up to second resolution!" | |
357 | #else | |
358 | #warning "wxStopWatch will be up to second resolution!" | |
359 | #endif // compiler | |
360 | #endif | |
361 | ||
362 | return val; | |
363 | ||
364 | #endif // time functions | |
365 | } | |
366 | ||
367 | #endif // wxUSE_LONGLONG | |
368 | ||
369 |