]>
git.saurik.com Git - wxWidgets.git/blob - src/common/timercmn.cpp
4cb3e51b1717988a3f03163012b54871d9679519
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
;
115 long wxStopWatch::GetElapsedTime() const
117 return (wxGetLocalTimeMillis() - m_t0
).GetLo();
120 long wxStopWatch::Time() const
122 return (m_pause
? m_pause
: GetElapsedTime());
125 // ----------------------------------------------------------------------------
126 // old timer functions superceded by wxStopWatch
127 // ----------------------------------------------------------------------------
129 static wxLongLong wxStartTime
= 0l;
131 // starts the global timer
134 wxStartTime
= wxGetLocalTimeMillis();
137 // Returns elapsed time in milliseconds
138 long wxGetElapsedTime(bool resetTimer
)
140 wxLongLong oldTime
= wxStartTime
;
141 wxLongLong newTime
= wxGetLocalTimeMillis();
144 wxStartTime
= newTime
;
146 return (newTime
- oldTime
).GetLo();
150 // ----------------------------------------------------------------------------
151 // the functions to get the current time and timezone info
152 // ----------------------------------------------------------------------------
154 // Get local time as seconds since 00:00:00, Jan 1st 1970
155 long wxGetLocalTime()
160 // This cannot be made static because mktime can overwrite it.
162 memset(&tm
, 0, sizeof(tm
));
165 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
169 tm
.tm_isdst
= -1; // let mktime guess
171 // Note that mktime assumes that the struct tm contains local time.
173 t1
= time(&t1
); // now
174 t0
= mktime(&tm
); // origin
176 // Return the difference in seconds.
178 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
179 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
181 wxLogSysError(_("Failed to get the local system time"));
185 // Get UTC time as seconds since 00:00:00, Jan 1st 1970
191 // This cannot be made static because mktime can overwrite it
193 memset(&tm
, 0, sizeof(tm
));
196 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
200 tm
.tm_isdst
= -1; // let mktime guess
202 // Note that mktime assumes that the struct tm contains local time.
204 t1
= time(&t1
); // now
205 t0
= mktime(&tm
); // origin in localtime
207 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
209 // To get t0 as GMT we convert to a struct tm with gmtime,
210 // and then back again.
216 memcpy(&tm
, ptm
, sizeof(tm
));
219 if (t0
!= (time_t)-1 )
220 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
221 wxLogSysError(_("Failed 2nd mktime"));
223 wxLogSysError(_("Failed gmtime"));
225 wxLogSysError(_("Failed to get the UTC system time"));
230 // Get local time as milliseconds since 00:00:00, Jan 1st 1970
231 wxLongLong
wxGetLocalTimeMillis()
233 // We use wxGetLocalTime() to get the seconds since
234 // 00:00:00 Jan 1st 1970 and then whatever is available
235 // to get millisecond resolution.
237 wxLongLong val
= 1000l;
238 val
*= wxGetLocalTime();
240 // If we got here, do not fail even if we can't get
241 // millisecond resolution.
243 #if defined(__WIN32__)
246 return (val
+ st
.wMilliseconds
);
247 #elif defined(__VISAGECPP__)
249 ::DosGetDateTime(&dt
);
250 return (val
+ dt
.hundredths
*10);
251 #elif defined(HAVE_GETTIMEOFDAY)
253 if ( wxGetTimeOfDay(&tp
, (struct timezone
*)NULL
) != -1 )
256 return (val
+ (tp
.tv_usec
/ 1000));
258 #elif defined(HAVE_FTIME)
260 if ( ftime(&tp
) == 0 )
262 return (val
+ tp
.millitm
);
265 #if !defined(__BORLANDC__) && !(defined(__VISUALC__) && defined(__WIN16__))
266 #warning "wxStopWatch will be up to second resolution!"