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 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
104 void wxStopWatch::Start(long t
)
108 LARGE_INTEGER frequency_li
;
109 ::QueryPerformanceFrequency( &frequency_li
);
110 m_frequency
= frequency_li
.QuadPart
;
111 if (m_frequency
== 0)
113 m_t0
= wxGetLocalTimeMillis() - t
;
117 LARGE_INTEGER counter_li
;
118 ::QueryPerformanceCounter( &counter_li
);
119 wxLongLong counter
= counter_li
.QuadPart
;
120 m_t0
= (counter
* 10000 / m_frequency
) - t
*10;
123 m_t0
= wxGetLocalTimeMillis() - t
;
129 long wxStopWatch::GetElapsedTime() const
133 if (m_frequency
== 0)
135 return (wxGetLocalTimeMillis() - m_t0
).GetLo();
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;
146 return (wxGetLocalTimeMillis() - m_t0
).GetLo();
150 long wxStopWatch::Time() const
152 return m_pauseCount
? m_pause
: GetElapsedTime();
155 #endif // wxUSE_STOPWATCH
157 // ----------------------------------------------------------------------------
158 // old timer functions superceded by wxStopWatch
159 // ----------------------------------------------------------------------------
163 static wxLongLong wxStartTime
= 0l;
165 // starts the global timer
168 wxStartTime
= wxGetLocalTimeMillis();
171 // Returns elapsed time in milliseconds
172 long wxGetElapsedTime(bool resetTimer
)
174 wxLongLong oldTime
= wxStartTime
;
175 wxLongLong newTime
= wxGetLocalTimeMillis();
178 wxStartTime
= newTime
;
180 return (newTime
- oldTime
).GetLo();
183 #endif // wxUSE_LONGLONG
185 // ----------------------------------------------------------------------------
186 // the functions to get the current time and timezone info
187 // ----------------------------------------------------------------------------
189 // Get local time as seconds since 00:00:00, Jan 1st 1970
190 long wxGetLocalTime()
195 // This cannot be made static because mktime can overwrite it.
197 memset(&tm
, 0, sizeof(tm
));
200 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
204 tm
.tm_isdst
= -1; // let mktime guess
206 // Note that mktime assumes that the struct tm contains local time.
208 t1
= time(&t1
); // now
209 t0
= mktime(&tm
); // origin
211 // Return the difference in seconds.
213 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
214 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
216 wxLogSysError(_("Failed to get the local system time"));
220 // Get UTC time as seconds since 00:00:00, Jan 1st 1970
223 return (long)time(NULL
);
228 // Get local time as milliseconds since 00:00:00, Jan 1st 1970
229 wxLongLong
wxGetLocalTimeMillis()
231 wxLongLong val
= 1000l;
233 // If possible, use a function which avoids conversions from
234 // broken-up time structures to milliseconds
236 #if defined(__WXPALMOS__)
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
252 SYSTEMTIME thenst
= { 1970, 1, 4, 1, 0, 0, 0, 0 }; // 00:00:00 Jan 1st 1970
254 SystemTimeToFileTime( &thenst
, &thenft
);
255 wxLongLong
then( thenft
.dwHighDateTime
, thenft
.dwLowDateTime
); // time in 100 nanoseconds
258 GetLocalTime( &nowst
);
260 SystemTimeToFileTime( &nowst
, &nowft
);
261 wxLongLong
now( nowft
.dwHighDateTime
, nowft
.dwLowDateTime
); // time in 100 nanoseconds
263 return ( now
- then
) / 10000.0; // time from 00:00:00 Jan 1st 1970 to now in milliseconds
265 #elif defined(HAVE_GETTIMEOFDAY)
267 if ( wxGetTimeOfDay(&tp
) != -1 )
270 return (val
+ (tp
.tv_usec
/ 1000));
274 wxLogError(_("wxGetTimeOfDay failed."));
277 #elif defined(HAVE_FTIME)
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...)
284 return (val
+ tp
.millitm
);
285 #elif defined(__WXMAC__)
287 static UInt64 gMilliAtStart
= 0;
289 Nanoseconds upTime
= AbsoluteToNanoseconds( UpTime() );
291 if ( gMilliAtStart
== 0 )
293 time_t start
= time(NULL
);
294 gMilliAtStart
= ((UInt64
) start
) * 1000000L;
295 gMilliAtStart
-= upTime
.lo
/ 1000 ;
296 gMilliAtStart
-= ( ( (UInt64
) upTime
.hi
) << 32 ) / (1000 * 1000);
299 UInt64 millival
= gMilliAtStart
;
300 millival
+= upTime
.lo
/ (1000 * 1000);
301 millival
+= ( ( (UInt64
) upTime
.hi
) << 32 ) / (1000 * 1000);
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.
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.
314 val
*= wxGetLocalTime();
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>
325 #warning "Possible clock skew bug in wxGetLocalTimeMillis()!"
330 val
+= st
.wMilliseconds
;
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
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!"
341 #warning "wxStopWatch will be up to second resolution!"
347 #endif // time functions
350 #else // !wxUSE_LONGLONG
352 double wxGetLocalTimeMillis(void)
354 return (double(clock()) / double(CLOCKS_PER_SEC
)) * 1000.0;
357 #endif // wxUSE_LONGLONG/!wxUSE_LONGLONG