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 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 #if defined(__WIN32__)
52 #if defined(__WIN32__) && !defined(HAVE_FTIME)
61 #include <sys/types.h> // for time_t
64 #if defined(HAVE_GETTIMEOFDAY)
67 #elif defined(HAVE_FTIME)
68 #include <sys/timeb.h>
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
76 IMPLEMENT_DYNAMIC_CLASS(wxTimerEvent
, wxEvent
)
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 // on some really old systems gettimeofday() doesn't have the second argument,
84 // define wxGetTimeOfDay() to hide this difference
85 #ifdef HAVE_GETTIMEOFDAY
86 #ifdef WX_GETTIMEOFDAY_NO_TZ
88 #define wxGetTimeOfDay(tv, tz) gettimeofday(tv)
90 #define wxGetTimeOfDay(tv, tz) gettimeofday((tv), (tz))
92 #endif // HAVE_GETTIMEOFDAY
94 // ----------------------------------------------------------------------------
96 // ----------------------------------------------------------------------------
98 wxLongLong
wxGetLocalTimeMillis();
100 // ============================================================================
102 // ============================================================================
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
110 void wxTimerBase
::Notify()
112 // the base class version generates an event if it has owner - which it
113 // should because otherwise nobody can process timer events
114 wxCHECK_RET( m_owner
, _T("wxTimer::Notify() should be overridden.") );
116 wxTimerEvent
event(m_idTimer
, m_milli
);
117 (void)m_owner
->ProcessEvent(event
);
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
126 void wxStopWatch
::Start(long t
)
128 m_t0
= wxGetLocalTimeMillis() - t
;
132 long wxStopWatch
::GetElapsedTime() const
134 return (wxGetLocalTimeMillis() - m_t0
).GetLo();
137 long wxStopWatch
::Time() const
139 return (m_pause ? m_pause
: GetElapsedTime());
142 // ----------------------------------------------------------------------------
143 // old timer functions superceded by wxStopWatch
144 // ----------------------------------------------------------------------------
146 static wxLongLong wxStartTime
= 0l;
148 // starts the global timer
151 wxStartTime
= wxGetLocalTimeMillis();
154 // Returns elapsed time in milliseconds
155 long wxGetElapsedTime(bool resetTimer
)
157 wxLongLong oldTime
= wxStartTime
;
158 wxLongLong newTime
= wxGetLocalTimeMillis();
161 wxStartTime
= newTime
;
163 return (newTime
- oldTime
).GetLo();
167 // ----------------------------------------------------------------------------
168 // the functions to get the current time and timezone info
169 // ----------------------------------------------------------------------------
171 // Get local time as seconds since 00:00:00, Jan 1st 1970
172 long wxGetLocalTime()
177 // This cannot be made static because mktime can overwrite it.
179 memset(&tm
, 0, sizeof(tm
));
182 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
186 tm
.tm_isdst
= -1; // let mktime guess
188 // Note that mktime assumes that the struct tm contains local time.
190 t1
= time(&t1
); // now
191 t0
= mktime(&tm
); // origin
193 // Return the difference in seconds.
195 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
196 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
198 wxLogSysError(_("Failed to get the local system time"));
202 // Get UTC time as seconds since 00:00:00, Jan 1st 1970
208 // This cannot be made static because mktime can overwrite it
210 memset(&tm
, 0, sizeof(tm
));
213 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
217 tm
.tm_isdst
= -1; // let mktime guess
219 // Note that mktime assumes that the struct tm contains local time.
221 t1
= time(&t1
); // now
222 t0
= mktime(&tm
); // origin in localtime
224 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
226 // To get t0 as GMT we convert to a struct tm with gmtime,
227 // and then back again.
233 memcpy(&tm
, ptm
, sizeof(tm
));
236 if (t0
!= (time_t)-1 )
237 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
238 wxLogSysError(_("mktime() failed"));
242 wxLogSysError(_("gmtime() failed"));
246 wxLogError(_("Failed to get the UTC system time."));
252 // Get local time as milliseconds since 00:00:00, Jan 1st 1970
253 wxLongLong
wxGetLocalTimeMillis()
255 wxLongLong val
= 1000l;
257 // If possible, use a functin which avoids conversions from
258 // broken-up time structures to milliseconds,
260 #if defined(HAVE_GETTIMEOFDAY)
262 if ( wxGetTimeOfDay(&tp
, (struct timezone
*)NULL
) != -1 )
265 return (val
+ (tp
.tv_usec
/ 1000));
269 wxLogError(_("wxGetTimeOfDay failed."));
272 #elif defined(HAVE_FTIME)
275 // ftime() is void and not int in some mingw32 headers, so don't
276 // test the return code (well, it shouldn't fail anyhow...)
279 return (val
+ tp
.millitm
);
281 // We use wxGetLocalTime() to get the seconds since
282 // 00:00:00 Jan 1st 1970 and then whatever is available
283 // to get millisecond resolution.
285 // NOTE that this might lead to a problem if the clocks
286 // use different sources, so this approach should be
287 // avoided where possible.
289 val
*= wxGetLocalTime();
291 #if defined(__VISAGECPP__)
292 #warning "Possible clock skew bug in wxStopWatch!"
294 ::DosGetDateTime(&dt
);
295 val
+= (dt
.hundredths
*10);
296 #elif defined (__WIN32__)
297 #warning "Possible clock skew bug in wxStopWatch!"
300 val
+= st
.wMilliseconds
;
302 #warning "wxStopWatch will be up to second resolution!"