1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/stopwatch.cpp
3 // Purpose: wxStopWatch and other non-GUI stuff from wx/timer.h
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)
10 // Created: 20.06.2003 (extracted from common/timercmn.cpp)
12 // Copyright: (c) 1998-2003 wxWidgets Team
13 // License: wxWindows license
14 ///////////////////////////////////////////////////////////////////////////////
16 // ============================================================================
18 // ============================================================================
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
24 // for compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
31 #include "wx/stopwatch.h"
35 #include "wx/msw/wrapwin.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 #if defined(__WIN32__) && !defined(HAVE_FTIME) && !defined(__MWERKS__) && !defined(__WXWINCE__)
49 #if defined(__VISAGECPP__) && !defined(HAVE_FTIME)
51 # if __IBMCPP__ >= 400
52 # define ftime(x) _ftime(x)
56 #if defined(__MWERKS__) && defined(__WXMSW__)
58 # undef HAVE_GETTIMEOFDAY
65 #include "wx/msw/private.h"
66 #include "wx/msw/wince/time.h"
68 #endif // __WXPALMOS5__
71 #if !defined(__WXMAC__) && !defined(__WXWINCE__)
72 #include <sys/types.h> // for time_t
75 #if defined(HAVE_GETTIMEOFDAY)
78 #elif defined(HAVE_FTIME)
79 #include <sys/timeb.h>
85 #include <DriverServices.h>
87 #include <Carbon/Carbon.h>
94 #include <SystemMgr.h>
97 // ============================================================================
99 // ============================================================================
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
107 void wxStopWatch::Start(long t
)
111 LARGE_INTEGER frequency_li
;
112 ::QueryPerformanceFrequency( &frequency_li
);
113 m_frequency
= frequency_li
.QuadPart
;
114 if (m_frequency
== 0)
116 m_t0
= wxGetLocalTimeMillis() - t
;
120 LARGE_INTEGER counter_li
;
121 ::QueryPerformanceCounter( &counter_li
);
122 wxLongLong counter
= counter_li
.QuadPart
;
123 m_t0
= (counter
* 10000 / m_frequency
) - t
*10;
126 m_t0
= wxGetLocalTimeMillis() - t
;
132 long wxStopWatch::GetElapsedTime() const
136 if (m_frequency
== 0)
138 return (wxGetLocalTimeMillis() - m_t0
).GetLo();
142 LARGE_INTEGER counter_li
;
143 ::QueryPerformanceCounter( &counter_li
);
144 wxLongLong counter
= counter_li
.QuadPart
;
145 wxLongLong res
= (counter
* 10000 / m_frequency
) - m_t0
;
146 return res
.GetLo() / 10;
149 return (wxGetLocalTimeMillis() - m_t0
).GetLo();
153 long wxStopWatch::Time() const
155 return m_pauseCount
? m_pause
: GetElapsedTime();
158 #endif // wxUSE_STOPWATCH
160 // ----------------------------------------------------------------------------
161 // old timer functions superceded by wxStopWatch
162 // ----------------------------------------------------------------------------
166 static wxLongLong wxStartTime
= 0l;
168 // starts the global timer
171 wxStartTime
= wxGetLocalTimeMillis();
174 // Returns elapsed time in milliseconds
175 long wxGetElapsedTime(bool resetTimer
)
177 wxLongLong oldTime
= wxStartTime
;
178 wxLongLong newTime
= wxGetLocalTimeMillis();
181 wxStartTime
= newTime
;
183 return (newTime
- oldTime
).GetLo();
186 #endif // wxUSE_LONGLONG
188 // ----------------------------------------------------------------------------
189 // the functions to get the current time and timezone info
190 // ----------------------------------------------------------------------------
192 // Get local time as seconds since 00:00:00, Jan 1st 1970
193 long wxGetLocalTime()
198 // This cannot be made static because mktime can overwrite it.
200 memset(&tm
, 0, sizeof(tm
));
203 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
207 tm
.tm_isdst
= -1; // let mktime guess
209 // Note that mktime assumes that the struct tm contains local time.
211 t1
= time(&t1
); // now
212 t0
= mktime(&tm
); // origin
214 // Return the difference in seconds.
216 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
217 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
219 wxLogSysError(_("Failed to get the local system time"));
223 // Get UTC time as seconds since 00:00:00, Jan 1st 1970
226 return (long)time(NULL
);
231 // Get local time as milliseconds since 00:00:00, Jan 1st 1970
232 wxLongLong
wxGetLocalTimeMillis()
234 wxLongLong val
= 1000l;
236 // If possible, use a function which avoids conversions from
237 // broken-up time structures to milliseconds
239 #if defined(__WXPALMOS__)
248 uint32_t now
= TimGetSeconds();
249 uint32_t then
= TimDateTimeToSeconds (&thenst
);
250 return SysTimeToMilliSecs(SysTimeInSecs(now
- then
));
251 #elif defined(__WXMSW__) && (defined(__WINE__) || defined(__MWERKS__))
252 // This should probably be the way all WXMSW compilers should do it
253 // Go direct to the OS for time
255 SYSTEMTIME thenst
= { 1970, 1, 4, 1, 0, 0, 0, 0 }; // 00:00:00 Jan 1st 1970
257 SystemTimeToFileTime( &thenst
, &thenft
);
258 wxLongLong
then( thenft
.dwHighDateTime
, thenft
.dwLowDateTime
); // time in 100 nanoseconds
261 GetLocalTime( &nowst
);
263 SystemTimeToFileTime( &nowst
, &nowft
);
264 wxLongLong
now( nowft
.dwHighDateTime
, nowft
.dwLowDateTime
); // time in 100 nanoseconds
266 return ( now
- then
) / 10000.0; // time from 00:00:00 Jan 1st 1970 to now in milliseconds
268 #elif defined(HAVE_GETTIMEOFDAY)
270 if ( wxGetTimeOfDay(&tp
) != -1 )
273 return (val
+ (tp
.tv_usec
/ 1000));
277 wxLogError(_("wxGetTimeOfDay failed."));
280 #elif defined(HAVE_FTIME)
283 // ftime() is void and not int in some mingw32 headers, so don't
284 // test the return code (well, it shouldn't fail anyhow...)
287 return (val
+ tp
.millitm
);
288 #elif defined(__WXMAC__)
290 static UInt64 gMilliAtStart
= 0;
292 Nanoseconds upTime
= AbsoluteToNanoseconds( UpTime() );
294 if ( gMilliAtStart
== 0 )
296 time_t start
= time(NULL
);
297 gMilliAtStart
= ((UInt64
) start
) * 1000000L;
298 gMilliAtStart
-= upTime
.lo
/ 1000 ;
299 gMilliAtStart
-= ( ( (UInt64
) upTime
.hi
) << 32 ) / (1000 * 1000);
302 UInt64 millival
= gMilliAtStart
;
303 millival
+= upTime
.lo
/ (1000 * 1000);
304 millival
+= ( ( (UInt64
) upTime
.hi
) << 32 ) / (1000 * 1000);
308 #else // no gettimeofday() nor ftime()
309 // We use wxGetLocalTime() to get the seconds since
310 // 00:00:00 Jan 1st 1970 and then whatever is available
311 // to get millisecond resolution.
313 // NOTE that this might lead to a problem if the clocks
314 // use different sources, so this approach should be
315 // avoided where possible.
317 val
*= wxGetLocalTime();
319 // GRG: This will go soon as all WIN32 seem to have ftime
320 // JACS: unfortunately not. WinCE doesn't have it.
321 #if defined (__WIN32__)
322 // If your platform/compiler needs to use two different functions
323 // to get ms resolution, please do NOT just shut off these warnings,
324 // drop me a line instead at <guille@iies.es>
328 #warning "Possible clock skew bug in wxGetLocalTimeMillis()!"
333 val
+= st
.wMilliseconds
;
335 // If your platform/compiler does not support ms resolution please
336 // do NOT just shut off these warnings, drop me a line instead at
339 #if defined(__VISUALC__) || defined (__WATCOMC__)
340 #pragma message("wxStopWatch will be up to second resolution!")
341 #elif defined(__BORLANDC__)
342 #pragma message "wxStopWatch will be up to second resolution!"
344 #warning "wxStopWatch will be up to second resolution!"
350 #endif // time functions
353 #else // !wxUSE_LONGLONG
355 double wxGetLocalTimeMillis(void)
357 return (double(clock()) / double(CLOCKS_PER_SEC
)) * 1000.0;
360 #endif // wxUSE_LONGLONG/!wxUSE_LONGLONG