]> git.saurik.com Git - wxWidgets.git/blob - src/common/timercmn.cpp
FALSE not false
[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 #if defined(__VISAGECPP__) && !defined(HAVE_FTIME)
57 #define HAVE_FTIME
58 # if __IBMCPP__ >= 400
59 # define ftime(x) _ftime(x)
60 # endif
61 #endif
62
63 #if defined(__MWERKS__) && defined(__WXMSW__)
64 # undef HAVE_FTIME
65 # undef HAVE_GETTIMEOFDAY
66 #endif
67
68 #include <time.h>
69 #ifndef __WXMAC__
70 #include <sys/types.h> // for time_t
71 #endif
72
73 #if defined(HAVE_GETTIMEOFDAY)
74 #include <sys/time.h>
75 #include <unistd.h>
76 #elif defined(HAVE_FTIME)
77 #include <sys/timeb.h>
78 #endif
79
80 // ----------------------------------------------------------------------------
81 // wxWin macros
82 // ----------------------------------------------------------------------------
83
84 #if wxUSE_GUI && wxUSE_TIMER
85 IMPLEMENT_DYNAMIC_CLASS(wxTimerEvent, wxEvent)
86 #endif // wxUSE_GUI
87
88 // ----------------------------------------------------------------------------
89 // macros
90 // ----------------------------------------------------------------------------
91
92 // on some really old systems gettimeofday() doesn't have the second argument,
93 // define wxGetTimeOfDay() to hide this difference
94 #ifdef HAVE_GETTIMEOFDAY
95 #ifdef WX_GETTIMEOFDAY_NO_TZ
96 struct timezone;
97 #define wxGetTimeOfDay(tv, tz) gettimeofday(tv)
98 #else
99 #define wxGetTimeOfDay(tv, tz) gettimeofday((tv), (tz))
100 #endif
101 #endif // HAVE_GETTIMEOFDAY
102
103 // ============================================================================
104 // implementation
105 // ============================================================================
106
107 // ----------------------------------------------------------------------------
108 // wxTimerBase
109 // ----------------------------------------------------------------------------
110
111 #if wxUSE_GUI && wxUSE_TIMER
112
113 wxTimerBase::~wxTimerBase()
114 {
115 // this destructor is required for Darwin
116 }
117
118 void wxTimerBase::Notify()
119 {
120 // the base class version generates an event if it has owner - which it
121 // should because otherwise nobody can process timer events
122 wxCHECK_RET( m_owner, _T("wxTimer::Notify() should be overridden.") );
123
124 wxTimerEvent event(m_idTimer, m_milli);
125 (void)m_owner->ProcessEvent(event);
126 }
127
128 bool wxTimerBase::Start(int milliseconds, bool oneShot)
129 {
130 if ( IsRunning() )
131 {
132 // not stopping the already running timer might work for some
133 // platforms (no problems under MSW) but leads to mysterious crashes
134 // on the others (GTK), so to be on the safe side do it here
135 Stop();
136 }
137
138 if ( milliseconds != -1 )
139 {
140 m_milli = milliseconds;
141 }
142
143 m_oneShot = oneShot;
144
145 return TRUE;
146 }
147
148 #endif // wxUSE_GUI
149
150 // ----------------------------------------------------------------------------
151 // wxStopWatch
152 // ----------------------------------------------------------------------------
153
154 #if wxUSE_LONGLONG
155
156 void wxStopWatch::Start(long t)
157 {
158 m_t0 = wxGetLocalTimeMillis() - t;
159 m_pause = 0;
160 }
161
162 long wxStopWatch::GetElapsedTime() const
163 {
164 return (wxGetLocalTimeMillis() - m_t0).GetLo();
165 }
166
167 long wxStopWatch::Time() const
168 {
169 return (m_pause ? m_pause : GetElapsedTime());
170 }
171
172 #endif // wxUSE_LONGLONG
173
174 // ----------------------------------------------------------------------------
175 // old timer functions superceded by wxStopWatch
176 // ----------------------------------------------------------------------------
177
178 #if wxUSE_LONGLONG
179
180 static wxLongLong wxStartTime = 0l;
181
182 // starts the global timer
183 void wxStartTimer()
184 {
185 wxStartTime = wxGetLocalTimeMillis();
186 }
187
188 // Returns elapsed time in milliseconds
189 long wxGetElapsedTime(bool resetTimer)
190 {
191 wxLongLong oldTime = wxStartTime;
192 wxLongLong newTime = wxGetLocalTimeMillis();
193
194 if ( resetTimer )
195 wxStartTime = newTime;
196
197 return (newTime - oldTime).GetLo();
198 }
199
200 #endif // wxUSE_LONGLONG
201
202 // ----------------------------------------------------------------------------
203 // the functions to get the current time and timezone info
204 // ----------------------------------------------------------------------------
205
206 // Get local time as seconds since 00:00:00, Jan 1st 1970
207 long wxGetLocalTime()
208 {
209 struct tm tm;
210 time_t t0, t1;
211
212 // This cannot be made static because mktime can overwrite it.
213 //
214 memset(&tm, 0, sizeof(tm));
215 tm.tm_year = 70;
216 tm.tm_mon = 0;
217 tm.tm_mday = 5; // not Jan 1st 1970 due to mktime 'feature'
218 tm.tm_hour = 0;
219 tm.tm_min = 0;
220 tm.tm_sec = 0;
221 tm.tm_isdst = -1; // let mktime guess
222
223 // Note that mktime assumes that the struct tm contains local time.
224 //
225 t1 = time(&t1); // now
226 t0 = mktime(&tm); // origin
227
228 // Return the difference in seconds.
229 //
230 if (( t0 != (time_t)-1 ) && ( t1 != (time_t)-1 ))
231 return (long)difftime(t1, t0) + (60 * 60 * 24 * 4);
232
233 wxLogSysError(_("Failed to get the local system time"));
234 return -1;
235 }
236
237 // Get UTC time as seconds since 00:00:00, Jan 1st 1970
238 long wxGetUTCTime()
239 {
240 struct tm tm;
241 struct tm *ptm;
242 time_t t0, t1;
243
244 // This cannot be made static because mktime can overwrite it
245 //
246 memset(&tm, 0, sizeof(tm));
247 tm.tm_year = 70;
248 tm.tm_mon = 0;
249 tm.tm_mday = 5; // not Jan 1st 1970 due to mktime 'feature'
250 tm.tm_hour = 0;
251 tm.tm_min = 0;
252 tm.tm_sec = 0;
253 tm.tm_isdst = -1; // let mktime guess
254
255 // Note that mktime assumes that the struct tm contains local time.
256 //
257 t1 = time(&t1); // now
258 t0 = mktime(&tm); // origin in localtime
259
260 if (( t0 != (time_t)-1 ) && ( t1 != (time_t)-1 ))
261 {
262 // To get t0 as GMT we convert to a struct tm with gmtime,
263 // and then back again.
264 //
265 ptm = gmtime(&t0);
266
267 if (ptm)
268 {
269 memcpy(&tm, ptm, sizeof(tm));
270 t0 = mktime(&tm);
271
272 if (t0 != (time_t)-1 )
273 return (long)difftime(t1, t0) + (60 * 60 * 24 * 4);
274 wxLogSysError(_("mktime() failed"));
275 }
276 else
277 {
278 wxLogSysError(_("gmtime() failed"));
279 }
280 }
281
282 wxLogError(_("Failed to get the UTC system time."));
283
284 return -1;
285 }
286
287 #if wxUSE_LONGLONG
288
289 // Get local time as milliseconds since 00:00:00, Jan 1st 1970
290 wxLongLong wxGetLocalTimeMillis()
291 {
292 wxLongLong val = 1000l;
293
294 // If possible, use a function which avoids conversions from
295 // broken-up time structures to milliseconds
296
297 #if defined(HAVE_GETTIMEOFDAY)
298 struct timeval tp;
299 if ( wxGetTimeOfDay(&tp, (struct timezone *)NULL) != -1 )
300 {
301 val *= tp.tv_sec;
302 return (val + (tp.tv_usec / 1000));
303 }
304 else
305 {
306 wxLogError(_("wxGetTimeOfDay failed."));
307 return 0;
308 }
309 #elif defined(HAVE_FTIME)
310 struct timeb tp;
311
312 // ftime() is void and not int in some mingw32 headers, so don't
313 // test the return code (well, it shouldn't fail anyhow...)
314 (void)ftime(&tp);
315 val *= tp.time;
316 return (val + tp.millitm);
317 #else // no gettimeofday() nor ftime()
318 // We use wxGetLocalTime() to get the seconds since
319 // 00:00:00 Jan 1st 1970 and then whatever is available
320 // to get millisecond resolution.
321 //
322 // NOTE that this might lead to a problem if the clocks
323 // use different sources, so this approach should be
324 // avoided where possible.
325
326 val *= wxGetLocalTime();
327
328 // GRG: This will go soon as all WIN32 seem to have ftime
329 #if defined (__WIN32__)
330 // If your platform/compiler needs to use two different functions
331 // to get ms resolution, please do NOT just shut off these warnings,
332 // drop me a line instead at <guille@iies.es>
333 #warning "Possible clock skew bug in wxGetLocalTimeMillis()!"
334
335 SYSTEMTIME st;
336 ::GetLocalTime(&st);
337 val += st.wMilliseconds;
338 #else // !Win32
339 // If your platform/compiler does not support ms resolution please
340 // do NOT just shut off these warnings, drop me a line instead at
341 // <guille@iies.es>
342
343 #if defined(__VISUALC__) || defined (__WATCOMC__)
344 #pragma message("wxStopWatch will be up to second resolution!")
345 #elif defined(__BORLANDC__)
346 #pragma message "wxStopWatch will be up to second resolution!"
347 #else
348 #warning "wxStopWatch will be up to second resolution!"
349 #endif // compiler
350 #endif
351
352 return val;
353
354 #endif // time functions
355 }
356
357 #endif // wxUSE_LONGLONG
358