]>
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 // ----------------------------------------------------------------------------
65 IMPLEMENT_DYNAMIC_CLASS(wxTimerEvent
, wxEvent
)
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 // on some really old systems gettimeofday() doesn't have the second argument,
73 // define wxGetTimeOfDay() to hide this difference
74 #ifdef HAVE_GETTIMEOFDAY
75 #ifdef WX_GETTIMEOFDAY_NO_TZ
77 #define wxGetTimeOfDay(tv, tz) gettimeofday(tv)
79 #define wxGetTimeOfDay(tv, tz) gettimeofday((tv), (tz))
81 #endif // HAVE_GETTIMEOFDAY
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
87 wxLongLong
wxGetLocalTimeMillis();
89 // ============================================================================
91 // ============================================================================
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
99 void wxTimerBase::Notify()
101 // the base class version generates an event if it has owner - which it
102 // should because otherwise nobody can process timer events
103 wxCHECK_RET( m_owner
, _T("wxTimer::Notify() should be overridden.") );
105 wxTimerEvent
event(m_idTimer
, m_milli
);
106 (void)m_owner
->ProcessEvent(event
);
111 // ----------------------------------------------------------------------------
113 // ----------------------------------------------------------------------------
115 void wxStopWatch::Start(long t
)
117 m_t0
= wxGetLocalTimeMillis() - t
;
121 long wxStopWatch::GetElapsedTime() const
123 return (wxGetLocalTimeMillis() - m_t0
).GetLo();
126 long wxStopWatch::Time() const
128 return (m_pause
? m_pause
: GetElapsedTime());
131 // ----------------------------------------------------------------------------
132 // old timer functions superceded by wxStopWatch
133 // ----------------------------------------------------------------------------
135 static wxLongLong wxStartTime
= 0l;
137 // starts the global timer
140 wxStartTime
= wxGetLocalTimeMillis();
143 // Returns elapsed time in milliseconds
144 long wxGetElapsedTime(bool resetTimer
)
146 wxLongLong oldTime
= wxStartTime
;
147 wxLongLong newTime
= wxGetLocalTimeMillis();
150 wxStartTime
= newTime
;
152 return (newTime
- oldTime
).GetLo();
156 // ----------------------------------------------------------------------------
157 // the functions to get the current time and timezone info
158 // ----------------------------------------------------------------------------
160 // Get local time as seconds since 00:00:00, Jan 1st 1970
161 long wxGetLocalTime()
166 // This cannot be made static because mktime can overwrite it.
168 memset(&tm
, 0, sizeof(tm
));
171 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
175 tm
.tm_isdst
= -1; // let mktime guess
177 // Note that mktime assumes that the struct tm contains local time.
179 t1
= time(&t1
); // now
180 t0
= mktime(&tm
); // origin
182 // Return the difference in seconds.
184 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
185 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
187 wxLogSysError(_("Failed to get the local system time"));
191 // Get UTC time as seconds since 00:00:00, Jan 1st 1970
197 // This cannot be made static because mktime can overwrite it
199 memset(&tm
, 0, sizeof(tm
));
202 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
206 tm
.tm_isdst
= -1; // let mktime guess
208 // Note that mktime assumes that the struct tm contains local time.
210 t1
= time(&t1
); // now
211 t0
= mktime(&tm
); // origin in localtime
213 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
215 // To get t0 as GMT we convert to a struct tm with gmtime,
216 // and then back again.
222 memcpy(&tm
, ptm
, sizeof(tm
));
225 if (t0
!= (time_t)-1 )
226 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
227 wxLogSysError(_("Failed 2nd mktime"));
229 wxLogSysError(_("Failed gmtime"));
231 wxLogSysError(_("Failed to get the UTC system time"));
236 // Get local time as milliseconds since 00:00:00, Jan 1st 1970
237 wxLongLong
wxGetLocalTimeMillis()
239 wxLongLong val
= 1000l;
241 #if defined(HAVE_GETTIMEOFDAY)
243 if ( wxGetTimeOfDay(&tp
, (struct timezone
*)NULL
) != -1 )
246 return (val
+ (tp
.tv_usec
/ 1000));
250 // We use wxGetLocalTime() to get the seconds since
251 // 00:00:00 Jan 1st 1970 and then whatever is available
252 // to get millisecond resolution.
253 // THIS LEADS TO A BUG SINCE REFERENCE TIME ARE DIFFERENT
254 val
*= wxGetLocalTime();
256 // If we got here, do not fail even if we can't get
257 // millisecond resolution.
259 #if defined(__WIN32__)
262 return (val
+ st
.wMilliseconds
);
263 #elif defined(__VISAGECPP__)
265 ::DosGetDateTime(&dt
);
266 return (val
+ dt
.hundredths
*10);
267 #elif defined(HAVE_FTIME)
269 if ( ftime(&tp
) == 0 )
271 return (val
+ tp
.millitm
);
273 #elif !defined(__BORLANDC__) && !(defined(__VISUALC__) && defined(__WIN16__))
274 #warning "wxStopWatch will be up to second resolution!"