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