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