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 // ----------------------------------------------------------------------------
98 void wxStopWatch::Start(long t
)
102 LARGE_INTEGER frequency_li
;
103 ::QueryPerformanceFrequency( &frequency_li
);
104 m_frequency
= frequency_li
.QuadPart
;
105 if (m_frequency
== 0)
107 m_t0
= wxGetLocalTimeMillis() - t
;
111 LARGE_INTEGER counter_li
;
112 ::QueryPerformanceCounter( &counter_li
);
113 wxLongLong counter
= counter_li
.QuadPart
;
114 m_t0
= (counter
* 10000 / m_frequency
) - t
*10;
117 m_t0
= wxGetLocalTimeMillis() - t
;
123 long wxStopWatch::GetElapsedTime() const
127 if (m_frequency
== 0)
129 return (wxGetLocalTimeMillis() - m_t0
).GetLo();
133 LARGE_INTEGER counter_li
;
134 ::QueryPerformanceCounter( &counter_li
);
135 wxLongLong counter
= counter_li
.QuadPart
;
136 wxLongLong res
= (counter
* 10000 / m_frequency
) - m_t0
;
137 return res
.GetLo() / 10;
140 return (wxGetLocalTimeMillis() - m_t0
).GetLo();
144 long wxStopWatch::Time() const
146 return m_pauseCount
? m_pause
: GetElapsedTime();
149 #endif // wxUSE_STOPWATCH
151 // ----------------------------------------------------------------------------
152 // old timer functions superceded by wxStopWatch
153 // ----------------------------------------------------------------------------
157 static wxLongLong wxStartTime
= 0l;
159 // starts the global timer
162 wxStartTime
= wxGetLocalTimeMillis();
165 // Returns elapsed time in milliseconds
166 long wxGetElapsedTime(bool resetTimer
)
168 wxLongLong oldTime
= wxStartTime
;
169 wxLongLong newTime
= wxGetLocalTimeMillis();
172 wxStartTime
= newTime
;
174 return (newTime
- oldTime
).GetLo();
177 #endif // wxUSE_LONGLONG
179 // ----------------------------------------------------------------------------
180 // the functions to get the current time and timezone info
181 // ----------------------------------------------------------------------------
183 // Get local time as seconds since 00:00:00, Jan 1st 1970
184 long wxGetLocalTime()
189 // This cannot be made static because mktime can overwrite it.
191 memset(&tm
, 0, sizeof(tm
));
194 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
198 tm
.tm_isdst
= -1; // let mktime guess
200 // Note that mktime assumes that the struct tm contains local time.
202 t1
= time(&t1
); // now
203 t0
= mktime(&tm
); // origin
205 // Return the difference in seconds.
207 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
208 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
210 wxLogSysError(_("Failed to get the local system time"));
214 // Get UTC time as seconds since 00:00:00, Jan 1st 1970
217 return (long)time(NULL
);
222 // Get local time as milliseconds since 00:00:00, Jan 1st 1970
223 wxLongLong
wxGetLocalTimeMillis()
225 wxLongLong val
= 1000l;
227 // If possible, use a function which avoids conversions from
228 // broken-up time structures to milliseconds
230 #if defined(__WXPALMOS__)
239 uint32_t now
= TimGetSeconds();
240 uint32_t then
= TimDateTimeToSeconds (&thenst
);
241 return SysTimeToMilliSecs(SysTimeInSecs(now
- then
));
242 #elif defined(__WXMSW__) && (defined(__WINE__) || defined(__MWERKS__))
243 // This should probably be the way all WXMSW compilers should do it
244 // Go direct to the OS for time
246 SYSTEMTIME thenst
= { 1970, 1, 4, 1, 0, 0, 0, 0 }; // 00:00:00 Jan 1st 1970
248 SystemTimeToFileTime( &thenst
, &thenft
);
249 wxLongLong
then( thenft
.dwHighDateTime
, thenft
.dwLowDateTime
); // time in 100 nanoseconds
252 GetLocalTime( &nowst
);
254 SystemTimeToFileTime( &nowst
, &nowft
);
255 wxLongLong
now( nowft
.dwHighDateTime
, nowft
.dwLowDateTime
); // time in 100 nanoseconds
257 return ( now
- then
) / 10000.0; // time from 00:00:00 Jan 1st 1970 to now in milliseconds
259 #elif defined(HAVE_GETTIMEOFDAY)
261 if ( wxGetTimeOfDay(&tp
) != -1 )
264 return (val
+ (tp
.tv_usec
/ 1000));
268 wxLogError(_("wxGetTimeOfDay failed."));
271 #elif defined(HAVE_FTIME)
274 // ftime() is void and not int in some mingw32 headers, so don't
275 // test the return code (well, it shouldn't fail anyhow...)
278 return (val
+ tp
.millitm
);
279 #else // no gettimeofday() nor ftime()
280 // We use wxGetLocalTime() to get the seconds since
281 // 00:00:00 Jan 1st 1970 and then whatever is available
282 // to get millisecond resolution.
284 // NOTE that this might lead to a problem if the clocks
285 // use different sources, so this approach should be
286 // avoided where possible.
288 val
*= wxGetLocalTime();
290 // GRG: This will go soon as all WIN32 seem to have ftime
291 // JACS: unfortunately not. WinCE doesn't have it.
292 #if defined (__WIN32__)
293 // If your platform/compiler needs to use two different functions
294 // to get ms resolution, please do NOT just shut off these warnings,
295 // drop me a line instead at <guille@iies.es>
299 #warning "Possible clock skew bug in wxGetLocalTimeMillis()!"
304 val
+= st
.wMilliseconds
;
306 // If your platform/compiler does not support ms resolution please
307 // do NOT just shut off these warnings, drop me a line instead at
310 #if defined(__VISUALC__) || defined (__WATCOMC__)
311 #pragma message("wxStopWatch will be up to second resolution!")
312 #elif defined(__BORLANDC__)
313 #pragma message "wxStopWatch will be up to second resolution!"
315 #warning "wxStopWatch will be up to second resolution!"
321 #endif // time functions
324 #else // !wxUSE_LONGLONG
326 double wxGetLocalTimeMillis(void)
328 return (double(clock()) / double(CLOCKS_PER_SEC
)) * 1000.0;
331 #endif // wxUSE_LONGLONG/!wxUSE_LONGLONG