]>
git.saurik.com Git - wxWidgets.git/blob - src/common/stopwatch.cpp
f6f5638032b4e0ce8df0911bd8804c8671088c5d
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__)
44 #include "wx/msw/wrapwin.h"
47 #if defined(__WIN32__) && !defined(HAVE_FTIME) && !defined(__MWERKS__) && !defined(__WXWINCE__)
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
66 #include "wx/msw/private.h"
67 #include "wx/msw/wince/time.h"
70 #if !defined(__WXMAC__) && !defined(__WXWINCE__)
71 #include <sys/types.h> // for time_t
74 #if defined(HAVE_GETTIMEOFDAY)
77 #elif defined(HAVE_FTIME)
78 #include <sys/timeb.h>
83 #include <DriverServices.h>
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 // on some really old systems gettimeofday() doesn't have the second argument,
91 // define wxGetTimeOfDay() to hide this difference
92 #ifdef HAVE_GETTIMEOFDAY
93 #ifdef WX_GETTIMEOFDAY_NO_TZ
95 #define wxGetTimeOfDay(tv, tz) gettimeofday(tv)
97 #define wxGetTimeOfDay(tv, tz) gettimeofday((tv), (tz))
99 #endif // HAVE_GETTIMEOFDAY
101 // ============================================================================
103 // ============================================================================
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
111 void wxStopWatch::Start(long t
)
113 m_t0
= wxGetLocalTimeMillis() - t
;
118 long wxStopWatch::GetElapsedTime() const
120 return (wxGetLocalTimeMillis() - m_t0
).GetLo();
123 long wxStopWatch::Time() const
125 return m_pauseCount
? m_pause
: GetElapsedTime();
128 #endif // wxUSE_STOPWATCH
130 // ----------------------------------------------------------------------------
131 // old timer functions superceded by wxStopWatch
132 // ----------------------------------------------------------------------------
136 static wxLongLong wxStartTime
= 0l;
138 // starts the global timer
141 wxStartTime
= wxGetLocalTimeMillis();
144 // Returns elapsed time in milliseconds
145 long wxGetElapsedTime(bool resetTimer
)
147 wxLongLong oldTime
= wxStartTime
;
148 wxLongLong newTime
= wxGetLocalTimeMillis();
151 wxStartTime
= newTime
;
153 return (newTime
- oldTime
).GetLo();
156 #endif // wxUSE_LONGLONG
158 // ----------------------------------------------------------------------------
159 // the functions to get the current time and timezone info
160 // ----------------------------------------------------------------------------
162 // Get local time as seconds since 00:00:00, Jan 1st 1970
163 long wxGetLocalTime()
168 // This cannot be made static because mktime can overwrite it.
170 memset(&tm
, 0, sizeof(tm
));
173 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
177 tm
.tm_isdst
= -1; // let mktime guess
179 // Note that mktime assumes that the struct tm contains local time.
181 t1
= time(&t1
); // now
182 t0
= mktime(&tm
); // origin
184 // Return the difference in seconds.
186 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
187 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
189 wxLogSysError(_("Failed to get the local system time"));
193 // Get UTC time as seconds since 00:00:00, Jan 1st 1970
200 // This cannot be made static because mktime can overwrite it
202 memset(&tm
, 0, sizeof(tm
));
205 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
209 tm
.tm_isdst
= -1; // let mktime guess
211 // Note that mktime assumes that the struct tm contains local time.
213 t1
= time(&t1
); // now
214 t0
= mktime(&tm
); // origin in localtime
216 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
218 // To get t0 as GMT we convert to a struct tm with gmtime,
219 // and then back again.
225 memcpy(&tm
, ptm
, sizeof(tm
));
228 if (t0
!= (time_t)-1 )
229 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
230 wxLogSysError(_("mktime() failed"));
234 wxLogSysError(_("gmtime() failed"));
238 wxLogError(_("Failed to get the UTC system time."));
245 // Get local time as milliseconds since 00:00:00, Jan 1st 1970
246 wxLongLong
wxGetLocalTimeMillis()
248 wxLongLong val
= 1000l;
250 // If possible, use a function which avoids conversions from
251 // broken-up time structures to milliseconds
253 #if defined(__WXMSW__) && defined(__MWERKS__)
254 // This should probably be the way all WXMSW compilers should do it
255 // Go direct to the OS for time
257 SYSTEMTIME thenst
= { 1970, 1, 4, 1, 0, 0, 0, 0 }; // 00:00:00 Jan 1st 1970
259 SystemTimeToFileTime( &thenst
, &thenft
);
260 wxLongLong
then( thenft
.dwHighDateTime
, thenft
.dwLowDateTime
); // time in 100 nanoseconds
263 GetLocalTime( &nowst
);
265 SystemTimeToFileTime( &nowst
, &nowft
);
266 wxLongLong
now( nowft
.dwHighDateTime
, nowft
.dwLowDateTime
); // time in 100 nanoseconds
268 return ( now
- then
) / 10000.0; // time from 00:00:00 Jan 1st 1970 to now in milliseconds
270 #elif defined(HAVE_GETTIMEOFDAY)
272 if ( wxGetTimeOfDay(&tp
, (struct timezone
*)NULL
) != -1 )
275 return (val
+ (tp
.tv_usec
/ 1000));
279 wxLogError(_("wxGetTimeOfDay failed."));
282 #elif defined(HAVE_FTIME)
285 // ftime() is void and not int in some mingw32 headers, so don't
286 // test the return code (well, it shouldn't fail anyhow...)
289 return (val
+ tp
.millitm
);
290 #elif defined(__WXMAC__)
292 static UInt64 gMilliAtStart
= 0;
294 Nanoseconds upTime
= AbsoluteToNanoseconds( UpTime() );
296 if ( gMilliAtStart
== 0 )
298 time_t start
= time(NULL
);
299 gMilliAtStart
= ((UInt64
) start
) * 1000000L;
300 gMilliAtStart
-= upTime
.lo
/ 1000 ;
301 gMilliAtStart
-= ( ( (UInt64
) upTime
.hi
) << 32 ) / (1000 * 1000);
304 UInt64 millival
= gMilliAtStart
;
305 millival
+= upTime
.lo
/ (1000 * 1000);
306 millival
+= ( ( (UInt64
) upTime
.hi
) << 32 ) / (1000 * 1000);
310 #else // no gettimeofday() nor ftime()
311 // We use wxGetLocalTime() to get the seconds since
312 // 00:00:00 Jan 1st 1970 and then whatever is available
313 // to get millisecond resolution.
315 // NOTE that this might lead to a problem if the clocks
316 // use different sources, so this approach should be
317 // avoided where possible.
319 val
*= wxGetLocalTime();
321 // GRG: This will go soon as all WIN32 seem to have ftime
322 // JACS: unfortunately not. WinCE doesn't have it.
323 #if defined (__WIN32__)
324 // If your platform/compiler needs to use two different functions
325 // to get ms resolution, please do NOT just shut off these warnings,
326 // drop me a line instead at <guille@iies.es>
330 #warning "Possible clock skew bug in wxGetLocalTimeMillis()!"
335 val
+= st
.wMilliseconds
;
337 // If your platform/compiler does not support ms resolution please
338 // do NOT just shut off these warnings, drop me a line instead at
341 #if defined(__VISUALC__) || defined (__WATCOMC__)
342 #pragma message("wxStopWatch will be up to second resolution!")
343 #elif defined(__BORLANDC__)
344 #pragma message "wxStopWatch will be up to second resolution!"
346 #warning "wxStopWatch will be up to second resolution!"
352 #endif // time functions
355 #endif // wxUSE_LONGLONG