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