]>
git.saurik.com Git - wxWidgets.git/blob - src/common/stopwatch.cpp
922a5f0ed8b937848f10c00df4bdfe599ccd2beb
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(__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 <SystemMgr.h>
88 // ============================================================================
90 // ============================================================================
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
110 bool CanBeUsed() const
112 return freq
.QuadPart
!= 0;
115 wxCriticalSection cs
;
120 } // anonymous namespace
124 void wxStopWatch::Start(long t
)
127 if ( !gs_perfCounter
.init
)
129 wxCriticalSectionLocker
lock(gs_perfCounter
.cs
);
130 ::QueryPerformanceFrequency(&gs_perfCounter
.freq
);
131 gs_perfCounter
.init
= true;
134 LARGE_INTEGER counter
;
135 if ( gs_perfCounter
.CanBeUsed() && ::QueryPerformanceCounter(&counter
) )
137 m_t0
= counter
.QuadPart
- t
*gs_perfCounter
.freq
.QuadPart
/1000;
139 else // Fall back to the generic code below.
142 m_t0
= wxGetLocalTimeMillis() - t
;
149 long wxStopWatch::GetElapsedTime() const
152 LARGE_INTEGER counter
;
153 if ( gs_perfCounter
.CanBeUsed() && ::QueryPerformanceCounter(&counter
) )
155 wxLongLong
delta(counter
.QuadPart
);
158 return ((delta
*1000)/gs_perfCounter
.freq
.QuadPart
).GetLo();
161 return (wxGetLocalTimeMillis() - m_t0
).GetLo();
164 long wxStopWatch::Time() const
166 return m_pauseCount
? m_pause
: GetElapsedTime();
169 #endif // wxUSE_STOPWATCH
171 // ----------------------------------------------------------------------------
172 // old timer functions superceded by wxStopWatch
173 // ----------------------------------------------------------------------------
177 static wxLongLong wxStartTime
= 0l;
179 // starts the global timer
182 wxStartTime
= wxGetLocalTimeMillis();
185 // Returns elapsed time in milliseconds
186 long wxGetElapsedTime(bool resetTimer
)
188 wxLongLong oldTime
= wxStartTime
;
189 wxLongLong newTime
= wxGetLocalTimeMillis();
192 wxStartTime
= newTime
;
194 return (newTime
- oldTime
).GetLo();
197 #endif // wxUSE_LONGLONG
199 // ----------------------------------------------------------------------------
200 // the functions to get the current time and timezone info
201 // ----------------------------------------------------------------------------
203 // Get local time as seconds since 00:00:00, Jan 1st 1970
204 long wxGetLocalTime()
209 // This cannot be made static because mktime can overwrite it.
211 memset(&tm
, 0, sizeof(tm
));
214 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
218 tm
.tm_isdst
= -1; // let mktime guess
220 // Note that mktime assumes that the struct tm contains local time.
222 t1
= time(&t1
); // now
223 t0
= mktime(&tm
); // origin
225 // Return the difference in seconds.
227 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
228 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
230 wxLogSysError(_("Failed to get the local system time"));
234 // Get UTC time as seconds since 00:00:00, Jan 1st 1970
237 return (long)time(NULL
);
242 // Get local time as milliseconds since 00:00:00, Jan 1st 1970
243 wxLongLong
wxGetLocalTimeMillis()
245 wxLongLong val
= 1000l;
247 // If possible, use a function which avoids conversions from
248 // broken-up time structures to milliseconds
250 #if defined(__WXPALMOS__)
259 uint32_t now
= TimGetSeconds();
260 uint32_t then
= TimDateTimeToSeconds (&thenst
);
261 return SysTimeToMilliSecs(SysTimeInSecs(now
- then
));
262 #elif defined(__WXMSW__) && (defined(__WINE__) || defined(__MWERKS__))
263 // This should probably be the way all WXMSW compilers should do it
264 // Go direct to the OS for time
266 SYSTEMTIME thenst
= { 1970, 1, 4, 1, 0, 0, 0, 0 }; // 00:00:00 Jan 1st 1970
268 SystemTimeToFileTime( &thenst
, &thenft
);
269 wxLongLong
then( thenft
.dwHighDateTime
, thenft
.dwLowDateTime
); // time in 100 nanoseconds
272 GetLocalTime( &nowst
);
274 SystemTimeToFileTime( &nowst
, &nowft
);
275 wxLongLong
now( nowft
.dwHighDateTime
, nowft
.dwLowDateTime
); // time in 100 nanoseconds
277 return ( now
- then
) / 10000.0; // time from 00:00:00 Jan 1st 1970 to now in milliseconds
279 #elif defined(HAVE_GETTIMEOFDAY)
281 if ( wxGetTimeOfDay(&tp
) != -1 )
284 return (val
+ (tp
.tv_usec
/ 1000));
288 wxLogError(_("wxGetTimeOfDay failed."));
291 #elif defined(HAVE_FTIME)
294 // ftime() is void and not int in some mingw32 headers, so don't
295 // test the return code (well, it shouldn't fail anyhow...)
298 return (val
+ tp
.millitm
);
299 #else // no gettimeofday() nor ftime()
300 // We use wxGetLocalTime() to get the seconds since
301 // 00:00:00 Jan 1st 1970 and then whatever is available
302 // to get millisecond resolution.
304 // NOTE that this might lead to a problem if the clocks
305 // use different sources, so this approach should be
306 // avoided where possible.
308 val
*= wxGetLocalTime();
310 // GRG: This will go soon as all WIN32 seem to have ftime
311 // JACS: unfortunately not. WinCE doesn't have it.
312 #if defined (__WIN32__)
313 // If your platform/compiler needs to use two different functions
314 // to get ms resolution, please do NOT just shut off these warnings,
315 // drop me a line instead at <guille@iies.es>
319 #warning "Possible clock skew bug in wxGetLocalTimeMillis()!"
324 val
+= st
.wMilliseconds
;
326 // If your platform/compiler does not support ms resolution please
327 // do NOT just shut off these warnings, drop me a line instead at
330 #if defined(__VISUALC__) || defined (__WATCOMC__)
331 #pragma message("wxStopWatch will be up to second resolution!")
332 #elif defined(__BORLANDC__)
333 #pragma message "wxStopWatch will be up to second resolution!"
335 #warning "wxStopWatch will be up to second resolution!"
341 #endif // time functions
344 #else // !wxUSE_LONGLONG
346 double wxGetLocalTimeMillis(void)
348 return (double(clock()) / double(CLOCKS_PER_SEC
)) * 1000.0;
351 #endif // wxUSE_LONGLONG/!wxUSE_LONGLONG