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 // Licence: wxWindows licence
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(__VISAGECPP__) && !defined(HAVE_FTIME)
47 # if __IBMCPP__ >= 400
48 # define ftime(x) _ftime(x)
52 #if defined(__MWERKS__) && defined(__WXMSW__)
54 # undef HAVE_GETTIMEOFDAY
61 #include "wx/msw/private.h"
62 #include "wx/msw/wince/time.h"
64 #endif // __WXPALMOS5__
67 #if !defined(__WXMAC__) && !defined(__WXWINCE__)
68 #include <sys/types.h> // for time_t
71 #if defined(HAVE_GETTIMEOFDAY)
74 #elif defined(HAVE_FTIME)
75 #include <sys/timeb.h>
81 #include <SystemMgr.h>
84 // ============================================================================
86 // ============================================================================
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
106 bool CanBeUsed() const
108 return freq
.QuadPart
!= 0;
111 wxCriticalSection cs
;
118 const int MILLISECONDS_PER_SECOND
= 1000;
119 const int MICROSECONDS_PER_MILLISECOND
= 1000;
120 const int MICROSECONDS_PER_SECOND
= 1000*1000;
122 } // anonymous namespace
124 void wxStopWatch::DoStart()
127 if ( !gs_perfCounter
.init
)
129 wxCriticalSectionLocker
lock(gs_perfCounter
.cs
);
130 ::QueryPerformanceFrequency(&gs_perfCounter
.freq
);
132 // Just a sanity check: it's not supposed to happen but verify that
133 // ::QueryPerformanceCounter() succeeds so that we can really use it.
134 LARGE_INTEGER counter
;
135 if ( !::QueryPerformanceCounter(&counter
) )
137 wxLogDebug("QueryPerformanceCounter() unexpected failed (%s), "
138 "will not use it.", wxSysErrorMsg());
140 gs_perfCounter
.freq
.QuadPart
= 0;
143 gs_perfCounter
.init
= true;
147 m_t0
= GetCurrentClockValue();
150 wxLongLong
wxStopWatch::GetClockFreq() const
153 // Under MSW we use the high resolution performance counter timer which has
154 // its own frequency (usually related to the CPU clock speed).
155 if ( gs_perfCounter
.CanBeUsed() )
156 return gs_perfCounter
.freq
.QuadPart
;
159 // Currently milliseconds are used everywhere else.
160 return MILLISECONDS_PER_SECOND
;
163 void wxStopWatch::Start(long t0
)
167 m_t0
-= (wxLongLong(t0
)*GetClockFreq())/MILLISECONDS_PER_SECOND
;
170 wxLongLong
wxStopWatch::GetCurrentClockValue() const
173 if ( gs_perfCounter
.CanBeUsed() )
175 LARGE_INTEGER counter
;
176 ::QueryPerformanceCounter(&counter
);
177 return counter
.QuadPart
;
181 return wxGetLocalTimeMillis();
184 wxLongLong
wxStopWatch::TimeInMicro() const
186 const wxLongLong
elapsed(m_pauseCount
? m_elapsedBeforePause
187 : GetCurrentClockValue() - m_t0
);
189 return (elapsed
*MICROSECONDS_PER_SECOND
)/GetClockFreq();
192 #endif // wxUSE_STOPWATCH
194 // ----------------------------------------------------------------------------
195 // old timer functions superceded by wxStopWatch
196 // ----------------------------------------------------------------------------
200 static wxLongLong wxStartTime
= 0l;
202 // starts the global timer
205 wxStartTime
= wxGetLocalTimeMillis();
208 // Returns elapsed time in milliseconds
209 long wxGetElapsedTime(bool resetTimer
)
211 wxLongLong oldTime
= wxStartTime
;
212 wxLongLong newTime
= wxGetLocalTimeMillis();
215 wxStartTime
= newTime
;
217 return (newTime
- oldTime
).GetLo();
220 #endif // wxUSE_LONGLONG
222 // ----------------------------------------------------------------------------
223 // the functions to get the current time and timezone info
224 // ----------------------------------------------------------------------------
226 // Get local time as seconds since 00:00:00, Jan 1st 1970
227 long wxGetLocalTime()
232 // This cannot be made static because mktime can overwrite it.
234 memset(&tm
, 0, sizeof(tm
));
237 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
241 tm
.tm_isdst
= -1; // let mktime guess
243 // Note that mktime assumes that the struct tm contains local time.
245 t1
= time(&t1
); // now
246 t0
= mktime(&tm
); // origin
248 // Return the difference in seconds.
250 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
251 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
253 wxLogSysError(_("Failed to get the local system time"));
257 // Get UTC time as seconds since 00:00:00, Jan 1st 1970
260 return (long)time(NULL
);
265 wxLongLong
wxGetUTCTimeUSec()
267 #if defined(__WXMSW__)
269 ::GetSystemTimeAsFileTime(&ft
);
271 // FILETIME is in 100ns or 0.1us since 1601-01-01, transform to us since
273 wxLongLong
t(ft
.dwHighDateTime
, ft
.dwLowDateTime
);
275 t
-= wxLL(11644473600000000); // Unix - Windows epochs difference in us.
279 #ifdef HAVE_GETTIMEOFDAY
281 if ( wxGetTimeOfDay(&tv
) != -1 )
283 wxLongLong
val(tv
.tv_sec
);
284 val
*= MICROSECONDS_PER_SECOND
;
288 #endif // HAVE_GETTIMEOFDAY
290 // Fall back to lesser precision function.
291 return wxGetUTCTimeMillis()*1000;
295 // Get local time as milliseconds since 00:00:00, Jan 1st 1970
296 wxLongLong
wxGetUTCTimeMillis()
298 wxLongLong val
= 1000l;
300 // If possible, use a function which avoids conversions from
301 // broken-up time structures to milliseconds
302 #if defined(__WXPALMOS__)
311 uint32_t now
= TimGetSeconds();
312 uint32_t then
= TimDateTimeToSeconds (&thenst
);
313 return SysTimeToMilliSecs(SysTimeInSecs(now
- then
));
314 #elif defined(__WXMSW__)
316 ::GetSystemTimeAsFileTime(&ft
);
318 // FILETIME is expressed in 100ns (or 0.1us) units since 1601-01-01,
319 // transform them to ms since 1970-01-01.
320 wxLongLong
t(ft
.dwHighDateTime
, ft
.dwLowDateTime
);
322 t
-= wxLL(11644473600000); // Unix - Windows epochs difference in ms.
324 #elif defined(HAVE_GETTIMEOFDAY)
326 if ( wxGetTimeOfDay(&tp
) != -1 )
329 return (val
+ (tp
.tv_usec
/ 1000));
333 wxLogError(_("wxGetTimeOfDay failed."));
336 #elif defined(HAVE_FTIME)
339 // ftime() is void and not int in some mingw32 headers, so don't
340 // test the return code (well, it shouldn't fail anyhow...)
343 return (val
+ tp
.millitm
);
344 #else // no gettimeofday() nor ftime()
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
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!"
354 #warning "wxStopWatch will be up to second resolution!"
357 val
*= wxGetUTCTime();
359 #endif // time functions
362 wxLongLong
wxGetLocalTimeMillis()
364 return wxGetUTCTimeMillis() - wxGetTimeZone()*MILLISECONDS_PER_SECOND
;
367 #else // !wxUSE_LONGLONG
369 double wxGetLocalTimeMillis(void)
371 return (double(clock()) / double(CLOCKS_PER_SEC
)) * 1000.0;
374 #endif // wxUSE_LONGLONG/!wxUSE_LONGLONG