1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/stopwatch.h
3 // Purpose: wxStopWatch and global time-related functions
4 // Author: Julian Smart (wxTimer), Sylvain Bougnoux (wxStopWatch)
5 // Created: 26.06.03 (extracted from wx/timer.h)
7 // Copyright: (c) 1998-2003 Julian Smart, Sylvain Bougnoux
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_STOPWATCH_H_
12 #define _WX_STOPWATCH_H_
15 #include "wx/longlong.h"
17 // ----------------------------------------------------------------------------
18 // wxStopWatch: measure time intervals with up to 1ms resolution
19 // ----------------------------------------------------------------------------
23 class WXDLLIMPEXP_BASE wxStopWatch
26 // ctor starts the stop watch
27 wxStopWatch() { m_pauseCount
= 0; Start(); }
29 // Start the stop watch at the moment t0 expressed in milliseconds (i.e.
30 // calling Time() immediately afterwards returns t0). This can be used to
31 // restart an existing stopwatch.
32 void Start(long t0
= 0);
34 // pause the stop watch
37 if ( m_pauseCount
++ == 0 )
38 m_elapsedBeforePause
= GetCurrentClockValue() - m_t0
;
44 wxASSERT_MSG( m_pauseCount
> 0,
45 wxT("Resuming stop watch which is not paused") );
47 if ( --m_pauseCount
== 0 )
50 m_t0
-= m_elapsedBeforePause
;
54 // Get elapsed time since the last Start() in microseconds.
55 wxLongLong
TimeInMicro() const;
57 // get elapsed time since the last Start() in milliseconds
58 long Time() const { return (TimeInMicro()/1000).ToLong(); }
61 // Really starts the stop watch. The initial time is set to current clock
65 // Returns the current clock value in its native units.
66 wxLongLong
GetCurrentClockValue() const;
68 // Return the frequency of the clock used in its ticks per second.
69 wxLongLong
GetClockFreq() const;
72 // The clock value when the stop watch was last started. Its units vary
73 // depending on the platform.
76 // The elapsed time as of last Pause() call (only valid if m_pauseCount >
77 // 0) in the same units as m_t0.
78 wxLongLong m_elapsedBeforePause
;
80 // if > 0, the stop watch is paused, otherwise it is running
84 #endif // wxUSE_STOPWATCH
86 #if wxUSE_LONGLONG && WXWIN_COMPATIBILITY_2_6
88 // Starts a global timer
89 // -- DEPRECATED: use wxStopWatch instead
90 wxDEPRECATED( void WXDLLIMPEXP_BASE
wxStartTimer() );
92 // Gets elapsed milliseconds since last wxStartTimer or wxGetElapsedTime
93 // -- DEPRECATED: use wxStopWatch instead
94 wxDEPRECATED( long WXDLLIMPEXP_BASE
wxGetElapsedTime(bool resetTimer
= true) );
96 #endif // wxUSE_LONGLONG && WXWIN_COMPATIBILITY_2_6
98 // ----------------------------------------------------------------------------
99 // global time functions
100 // ----------------------------------------------------------------------------
102 // Get number of seconds since local time 00:00:00 Jan 1st 1970.
103 extern long WXDLLIMPEXP_BASE
wxGetLocalTime();
105 // Get number of seconds since GMT 00:00:00, Jan 1st 1970.
106 extern long WXDLLIMPEXP_BASE
wxGetUTCTime();
109 typedef wxLongLong wxMilliClock_t
;
110 inline long wxMilliClockToLong(wxLongLong ll
) { return ll
.ToLong(); }
112 typedef double wxMilliClock_t
;
113 inline long wxMilliClockToLong(double d
) { return wx_truncate_cast(long, d
); }
114 #endif // wxUSE_LONGLONG
116 // Get number of milliseconds since local time 00:00:00 Jan 1st 1970
117 extern wxMilliClock_t WXDLLIMPEXP_BASE
wxGetLocalTimeMillis();
119 #define wxGetCurrentTime() wxGetLocalTime()
121 // on some really old systems gettimeofday() doesn't have the second argument,
122 // define wxGetTimeOfDay() to hide this difference
123 #ifdef HAVE_GETTIMEOFDAY
124 #ifdef WX_GETTIMEOFDAY_NO_TZ
125 #define wxGetTimeOfDay(tv) gettimeofday(tv)
127 #define wxGetTimeOfDay(tv) gettimeofday((tv), NULL)
129 #endif // HAVE_GETTIMEOFDAY
131 #endif // _WX_STOPWATCH_H_