]>
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"
37 #include "wx/longlong.h"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 #if defined(__WIN32__)
47 #if defined(__WIN32__) && !defined(HAVE_FTIME) && !defined(__MWERKS__)
51 #if defined(__VISAGECPP__) && !defined(HAVE_FTIME)
53 # if __IBMCPP__ >= 400
54 # define ftime(x) _ftime(x)
58 #if defined(__MWERKS__) && defined(__WXMSW__)
60 # undef HAVE_GETTIMEOFDAY
65 #include <sys/types.h> // for time_t
68 #if defined(HAVE_GETTIMEOFDAY)
71 #elif defined(HAVE_FTIME)
72 #include <sys/timeb.h>
77 #include <DriverServices.h>
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 // on some really old systems gettimeofday() doesn't have the second argument,
85 // define wxGetTimeOfDay() to hide this difference
86 #ifdef HAVE_GETTIMEOFDAY
87 #ifdef WX_GETTIMEOFDAY_NO_TZ
89 #define wxGetTimeOfDay(tv, tz) gettimeofday(tv)
91 #define wxGetTimeOfDay(tv, tz) gettimeofday((tv), (tz))
93 #endif // HAVE_GETTIMEOFDAY
95 // ============================================================================
97 // ============================================================================
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
105 void wxStopWatch::Start(long t
)
107 m_t0
= wxGetLocalTimeMillis() - t
;
112 long wxStopWatch::GetElapsedTime() const
114 return (wxGetLocalTimeMillis() - m_t0
).GetLo();
117 long wxStopWatch::Time() const
119 return m_pauseCount
? m_pause
: GetElapsedTime();
122 #endif // wxUSE_STOPWATCH
124 // ----------------------------------------------------------------------------
125 // old timer functions superceded by wxStopWatch
126 // ----------------------------------------------------------------------------
130 static wxLongLong wxStartTime
= 0l;
132 // starts the global timer
135 wxStartTime
= wxGetLocalTimeMillis();
138 // Returns elapsed time in milliseconds
139 long wxGetElapsedTime(bool resetTimer
)
141 wxLongLong oldTime
= wxStartTime
;
142 wxLongLong newTime
= wxGetLocalTimeMillis();
145 wxStartTime
= newTime
;
147 return (newTime
- oldTime
).GetLo();
150 #endif // wxUSE_LONGLONG
152 // ----------------------------------------------------------------------------
153 // the functions to get the current time and timezone info
154 // ----------------------------------------------------------------------------
156 // Get local time as seconds since 00:00:00, Jan 1st 1970
157 long wxGetLocalTime()
162 // This cannot be made static because mktime can overwrite it.
164 memset(&tm
, 0, sizeof(tm
));
167 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
171 tm
.tm_isdst
= -1; // let mktime guess
173 // Note that mktime assumes that the struct tm contains local time.
175 t1
= time(&t1
); // now
176 t0
= mktime(&tm
); // origin
178 // Return the difference in seconds.
180 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
181 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
183 wxLogSysError(_("Failed to get the local system time"));
187 // Get UTC time as seconds since 00:00:00, Jan 1st 1970
194 // This cannot be made static because mktime can overwrite it
196 memset(&tm
, 0, sizeof(tm
));
199 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
203 tm
.tm_isdst
= -1; // let mktime guess
205 // Note that mktime assumes that the struct tm contains local time.
207 t1
= time(&t1
); // now
208 t0
= mktime(&tm
); // origin in localtime
210 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
212 // To get t0 as GMT we convert to a struct tm with gmtime,
213 // and then back again.
219 memcpy(&tm
, ptm
, sizeof(tm
));
222 if (t0
!= (time_t)-1 )
223 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
224 wxLogSysError(_("mktime() failed"));
228 wxLogSysError(_("gmtime() failed"));
232 wxLogError(_("Failed to get the UTC system time."));
239 // Get local time as milliseconds since 00:00:00, Jan 1st 1970
240 wxLongLong
wxGetLocalTimeMillis()
242 wxLongLong val
= 1000l;
244 // If possible, use a function which avoids conversions from
245 // broken-up time structures to milliseconds
247 #if defined(__WXMSW__) && defined(__MWERKS__)
248 // This should probably be the way all WXMSW compilers should do it
249 // Go direct to the OS for time
251 SYSTEMTIME thenst
= { 1970, 1, 4, 1, 0, 0, 0, 0 }; // 00:00:00 Jan 1st 1970
253 SystemTimeToFileTime( &thenst
, &thenft
);
254 wxLongLong
then( thenft
.dwHighDateTime
, thenft
.dwLowDateTime
); // time in 100 nanoseconds
257 GetLocalTime( &nowst
);
259 SystemTimeToFileTime( &nowst
, &nowft
);
260 wxLongLong
now( nowft
.dwHighDateTime
, nowft
.dwLowDateTime
); // time in 100 nanoseconds
262 return ( now
- then
) / 10000.0; // time from 00:00:00 Jan 1st 1970 to now in milliseconds
264 #elif defined(HAVE_GETTIMEOFDAY)
266 if ( wxGetTimeOfDay(&tp
, (struct timezone
*)NULL
) != -1 )
269 return (val
+ (tp
.tv_usec
/ 1000));
273 wxLogError(_("wxGetTimeOfDay failed."));
276 #elif defined(HAVE_FTIME)
279 // ftime() is void and not int in some mingw32 headers, so don't
280 // test the return code (well, it shouldn't fail anyhow...)
283 return (val
+ tp
.millitm
);
284 #elif defined(__WXMAC__)
286 static UInt64 gMilliAtStart
= 0;
288 Nanoseconds upTime
= AbsoluteToNanoseconds( UpTime() );
290 if ( gMilliAtStart
== 0 )
292 time_t start
= time(NULL
);
293 gMilliAtStart
= ((UInt64
) start
) * 1000000L;
294 gMilliAtStart
-= upTime
.lo
/ 1000 ;
295 gMilliAtStart
-= ( ( (UInt64
) upTime
.hi
) << 32 ) / (1000 * 1000);
298 UInt64 millival
= gMilliAtStart
;
299 millival
+= upTime
.lo
/ (1000 * 1000);
300 millival
+= ( ( (UInt64
) upTime
.hi
) << 32 ) / (1000 * 1000);
304 #else // no gettimeofday() nor ftime()
305 // We use wxGetLocalTime() to get the seconds since
306 // 00:00:00 Jan 1st 1970 and then whatever is available
307 // to get millisecond resolution.
309 // NOTE that this might lead to a problem if the clocks
310 // use different sources, so this approach should be
311 // avoided where possible.
313 val
*= wxGetLocalTime();
315 // GRG: This will go soon as all WIN32 seem to have ftime
316 #if defined (__WIN32__)
317 // If your platform/compiler needs to use two different functions
318 // to get ms resolution, please do NOT just shut off these warnings,
319 // drop me a line instead at <guille@iies.es>
320 #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 #endif // wxUSE_LONGLONG