]>
git.saurik.com Git - wxWidgets.git/blob - src/common/timercmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/timercmn.cpp
3 // Purpose: Common timer implementation
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)
12 // Copyright: (c) Julian Smart and Markus Holzem
13 // (c) 1999 Guillermo Rodriguez <guille@iies.es>
14 // Licence: wxWindows license
15 /////////////////////////////////////////////////////////////////////////////
17 // ============================================================================
19 // ============================================================================
21 // ----------------------------------------------------------------------------
23 // ----------------------------------------------------------------------------
26 #pragma implementation "timerbase.h"
29 // For compilers that support precompilation, includes "wx.h".
30 #include "wx/wxprec.h"
42 #include "wx/longlong.h"
44 #if defined(__WIN32__)
50 #include <sys/types.h> // for time_t
53 #if defined(HAVE_GETTIMEOFDAY)
56 #elif defined(HAVE_FTIME)
57 #include <sys/timeb.h>
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 IMPLEMENT_DYNAMIC_CLASS(wxTimerEvent
, wxEvent
)
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 // on some really old systems gettimeofday() doesn't have the second argument,
71 // define wxGetTimeOfDay() to hide this difference
72 #ifdef HAVE_GETTIMEOFDAY
73 #ifdef WX_GETTIMEOFDAY_NO_TZ
75 #define wxGetTimeOfDay(tv, tz) gettimeofday(tv)
77 #define wxGetTimeOfDay(tv, tz) gettimeofday((tv), (tz))
79 #endif // HAVE_GETTIMEOFDAY
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 wxLongLong
wxGetLocalTimeMillis();
87 // ============================================================================
89 // ============================================================================
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
95 void wxTimerBase::Notify()
97 // the base class version generates an event if it has owner - which it
98 // should because otherwise nobody can process timer events
99 wxCHECK_RET( m_owner
, _T("wxTimer::Notify() should be overridden.") );
101 wxTimerEvent
event(m_idTimer
, m_milli
);
102 (void)m_owner
->ProcessEvent(event
);
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
109 void wxStopWatch::Start(long t
)
111 m_t0
= wxGetLocalTimeMillis() - t
;
116 long wxStopWatch::GetElapsedTime() const
118 return (wxGetLocalTimeMillis() - m_t0
).GetLo();
121 long wxStopWatch::Time() const
123 return (m_pause
? m_pause
: GetElapsedTime());
126 // ----------------------------------------------------------------------------
127 // old timer functions superceded by wxStopWatch
128 // ----------------------------------------------------------------------------
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();
151 // ----------------------------------------------------------------------------
152 // the functions to get the current time and timezone info
153 // ----------------------------------------------------------------------------
155 // Get local time as seconds since 00:00:00, Jan 1st 1970
156 long wxGetLocalTime()
161 // This cannot be made static because mktime can overwrite it.
163 memset(&tm
, 0, sizeof(tm
));
166 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
170 tm
.tm_isdst
= -1; // let mktime guess
172 // Note that mktime assumes that the struct tm contains local time.
174 t1
= time(&t1
); // now
175 t0
= mktime(&tm
); // origin
177 // Return the difference in seconds.
179 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
180 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
182 wxLogSysError(_("Failed to get the local system time"));
186 // 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(_("Failed 2nd mktime"));
224 wxLogSysError(_("Failed gmtime"));
226 wxLogSysError(_("Failed to get the UTC system time"));
231 // Get local time as milliseconds since 00:00:00, Jan 1st 1970
232 wxLongLong
wxGetLocalTimeMillis()
234 // We use wxGetLocalTime() to get the seconds since
235 // 00:00:00 Jan 1st 1970 and then whatever is available
236 // to get millisecond resolution.
238 wxLongLong val
= 1000l;
239 val
*= wxGetLocalTime();
241 // If we got here, do not fail even if we can't get
242 // millisecond resolution.
244 #if defined(__WIN32__)
247 return (val
+ st
.wMilliseconds
);
248 #elif defined(HAVE_GETTIMEOFDAY)
250 if ( wxGetTimeOfDay(&tp
, (struct timezone
*)NULL
) != -1 )
252 return (val
+ (tp
.tv_usec
/ 1000));
254 #elif defined(HAVE_FTIME)
256 if ( ftime(&tp
) == 0 )
258 return (val
+ tp
.millitm
);
261 #if !defined(__BORLANDC__) && !(defined(__VISUALC__) && defined(__WIN16__))
262 #warning "wxStopWatch will be up to second resolution!"