]> git.saurik.com Git - wxWidgets.git/blob - src/common/timercmn.cpp
Use GTK_OBJECT_GET_CLASS macro.
[wxWidgets.git] / src / common / timercmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/timercmn.cpp
3 // Purpose: Common timer implementation
4 // Author:
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)
9 // Modified by:
10 // Created: 04/01/98
11 // RCS-ID: $Id$
12 // Copyright: (c) Julian Smart and Markus Holzem
13 // (c) 1999 Guillermo Rodriguez <guille@iies.es>
14 // Licence: wxWindows license
15 /////////////////////////////////////////////////////////////////////////////
16
17 // ============================================================================
18 // declarations
19 // ============================================================================
20
21 // ----------------------------------------------------------------------------
22 // wxWin headers
23 // ----------------------------------------------------------------------------
24
25 #ifdef __GNUG__
26 #pragma implementation "timerbase.h"
27 #endif
28
29 // For compilers that support precompilation, includes "wx.h".
30 #include "wx/wxprec.h"
31
32 #ifdef __BORLANDC__
33 #pragma hdrstop
34 #endif
35
36 #ifndef WX_PRECOMP
37 #include "wx/intl.h"
38 #include "wx/log.h"
39 #endif
40
41 #include "wx/timer.h"
42 #include "wx/longlong.h"
43
44 // ----------------------------------------------------------------------------
45 // System headers
46 // ----------------------------------------------------------------------------
47
48 #if defined(__WIN32__)
49 #include <windows.h>
50 #endif
51
52 #if defined(__WIN32__) && !defined(HAVE_FTIME)
53 #define HAVE_FTIME
54 #endif
55
56 #include <time.h>
57 #ifndef __WXMAC__
58 #include <sys/types.h> // for time_t
59 #endif
60
61 #if defined(HAVE_GETTIMEOFDAY)
62 #include <sys/time.h>
63 #include <unistd.h>
64 #elif defined(HAVE_FTIME)
65 #include <sys/timeb.h>
66 #endif
67
68 // ----------------------------------------------------------------------------
69 // wxWin macros
70 // ----------------------------------------------------------------------------
71
72 #if wxUSE_GUI
73 IMPLEMENT_DYNAMIC_CLASS(wxTimerEvent, wxEvent)
74 #endif // wxUSE_GUI
75
76 // ----------------------------------------------------------------------------
77 // macros
78 // ----------------------------------------------------------------------------
79
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
84 struct timezone;
85 #define wxGetTimeOfDay(tv, tz) gettimeofday(tv)
86 #else
87 #define wxGetTimeOfDay(tv, tz) gettimeofday((tv), (tz))
88 #endif
89 #endif // HAVE_GETTIMEOFDAY
90
91 // ============================================================================
92 // implementation
93 // ============================================================================
94
95 // ----------------------------------------------------------------------------
96 // wxTimerBase
97 // ----------------------------------------------------------------------------
98
99 #if wxUSE_GUI
100
101 void wxTimerBase::Notify()
102 {
103 // the base class version generates an event if it has owner - which it
104 // should because otherwise nobody can process timer events
105 wxCHECK_RET( m_owner, _T("wxTimer::Notify() should be overridden.") );
106
107 wxTimerEvent event(m_idTimer, m_milli);
108 (void)m_owner->ProcessEvent(event);
109 }
110
111 #endif // wxUSE_GUI
112
113 // ----------------------------------------------------------------------------
114 // wxStopWatch
115 // ----------------------------------------------------------------------------
116
117 void wxStopWatch::Start(long t)
118 {
119 m_t0 = wxGetLocalTimeMillis() - t;
120 m_pause = 0;
121 }
122
123 long wxStopWatch::GetElapsedTime() const
124 {
125 return (wxGetLocalTimeMillis() - m_t0).GetLo();
126 }
127
128 long wxStopWatch::Time() const
129 {
130 return (m_pause ? m_pause : GetElapsedTime());
131 }
132
133 // ----------------------------------------------------------------------------
134 // old timer functions superceded by wxStopWatch
135 // ----------------------------------------------------------------------------
136
137 static wxLongLong wxStartTime = 0l;
138
139 // starts the global timer
140 void wxStartTimer()
141 {
142 wxStartTime = wxGetLocalTimeMillis();
143 }
144
145 // Returns elapsed time in milliseconds
146 long wxGetElapsedTime(bool resetTimer)
147 {
148 wxLongLong oldTime = wxStartTime;
149 wxLongLong newTime = wxGetLocalTimeMillis();
150
151 if ( resetTimer )
152 wxStartTime = newTime;
153
154 return (newTime - oldTime).GetLo();
155 }
156
157
158 // ----------------------------------------------------------------------------
159 // the functions to get the current time and timezone info
160 // ----------------------------------------------------------------------------
161
162 // Get local time as seconds since 00:00:00, Jan 1st 1970
163 long wxGetLocalTime()
164 {
165 struct tm tm;
166 time_t t0, t1;
167
168 // This cannot be made static because mktime can overwrite it.
169 //
170 memset(&tm, 0, sizeof(tm));
171 tm.tm_year = 70;
172 tm.tm_mon = 0;
173 tm.tm_mday = 5; // not Jan 1st 1970 due to mktime 'feature'
174 tm.tm_hour = 0;
175 tm.tm_min = 0;
176 tm.tm_sec = 0;
177 tm.tm_isdst = -1; // let mktime guess
178
179 // Note that mktime assumes that the struct tm contains local time.
180 //
181 t1 = time(&t1); // now
182 t0 = mktime(&tm); // origin
183
184 // Return the difference in seconds.
185 //
186 if (( t0 != (time_t)-1 ) && ( t1 != (time_t)-1 ))
187 return (long)difftime(t1, t0) + (60 * 60 * 24 * 4);
188
189 wxLogSysError(_("Failed to get the local system time"));
190 return -1;
191 }
192
193 // Get UTC time as seconds since 00:00:00, Jan 1st 1970
194 long wxGetUTCTime()
195 {
196 struct tm tm, *ptm;
197 time_t t0, t1;
198
199 // This cannot be made static because mktime can overwrite it
200 //
201 memset(&tm, 0, sizeof(tm));
202 tm.tm_year = 70;
203 tm.tm_mon = 0;
204 tm.tm_mday = 5; // not Jan 1st 1970 due to mktime 'feature'
205 tm.tm_hour = 0;
206 tm.tm_min = 0;
207 tm.tm_sec = 0;
208 tm.tm_isdst = -1; // let mktime guess
209
210 // Note that mktime assumes that the struct tm contains local time.
211 //
212 t1 = time(&t1); // now
213 t0 = mktime(&tm); // origin in localtime
214
215 if (( t0 != (time_t)-1 ) && ( t1 != (time_t)-1 ))
216 {
217 // To get t0 as GMT we convert to a struct tm with gmtime,
218 // and then back again.
219 //
220 ptm = gmtime(&t0);
221
222 if (ptm)
223 {
224 memcpy(&tm, ptm, sizeof(tm));
225 t0 = mktime(&tm);
226
227 if (t0 != (time_t)-1 )
228 return (long)difftime(t1, t0) + (60 * 60 * 24 * 4);
229 wxLogSysError(_("mktime() failed"));
230 }
231 else
232 {
233 wxLogSysError(_("gmtime() failed"));
234 }
235 }
236
237 wxLogError(_("Failed to get the UTC system time."));
238
239 return -1;
240 }
241
242
243 // Get local time as milliseconds since 00:00:00, Jan 1st 1970
244 wxLongLong wxGetLocalTimeMillis()
245 {
246 wxLongLong val = 1000l;
247
248 // If possible, use a functin which avoids conversions from
249 // broken-up time structures to milliseconds,
250
251 #if defined(HAVE_GETTIMEOFDAY)
252 struct timeval tp;
253 if ( wxGetTimeOfDay(&tp, (struct timezone *)NULL) != -1 )
254 {
255 val *= tp.tv_sec;
256 return (val + (tp.tv_usec / 1000));
257 }
258 else
259 {
260 wxLogError(_("wxGetTimeOfDay failed."));
261 return 0;
262 }
263 #elif defined(HAVE_FTIME)
264 struct timeb tp;
265
266 // ftime() is void and not int in some mingw32 headers, so don't
267 // test the return code (well, it shouldn't fail anyhow...)
268 (void)ftime(&tp);
269 val *= tp.time;
270 return (val + tp.millitm);
271 #else // no gettimeofday() nor ftime()
272 // We use wxGetLocalTime() to get the seconds since
273 // 00:00:00 Jan 1st 1970 and then whatever is available
274 // to get millisecond resolution.
275 //
276 // NOTE that this might lead to a problem if the clocks
277 // use different sources, so this approach should be
278 // avoided where possible.
279
280 val *= wxGetLocalTime();
281
282 #if defined(__VISAGECPP__)
283 // If your platform/compiler needs to use two different functions
284 // to get ms resolution, please do NOT just shut off these warnings,
285 // drop me a line instead at <guille@iies.es>
286 #warning "Possible clock skew bug in wxGetLocalTimeMillis()!"
287
288 DATETIME dt;
289 ::DosGetDateTime(&dt);
290 val += (dt.hundredths*10);
291 #elif defined (__WIN32__)
292 // If your platform/compiler needs to use two different functions
293 // to get ms resolution, please do NOT just shut off these warnings,
294 // drop me a line instead at <guille@iies.es>
295 #warning "Possible clock skew bug in wxGetLocalTimeMillis()!"
296
297 SYSTEMTIME st;
298 ::GetLocalTime(&st);
299 val += st.wMilliseconds;
300 #else // !Win32
301 // If your platform/compiler does not support ms resolution please
302 // do NOT just shut off these warnings, drop me a line instead at
303 // <guille@iies.es>
304
305 #if defined(__VISUALC__)
306 #pragma message("wxStopWatch will be up to second resolution!")
307 #elif defined(__BORLANDC__)
308 #pragma message "wxStopWatch will be up to second resolution!"
309 #else
310 #warning "wxStopWatch will be up to second resolution!"
311 #endif // compiler
312 #endif
313
314 return val;
315
316 #endif // time functions
317 }