]>
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 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 #if defined(__WIN32__)
52 #if defined(__WIN32__) && !defined(HAVE_FTIME)
58 #include <sys/types.h> // for time_t
61 #if defined(HAVE_GETTIMEOFDAY)
64 #elif defined(HAVE_FTIME)
65 #include <sys/timeb.h>
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
73 IMPLEMENT_DYNAMIC_CLASS(wxTimerEvent
, wxEvent
)
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 // on some really old systems gettimeofday() doesn't have the second argument,
81 // define wxGetTimeOfDay() to hide this difference
82 #ifdef HAVE_GETTIMEOFDAY
83 #ifdef WX_GETTIMEOFDAY_NO_TZ
85 #define wxGetTimeOfDay(tv, tz) gettimeofday(tv)
87 #define wxGetTimeOfDay(tv, tz) gettimeofday((tv), (tz))
89 #endif // HAVE_GETTIMEOFDAY
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
95 wxLongLong
wxGetLocalTimeMillis();
97 // ============================================================================
99 // ============================================================================
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
107 void wxTimerBase::Notify()
109 // the base class version generates an event if it has owner - which it
110 // should because otherwise nobody can process timer events
111 wxCHECK_RET( m_owner
, _T("wxTimer::Notify() should be overridden.") );
113 wxTimerEvent
event(m_idTimer
, m_milli
);
114 (void)m_owner
->ProcessEvent(event
);
119 // ----------------------------------------------------------------------------
121 // ----------------------------------------------------------------------------
123 void wxStopWatch::Start(long t
)
125 m_t0
= wxGetLocalTimeMillis() - t
;
129 long wxStopWatch::GetElapsedTime() const
131 return (wxGetLocalTimeMillis() - m_t0
).GetLo();
134 long wxStopWatch::Time() const
136 return (m_pause
? m_pause
: GetElapsedTime());
139 // ----------------------------------------------------------------------------
140 // old timer functions superceded by wxStopWatch
141 // ----------------------------------------------------------------------------
143 static wxLongLong wxStartTime
= 0l;
145 // starts the global timer
148 wxStartTime
= wxGetLocalTimeMillis();
151 // Returns elapsed time in milliseconds
152 long wxGetElapsedTime(bool resetTimer
)
154 wxLongLong oldTime
= wxStartTime
;
155 wxLongLong newTime
= wxGetLocalTimeMillis();
158 wxStartTime
= newTime
;
160 return (newTime
- oldTime
).GetLo();
164 // ----------------------------------------------------------------------------
165 // the functions to get the current time and timezone info
166 // ----------------------------------------------------------------------------
168 // Get local time as seconds since 00:00:00, Jan 1st 1970
169 long wxGetLocalTime()
174 // This cannot be made static because mktime can overwrite it.
176 memset(&tm
, 0, sizeof(tm
));
179 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
183 tm
.tm_isdst
= -1; // let mktime guess
185 // Note that mktime assumes that the struct tm contains local time.
187 t1
= time(&t1
); // now
188 t0
= mktime(&tm
); // origin
190 // Return the difference in seconds.
192 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
193 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
195 wxLogSysError(_("Failed to get the local system time"));
199 // Get UTC time as seconds since 00:00:00, Jan 1st 1970
205 // This cannot be made static because mktime can overwrite it
207 memset(&tm
, 0, sizeof(tm
));
210 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
214 tm
.tm_isdst
= -1; // let mktime guess
216 // Note that mktime assumes that the struct tm contains local time.
218 t1
= time(&t1
); // now
219 t0
= mktime(&tm
); // origin in localtime
221 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
223 // To get t0 as GMT we convert to a struct tm with gmtime,
224 // and then back again.
230 memcpy(&tm
, ptm
, sizeof(tm
));
233 if (t0
!= (time_t)-1 )
234 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
235 wxLogSysError(_("mktime() failed"));
239 wxLogSysError(_("gmtime() failed"));
243 wxLogError(_("Failed to get the UTC system time."));
249 // Get local time as milliseconds since 00:00:00, Jan 1st 1970
250 wxLongLong
wxGetLocalTimeMillis()
252 wxLongLong val
= 1000l;
254 // If possible, use a functin which avoids conversions from
255 // broken-up time structures to milliseconds,
257 #if defined(HAVE_GETTIMEOFDAY)
259 if ( wxGetTimeOfDay(&tp
, (struct timezone
*)NULL
) != -1 )
262 return (val
+ (tp
.tv_usec
/ 1000));
266 wxLogError(_("wxGetTimeOfDay failed."));
269 #elif defined(HAVE_FTIME)
272 // ftime() is void and not int in some mingw32 headers, so don't
273 // test the return code (well, it shouldn't fail anyhow...)
276 return (val
+ tp
.millitm
);
278 // We use wxGetLocalTime() to get the seconds since
279 // 00:00:00 Jan 1st 1970 and then whatever is available
280 // to get millisecond resolution.
282 // NOTE that this might lead to a problem if the clocks
283 // use different sources, so this approach should be
284 // avoided where possible.
286 val
*= wxGetLocalTime();
288 #if defined(__VISAGECPP__)
290 ::DosGetDateTime(&dt
);
291 val
+= (dt
.hundredths
*10);
292 #elif defined (__WIN32__)
293 #warning "Possible clock skew bug in wxStopWatch!"
296 val
+= st
.wMilliseconds
;
298 #warning "wxStopWatch will be up to second resolution!"