]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
0470b1e6 | 2 | // Name: common/timercmn.cpp |
c801d85f | 3 | // Purpose: Common timer implementation |
5ebdc86a GRG |
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: | |
c801d85f KB |
10 | // Created: 04/01/98 |
11 | // RCS-ID: $Id$ | |
12 | // Copyright: (c) Julian Smart and Markus Holzem | |
b704229e | 13 | // (c) 1999 Guillermo Rodriguez <guille@iies.es> |
ed791986 | 14 | // Licence: wxWindows license |
c801d85f KB |
15 | ///////////////////////////////////////////////////////////////////////////// |
16 | ||
0470b1e6 VZ |
17 | // ============================================================================ |
18 | // declarations | |
19 | // ============================================================================ | |
20 | ||
21 | // ---------------------------------------------------------------------------- | |
91ff98b7 | 22 | // wxWin headers |
0470b1e6 VZ |
23 | // ---------------------------------------------------------------------------- |
24 | ||
c801d85f | 25 | #ifdef __GNUG__ |
0470b1e6 | 26 | #pragma implementation "timerbase.h" |
c801d85f KB |
27 | #endif |
28 | ||
29 | // For compilers that support precompilation, includes "wx.h". | |
30 | #include "wx/wxprec.h" | |
31 | ||
32 | #ifdef __BORLANDC__ | |
0470b1e6 | 33 | #pragma hdrstop |
c801d85f KB |
34 | #endif |
35 | ||
36 | #ifndef WX_PRECOMP | |
0470b1e6 VZ |
37 | #include "wx/intl.h" |
38 | #include "wx/log.h" | |
c801d85f KB |
39 | #endif |
40 | ||
41 | #include "wx/timer.h" | |
b8f04990 | 42 | #include "wx/longlong.h" |
c801d85f | 43 | |
91ff98b7 GRG |
44 | // ---------------------------------------------------------------------------- |
45 | // System headers | |
46 | // ---------------------------------------------------------------------------- | |
47 | ||
b8f04990 GRG |
48 | #if defined(__WIN32__) |
49 | #include <windows.h> | |
0470b1e6 VZ |
50 | #endif |
51 | ||
91ff98b7 GRG |
52 | #if defined(__WIN32__) && !defined(HAVE_FTIME) |
53 | #define HAVE_FTIME | |
91ff98b7 GRG |
54 | #endif |
55 | ||
47af9124 GRG |
56 | #if defined(__VISAGECPP__) && !defined(HAVE_FTIME) |
57 | #define HAVE_FTIME | |
07d7f20b DW |
58 | # if __IBMCPP__ >= 400 |
59 | # define ftime(x) _ftime(x) | |
60 | # endif | |
47af9124 GRG |
61 | #endif |
62 | ||
b8f04990 GRG |
63 | #include <time.h> |
64 | #ifndef __WXMAC__ | |
65 | #include <sys/types.h> // for time_t | |
07cf98cb VZ |
66 | #endif |
67 | ||
0470b1e6 VZ |
68 | #if defined(HAVE_GETTIMEOFDAY) |
69 | #include <sys/time.h> | |
70 | #include <unistd.h> | |
0470b1e6 VZ |
71 | #elif defined(HAVE_FTIME) |
72 | #include <sys/timeb.h> | |
c801d85f KB |
73 | #endif |
74 | ||
ed791986 VZ |
75 | // ---------------------------------------------------------------------------- |
76 | // wxWin macros | |
77 | // ---------------------------------------------------------------------------- | |
78 | ||
52a07708 VZ |
79 | #if wxUSE_GUI |
80 | IMPLEMENT_DYNAMIC_CLASS(wxTimerEvent, wxEvent) | |
81 | #endif // wxUSE_GUI | |
ed791986 | 82 | |
0470b1e6 VZ |
83 | // ---------------------------------------------------------------------------- |
84 | // macros | |
85 | // ---------------------------------------------------------------------------- | |
ce3ed50d | 86 | |
0470b1e6 VZ |
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 | |
c801d85f | 97 | |
0470b1e6 VZ |
98 | // ============================================================================ |
99 | // implementation | |
100 | // ============================================================================ | |
c801d85f | 101 | |
ed791986 VZ |
102 | // ---------------------------------------------------------------------------- |
103 | // wxTimerBase | |
104 | // ---------------------------------------------------------------------------- | |
105 | ||
91ff98b7 GRG |
106 | #if wxUSE_GUI |
107 | ||
ed791986 VZ |
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 | } | |
b8f04990 | 117 | |
99646f7e VZ |
118 | bool wxTimerBase::Start(int milliseconds, bool oneShot) |
119 | { | |
120 | if ( IsRunning() ) | |
121 | { | |
122 | // not stopping the already running timer might work for some | |
123 | // platforms (no problems under MSW) but leads to mysterious crashes | |
124 | // on the others (GTK), so to be on the safe side do it here | |
125 | Stop(); | |
126 | } | |
127 | ||
128 | if ( milliseconds != -1 ) | |
129 | { | |
130 | m_milli = milliseconds; | |
131 | } | |
132 | ||
133 | m_oneShot = oneShot; | |
134 | ||
135 | return TRUE; | |
136 | } | |
137 | ||
52a07708 VZ |
138 | #endif // wxUSE_GUI |
139 | ||
0470b1e6 VZ |
140 | // ---------------------------------------------------------------------------- |
141 | // wxStopWatch | |
142 | // ---------------------------------------------------------------------------- | |
c801d85f | 143 | |
0470b1e6 VZ |
144 | void wxStopWatch::Start(long t) |
145 | { | |
b8f04990 | 146 | m_t0 = wxGetLocalTimeMillis() - t; |
0470b1e6 VZ |
147 | m_pause = 0; |
148 | } | |
149 | ||
92da8bde | 150 | long wxStopWatch::GetElapsedTime() const |
0470b1e6 | 151 | { |
fd7cf532 | 152 | return (wxGetLocalTimeMillis() - m_t0).GetLo(); |
b8f04990 GRG |
153 | } |
154 | ||
8242fb3b | 155 | long wxStopWatch::Time() const |
b8f04990 | 156 | { |
8242fb3b | 157 | return (m_pause ? m_pause : GetElapsedTime()); |
0470b1e6 VZ |
158 | } |
159 | ||
160 | // ---------------------------------------------------------------------------- | |
161 | // old timer functions superceded by wxStopWatch | |
162 | // ---------------------------------------------------------------------------- | |
c801d85f | 163 | |
cd0b1709 | 164 | static wxLongLong wxStartTime = 0l; |
c801d85f | 165 | |
0470b1e6 VZ |
166 | // starts the global timer |
167 | void wxStartTimer() | |
c801d85f | 168 | { |
b8f04990 | 169 | wxStartTime = wxGetLocalTimeMillis(); |
c801d85f KB |
170 | } |
171 | ||
172 | // Returns elapsed time in milliseconds | |
173 | long wxGetElapsedTime(bool resetTimer) | |
f0599ea9 | 174 | { |
b8f04990 GRG |
175 | wxLongLong oldTime = wxStartTime; |
176 | wxLongLong newTime = wxGetLocalTimeMillis(); | |
f0599ea9 | 177 | |
0470b1e6 VZ |
178 | if ( resetTimer ) |
179 | wxStartTime = newTime; | |
180 | ||
b8f04990 | 181 | return (newTime - oldTime).GetLo(); |
f0599ea9 SB |
182 | } |
183 | ||
184 | ||
0470b1e6 VZ |
185 | // ---------------------------------------------------------------------------- |
186 | // the functions to get the current time and timezone info | |
187 | // ---------------------------------------------------------------------------- | |
188 | ||
b8f04990 GRG |
189 | // Get local time as seconds since 00:00:00, Jan 1st 1970 |
190 | long wxGetLocalTime() | |
c801d85f | 191 | { |
b8f04990 GRG |
192 | struct tm tm; |
193 | time_t t0, t1; | |
194 | ||
195 | // This cannot be made static because mktime can overwrite it. | |
196 | // | |
197 | memset(&tm, 0, sizeof(tm)); | |
198 | tm.tm_year = 70; | |
199 | tm.tm_mon = 0; | |
200 | tm.tm_mday = 5; // not Jan 1st 1970 due to mktime 'feature' | |
201 | tm.tm_hour = 0; | |
202 | tm.tm_min = 0; | |
203 | tm.tm_sec = 0; | |
204 | tm.tm_isdst = -1; // let mktime guess | |
205 | ||
206 | // Note that mktime assumes that the struct tm contains local time. | |
207 | // | |
208 | t1 = time(&t1); // now | |
209 | t0 = mktime(&tm); // origin | |
210 | ||
211 | // Return the difference in seconds. | |
212 | // | |
213 | if (( t0 != (time_t)-1 ) && ( t1 != (time_t)-1 )) | |
214 | return (long)difftime(t1, t0) + (60 * 60 * 24 * 4); | |
215 | ||
216 | wxLogSysError(_("Failed to get the local system time")); | |
217 | return -1; | |
218 | } | |
b76b015e | 219 | |
b8f04990 GRG |
220 | // Get UTC time as seconds since 00:00:00, Jan 1st 1970 |
221 | long wxGetUTCTime() | |
222 | { | |
33ac7e6f KB |
223 | struct tm tm; |
224 | struct tm *ptm; | |
b8f04990 GRG |
225 | time_t t0, t1; |
226 | ||
227 | // This cannot be made static because mktime can overwrite it | |
228 | // | |
229 | memset(&tm, 0, sizeof(tm)); | |
230 | tm.tm_year = 70; | |
231 | tm.tm_mon = 0; | |
232 | tm.tm_mday = 5; // not Jan 1st 1970 due to mktime 'feature' | |
233 | tm.tm_hour = 0; | |
234 | tm.tm_min = 0; | |
235 | tm.tm_sec = 0; | |
236 | tm.tm_isdst = -1; // let mktime guess | |
237 | ||
238 | // Note that mktime assumes that the struct tm contains local time. | |
239 | // | |
240 | t1 = time(&t1); // now | |
241 | t0 = mktime(&tm); // origin in localtime | |
242 | ||
243 | if (( t0 != (time_t)-1 ) && ( t1 != (time_t)-1 )) | |
0470b1e6 | 244 | { |
b8f04990 GRG |
245 | // To get t0 as GMT we convert to a struct tm with gmtime, |
246 | // and then back again. | |
247 | // | |
248 | ptm = gmtime(&t0); | |
0470b1e6 | 249 | |
b8f04990 | 250 | if (ptm) |
0470b1e6 | 251 | { |
b8f04990 GRG |
252 | memcpy(&tm, ptm, sizeof(tm)); |
253 | t0 = mktime(&tm); | |
254 | ||
255 | if (t0 != (time_t)-1 ) | |
256 | return (long)difftime(t1, t0) + (60 * 60 * 24 * 4); | |
2c8e4738 VZ |
257 | wxLogSysError(_("mktime() failed")); |
258 | } | |
259 | else | |
260 | { | |
261 | wxLogSysError(_("gmtime() failed")); | |
0470b1e6 VZ |
262 | } |
263 | } | |
2c8e4738 VZ |
264 | |
265 | wxLogError(_("Failed to get the UTC system time.")); | |
266 | ||
b8f04990 GRG |
267 | return -1; |
268 | } | |
269 | ||
270 | ||
271 | // Get local time as milliseconds since 00:00:00, Jan 1st 1970 | |
272 | wxLongLong wxGetLocalTimeMillis() | |
273 | { | |
0158f584 SB |
274 | wxLongLong val = 1000l; |
275 | ||
658c5400 GRG |
276 | // If possible, use a functin which avoids conversions from |
277 | // broken-up time structures to milliseconds, | |
278 | ||
0158f584 SB |
279 | #if defined(HAVE_GETTIMEOFDAY) |
280 | struct timeval tp; | |
281 | if ( wxGetTimeOfDay(&tp, (struct timezone *)NULL) != -1 ) | |
282 | { | |
283 | val *= tp.tv_sec; | |
284 | return (val + (tp.tv_usec / 1000)); | |
285 | } | |
91ff98b7 GRG |
286 | else |
287 | { | |
288 | wxLogError(_("wxGetTimeOfDay failed.")); | |
289 | return 0; | |
290 | } | |
658c5400 GRG |
291 | #elif defined(HAVE_FTIME) |
292 | struct timeb tp; | |
2c8e4738 | 293 | |
91ff98b7 GRG |
294 | // ftime() is void and not int in some mingw32 headers, so don't |
295 | // test the return code (well, it shouldn't fail anyhow...) | |
2c8e4738 VZ |
296 | (void)ftime(&tp); |
297 | val *= tp.time; | |
298 | return (val + tp.millitm); | |
f6bcfd97 | 299 | #else // no gettimeofday() nor ftime() |
b8f04990 GRG |
300 | // We use wxGetLocalTime() to get the seconds since |
301 | // 00:00:00 Jan 1st 1970 and then whatever is available | |
302 | // to get millisecond resolution. | |
658c5400 | 303 | // |
91ff98b7 GRG |
304 | // NOTE that this might lead to a problem if the clocks |
305 | // use different sources, so this approach should be | |
306 | // avoided where possible. | |
658c5400 | 307 | |
cd0b1709 | 308 | val *= wxGetLocalTime(); |
b8f04990 | 309 | |
47af9124 GRG |
310 | // GRG: This will go soon as all WIN32 seem to have ftime |
311 | #if defined (__WIN32__) | |
f6bcfd97 BP |
312 | // If your platform/compiler needs to use two different functions |
313 | // to get ms resolution, please do NOT just shut off these warnings, | |
314 | // drop me a line instead at <guille@iies.es> | |
315 | #warning "Possible clock skew bug in wxGetLocalTimeMillis()!" | |
316 | ||
91ff98b7 GRG |
317 | SYSTEMTIME st; |
318 | ::GetLocalTime(&st); | |
319 | val += st.wMilliseconds; | |
f6bcfd97 BP |
320 | #else // !Win32 |
321 | // If your platform/compiler does not support ms resolution please | |
322 | // do NOT just shut off these warnings, drop me a line instead at | |
323 | // <guille@iies.es> | |
324 | ||
325 | #if defined(__VISUALC__) | |
326 | #pragma message("wxStopWatch will be up to second resolution!") | |
327 | #elif defined(__BORLANDC__) | |
328 | #pragma message "wxStopWatch will be up to second resolution!" | |
329 | #else | |
330 | #warning "wxStopWatch will be up to second resolution!" | |
331 | #endif // compiler | |
c801d85f | 332 | #endif |
c801d85f | 333 | |
b8f04990 | 334 | return val; |
91ff98b7 | 335 | |
f6bcfd97 | 336 | #endif // time functions |
c801d85f | 337 | } |