]>
git.saurik.com Git - wxWidgets.git/blob - src/common/stopwatch.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: 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 wxWindows Team
13 // License: wxWindows license
14 ///////////////////////////////////////////////////////////////////////////////
16 // ============================================================================
18 // ============================================================================
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
24 // for compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
35 #include "wx/longlong.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 #if defined(__WIN32__)
45 #if defined(__WIN32__) && !defined(HAVE_FTIME) && !defined(__MWERKS__)
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
63 #include <sys/types.h> // for time_t
66 #if defined(HAVE_GETTIMEOFDAY)
69 #elif defined(HAVE_FTIME)
70 #include <sys/timeb.h>
75 #include <DriverServices.h>
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 // on some really old systems gettimeofday() doesn't have the second argument,
83 // define wxGetTimeOfDay() to hide this difference
84 #ifdef HAVE_GETTIMEOFDAY
85 #ifdef WX_GETTIMEOFDAY_NO_TZ
87 #define wxGetTimeOfDay(tv, tz) gettimeofday(tv)
89 #define wxGetTimeOfDay(tv, tz) gettimeofday((tv), (tz))
91 #endif // HAVE_GETTIMEOFDAY
93 // ============================================================================
95 // ============================================================================
97 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
103 void wxStopWatch::Start(long t
)
105 m_t0
= wxGetLocalTimeMillis() - t
;
110 long wxStopWatch::GetElapsedTime() const
112 return (wxGetLocalTimeMillis() - m_t0
).GetLo();
115 long wxStopWatch::Time() const
117 return m_pauseCount
? m_pause
: GetElapsedTime();
120 #endif // wxUSE_STOPWATCH
122 // ----------------------------------------------------------------------------
123 // old timer functions superceded by wxStopWatch
124 // ----------------------------------------------------------------------------
128 static wxLongLong wxStartTime
= 0l;
130 // starts the global timer
133 wxStartTime
= wxGetLocalTimeMillis();
136 // Returns elapsed time in milliseconds
137 long wxGetElapsedTime(bool resetTimer
)
139 wxLongLong oldTime
= wxStartTime
;
140 wxLongLong newTime
= wxGetLocalTimeMillis();
143 wxStartTime
= newTime
;
145 return (newTime
- oldTime
).GetLo();
148 #endif // wxUSE_LONGLONG
150 // ----------------------------------------------------------------------------
151 // the functions to get the current time and timezone info
152 // ----------------------------------------------------------------------------
154 // Get local time as seconds since 00:00:00, Jan 1st 1970
155 long wxGetLocalTime()
160 // This cannot be made static because mktime can overwrite it.
162 memset(&tm
, 0, sizeof(tm
));
165 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
169 tm
.tm_isdst
= -1; // let mktime guess
171 // Note that mktime assumes that the struct tm contains local time.
173 t1
= time(&t1
); // now
174 t0
= mktime(&tm
); // origin
176 // Return the difference in seconds.
178 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
179 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
181 wxLogSysError(_("Failed to get the local system time"));
185 // Get UTC time as seconds since 00:00:00, Jan 1st 1970
192 // This cannot be made static because mktime can overwrite it
194 memset(&tm
, 0, sizeof(tm
));
197 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
201 tm
.tm_isdst
= -1; // let mktime guess
203 // Note that mktime assumes that the struct tm contains local time.
205 t1
= time(&t1
); // now
206 t0
= mktime(&tm
); // origin in localtime
208 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
210 // To get t0 as GMT we convert to a struct tm with gmtime,
211 // and then back again.
217 memcpy(&tm
, ptm
, sizeof(tm
));
220 if (t0
!= (time_t)-1 )
221 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
222 wxLogSysError(_("mktime() failed"));
226 wxLogSysError(_("gmtime() failed"));
230 wxLogError(_("Failed to get the UTC system time."));
237 // Get local time as milliseconds since 00:00:00, Jan 1st 1970
238 wxLongLong
wxGetLocalTimeMillis()
240 wxLongLong val
= 1000l;
242 // If possible, use a function which avoids conversions from
243 // broken-up time structures to milliseconds
245 #if defined(__WXMSW__) && defined(__MWERKS__)
246 // This should probably be the way all WXMSW compilers should do it
247 // Go direct to the OS for time
249 SYSTEMTIME thenst
= { 1970, 1, 4, 1, 0, 0, 0, 0 }; // 00:00:00 Jan 1st 1970
251 SystemTimeToFileTime( &thenst
, &thenft
);
252 wxLongLong
then( thenft
.dwHighDateTime
, thenft
.dwLowDateTime
); // time in 100 nanoseconds
255 GetLocalTime( &nowst
);
257 SystemTimeToFileTime( &nowst
, &nowft
);
258 wxLongLong
now( nowft
.dwHighDateTime
, nowft
.dwLowDateTime
); // time in 100 nanoseconds
260 return ( now
- then
) / 10000.0; // time from 00:00:00 Jan 1st 1970 to now in milliseconds
262 #elif defined(HAVE_GETTIMEOFDAY)
264 if ( wxGetTimeOfDay(&tp
, (struct timezone
*)NULL
) != -1 )
267 return (val
+ (tp
.tv_usec
/ 1000));
271 wxLogError(_("wxGetTimeOfDay failed."));
274 #elif defined(HAVE_FTIME)
277 // ftime() is void and not int in some mingw32 headers, so don't
278 // test the return code (well, it shouldn't fail anyhow...)
281 return (val
+ tp
.millitm
);
282 #elif defined(__WXMAC__)
284 static UInt64 gMilliAtStart
= 0;
286 Nanoseconds upTime
= AbsoluteToNanoseconds( UpTime() );
288 if ( gMilliAtStart
== 0 )
290 time_t start
= time(NULL
);
291 gMilliAtStart
= ((UInt64
) start
) * 1000000L;
292 gMilliAtStart
-= upTime
.lo
/ 1000 ;
293 gMilliAtStart
-= ( ( (UInt64
) upTime
.hi
) << 32 ) / (1000 * 1000);
296 UInt64 millival
= gMilliAtStart
;
297 millival
+= upTime
.lo
/ (1000 * 1000);
298 millival
+= ( ( (UInt64
) upTime
.hi
) << 32 ) / (1000 * 1000);
302 #else // no gettimeofday() nor ftime()
303 // We use wxGetLocalTime() to get the seconds since
304 // 00:00:00 Jan 1st 1970 and then whatever is available
305 // to get millisecond resolution.
307 // NOTE that this might lead to a problem if the clocks
308 // use different sources, so this approach should be
309 // avoided where possible.
311 val
*= wxGetLocalTime();
313 // GRG: This will go soon as all WIN32 seem to have ftime
314 #if defined (__WIN32__)
315 // If your platform/compiler needs to use two different functions
316 // to get ms resolution, please do NOT just shut off these warnings,
317 // drop me a line instead at <guille@iies.es>
318 #warning "Possible clock skew bug in wxGetLocalTimeMillis()!"
322 val
+= st
.wMilliseconds
;
324 // If your platform/compiler does not support ms resolution please
325 // do NOT just shut off these warnings, drop me a line instead at
328 #if defined(__VISUALC__) || defined (__WATCOMC__)
329 #pragma message("wxStopWatch will be up to second resolution!")
330 #elif defined(__BORLANDC__)
331 #pragma message "wxStopWatch will be up to second resolution!"
333 #warning "wxStopWatch will be up to second resolution!"
339 #endif // time functions
342 #endif // wxUSE_LONGLONG