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
64 #include "wx/msw/private.h"
65 #include "wx/msw/wince/time.h"
68 #if !defined(__WXMAC__) && !defined(__WXWINCE__)
69 #include <sys/types.h> // for time_t
72 #if defined(HAVE_GETTIMEOFDAY)
75 #elif defined(HAVE_FTIME)
76 #include <sys/timeb.h>
82 #include <DriverServices.h>
84 #include <Carbon/Carbon.h>
91 #include <SystemMgr.h>
94 // ----------------------------------------------------------------------------
96 // ----------------------------------------------------------------------------
98 // on some really old systems gettimeofday() doesn't have the second argument,
99 // define wxGetTimeOfDay() to hide this difference
100 #ifdef HAVE_GETTIMEOFDAY
101 #ifdef WX_GETTIMEOFDAY_NO_TZ
103 #define wxGetTimeOfDay(tv, tz) gettimeofday(tv)
105 #define wxGetTimeOfDay(tv, tz) gettimeofday((tv), (tz))
107 #endif // HAVE_GETTIMEOFDAY
109 // ============================================================================
111 // ============================================================================
113 // ----------------------------------------------------------------------------
115 // ----------------------------------------------------------------------------
119 void wxStopWatch::Start(long t
)
123 LARGE_INTEGER frequency_li
;
124 ::QueryPerformanceFrequency( &frequency_li
);
125 m_frequency
= frequency_li
.QuadPart
;
126 if (m_frequency
== 0)
128 m_t0
= wxGetLocalTimeMillis() - t
;
132 LARGE_INTEGER counter_li
;
133 ::QueryPerformanceCounter( &counter_li
);
134 wxLongLong counter
= counter_li
.QuadPart
;
135 m_t0
= (counter
* 10000 / m_frequency
) - t
*10;
138 m_t0
= wxGetLocalTimeMillis() - t
;
144 long wxStopWatch::GetElapsedTime() const
148 if (m_frequency
== 0)
150 return (wxGetLocalTimeMillis() - m_t0
).GetLo();
154 LARGE_INTEGER counter_li
;
155 ::QueryPerformanceCounter( &counter_li
);
156 wxLongLong counter
= counter_li
.QuadPart
;
157 wxLongLong res
= (counter
* 10000 / m_frequency
) - m_t0
;
158 return res
.GetLo() / 10;
161 return (wxGetLocalTimeMillis() - m_t0
).GetLo();
165 long wxStopWatch::Time() const
167 return m_pauseCount
? m_pause
: GetElapsedTime();
170 #endif // wxUSE_STOPWATCH
172 // ----------------------------------------------------------------------------
173 // old timer functions superceded by wxStopWatch
174 // ----------------------------------------------------------------------------
178 static wxLongLong wxStartTime
= 0l;
180 // starts the global timer
183 wxStartTime
= wxGetLocalTimeMillis();
186 // Returns elapsed time in milliseconds
187 long wxGetElapsedTime(bool resetTimer
)
189 wxLongLong oldTime
= wxStartTime
;
190 wxLongLong newTime
= wxGetLocalTimeMillis();
193 wxStartTime
= newTime
;
195 return (newTime
- oldTime
).GetLo();
198 #endif // wxUSE_LONGLONG
200 // ----------------------------------------------------------------------------
201 // the functions to get the current time and timezone info
202 // ----------------------------------------------------------------------------
204 // Get local time as seconds since 00:00:00, Jan 1st 1970
205 long wxGetLocalTime()
210 // This cannot be made static because mktime can overwrite it.
212 memset(&tm
, 0, sizeof(tm
));
215 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
219 tm
.tm_isdst
= -1; // let mktime guess
221 // Note that mktime assumes that the struct tm contains local time.
223 t1
= time(&t1
); // now
224 t0
= mktime(&tm
); // origin
226 // Return the difference in seconds.
228 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
229 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
231 wxLogSysError(_("Failed to get the local system time"));
235 // Get UTC time as seconds since 00:00:00, Jan 1st 1970
238 return (long)time(NULL
);
243 // Get local time as milliseconds since 00:00:00, Jan 1st 1970
244 wxLongLong
wxGetLocalTimeMillis()
246 wxLongLong val
= 1000l;
248 // If possible, use a function which avoids conversions from
249 // broken-up time structures to milliseconds
251 #if defined(__WXPALMOS__)
260 uint32_t now
= TimGetSeconds();
261 uint32_t then
= TimDateTimeToSeconds (&thenst
);
262 return SysTimeToMilliSecs(SysTimeInSecs(now
- then
));
263 #elif defined(__WXMSW__) && (defined(__WINE__) || defined(__MWERKS__))
264 // This should probably be the way all WXMSW compilers should do it
265 // Go direct to the OS for time
267 SYSTEMTIME thenst
= { 1970, 1, 4, 1, 0, 0, 0, 0 }; // 00:00:00 Jan 1st 1970
269 SystemTimeToFileTime( &thenst
, &thenft
);
270 wxLongLong
then( thenft
.dwHighDateTime
, thenft
.dwLowDateTime
); // time in 100 nanoseconds
273 GetLocalTime( &nowst
);
275 SystemTimeToFileTime( &nowst
, &nowft
);
276 wxLongLong
now( nowft
.dwHighDateTime
, nowft
.dwLowDateTime
); // time in 100 nanoseconds
278 return ( now
- then
) / 10000.0; // time from 00:00:00 Jan 1st 1970 to now in milliseconds
280 #elif defined(HAVE_GETTIMEOFDAY)
282 if ( wxGetTimeOfDay(&tp
, (struct timezone
*)NULL
) != -1 )
285 return (val
+ (tp
.tv_usec
/ 1000));
289 wxLogError(_("wxGetTimeOfDay failed."));
292 #elif defined(HAVE_FTIME)
295 // ftime() is void and not int in some mingw32 headers, so don't
296 // test the return code (well, it shouldn't fail anyhow...)
299 return (val
+ tp
.millitm
);
300 #elif defined(__WXMAC__)
302 static UInt64 gMilliAtStart
= 0;
304 Nanoseconds upTime
= AbsoluteToNanoseconds( UpTime() );
306 if ( gMilliAtStart
== 0 )
308 time_t start
= time(NULL
);
309 gMilliAtStart
= ((UInt64
) start
) * 1000000L;
310 gMilliAtStart
-= upTime
.lo
/ 1000 ;
311 gMilliAtStart
-= ( ( (UInt64
) upTime
.hi
) << 32 ) / (1000 * 1000);
314 UInt64 millival
= gMilliAtStart
;
315 millival
+= upTime
.lo
/ (1000 * 1000);
316 millival
+= ( ( (UInt64
) upTime
.hi
) << 32 ) / (1000 * 1000);
320 #else // no gettimeofday() nor ftime()
321 // We use wxGetLocalTime() to get the seconds since
322 // 00:00:00 Jan 1st 1970 and then whatever is available
323 // to get millisecond resolution.
325 // NOTE that this might lead to a problem if the clocks
326 // use different sources, so this approach should be
327 // avoided where possible.
329 val
*= wxGetLocalTime();
331 // GRG: This will go soon as all WIN32 seem to have ftime
332 // JACS: unfortunately not. WinCE doesn't have it.
333 #if defined (__WIN32__)
334 // If your platform/compiler needs to use two different functions
335 // to get ms resolution, please do NOT just shut off these warnings,
336 // drop me a line instead at <guille@iies.es>
340 #warning "Possible clock skew bug in wxGetLocalTimeMillis()!"
345 val
+= st
.wMilliseconds
;
347 // If your platform/compiler does not support ms resolution please
348 // do NOT just shut off these warnings, drop me a line instead at
351 #if defined(__VISUALC__) || defined (__WATCOMC__)
352 #pragma message("wxStopWatch will be up to second resolution!")
353 #elif defined(__BORLANDC__)
354 #pragma message "wxStopWatch will be up to second resolution!"
356 #warning "wxStopWatch will be up to second resolution!"
362 #endif // time functions
365 #else // !wxUSE_LONGLONG
367 double wxGetLocalTimeMillis(void)
369 return (double(clock()) / double(CLOCKS_PER_SEC
)) * 1000.0;
372 #endif // wxUSE_LONGLONG/!wxUSE_LONGLONG