]>
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 | ||
b8f04990 GRG |
56 | #include <time.h> |
57 | #ifndef __WXMAC__ | |
58 | #include <sys/types.h> // for time_t | |
07cf98cb VZ |
59 | #endif |
60 | ||
0470b1e6 VZ |
61 | #if defined(HAVE_GETTIMEOFDAY) |
62 | #include <sys/time.h> | |
63 | #include <unistd.h> | |
0470b1e6 VZ |
64 | #elif defined(HAVE_FTIME) |
65 | #include <sys/timeb.h> | |
c801d85f KB |
66 | #endif |
67 | ||
ed791986 VZ |
68 | // ---------------------------------------------------------------------------- |
69 | // wxWin macros | |
70 | // ---------------------------------------------------------------------------- | |
71 | ||
52a07708 VZ |
72 | #if wxUSE_GUI |
73 | IMPLEMENT_DYNAMIC_CLASS(wxTimerEvent, wxEvent) | |
74 | #endif // wxUSE_GUI | |
ed791986 | 75 | |
0470b1e6 VZ |
76 | // ---------------------------------------------------------------------------- |
77 | // macros | |
78 | // ---------------------------------------------------------------------------- | |
ce3ed50d | 79 | |
0470b1e6 VZ |
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 | |
c801d85f | 90 | |
ed791986 VZ |
91 | // ---------------------------------------------------------------------------- |
92 | // prototypes | |
93 | // ---------------------------------------------------------------------------- | |
94 | ||
95 | wxLongLong wxGetLocalTimeMillis(); | |
96 | ||
0470b1e6 VZ |
97 | // ============================================================================ |
98 | // implementation | |
99 | // ============================================================================ | |
c801d85f | 100 | |
ed791986 VZ |
101 | // ---------------------------------------------------------------------------- |
102 | // wxTimerBase | |
103 | // ---------------------------------------------------------------------------- | |
104 | ||
91ff98b7 GRG |
105 | #if wxUSE_GUI |
106 | ||
ed791986 VZ |
107 | void wxTimerBase::Notify() |
108 | { | |
109 | // the base class version generates an event if it has owner - which it | |
110 | // should because otherwise nobody can process timer events | |
111 | wxCHECK_RET( m_owner, _T("wxTimer::Notify() should be overridden.") ); | |
112 | ||
113 | wxTimerEvent event(m_idTimer, m_milli); | |
114 | (void)m_owner->ProcessEvent(event); | |
115 | } | |
b8f04990 | 116 | |
52a07708 VZ |
117 | #endif // wxUSE_GUI |
118 | ||
0470b1e6 VZ |
119 | // ---------------------------------------------------------------------------- |
120 | // wxStopWatch | |
121 | // ---------------------------------------------------------------------------- | |
c801d85f | 122 | |
0470b1e6 VZ |
123 | void wxStopWatch::Start(long t) |
124 | { | |
b8f04990 | 125 | m_t0 = wxGetLocalTimeMillis() - t; |
0470b1e6 VZ |
126 | m_pause = 0; |
127 | } | |
128 | ||
92da8bde | 129 | long wxStopWatch::GetElapsedTime() const |
0470b1e6 | 130 | { |
fd7cf532 | 131 | return (wxGetLocalTimeMillis() - m_t0).GetLo(); |
b8f04990 GRG |
132 | } |
133 | ||
8242fb3b | 134 | long wxStopWatch::Time() const |
b8f04990 | 135 | { |
8242fb3b | 136 | return (m_pause ? m_pause : GetElapsedTime()); |
0470b1e6 VZ |
137 | } |
138 | ||
139 | // ---------------------------------------------------------------------------- | |
140 | // old timer functions superceded by wxStopWatch | |
141 | // ---------------------------------------------------------------------------- | |
c801d85f | 142 | |
cd0b1709 | 143 | static wxLongLong wxStartTime = 0l; |
c801d85f | 144 | |
0470b1e6 VZ |
145 | // starts the global timer |
146 | void wxStartTimer() | |
c801d85f | 147 | { |
b8f04990 | 148 | wxStartTime = wxGetLocalTimeMillis(); |
c801d85f KB |
149 | } |
150 | ||
151 | // Returns elapsed time in milliseconds | |
152 | long wxGetElapsedTime(bool resetTimer) | |
f0599ea9 | 153 | { |
b8f04990 GRG |
154 | wxLongLong oldTime = wxStartTime; |
155 | wxLongLong newTime = wxGetLocalTimeMillis(); | |
f0599ea9 | 156 | |
0470b1e6 VZ |
157 | if ( resetTimer ) |
158 | wxStartTime = newTime; | |
159 | ||
b8f04990 | 160 | return (newTime - oldTime).GetLo(); |
f0599ea9 SB |
161 | } |
162 | ||
163 | ||
0470b1e6 VZ |
164 | // ---------------------------------------------------------------------------- |
165 | // the functions to get the current time and timezone info | |
166 | // ---------------------------------------------------------------------------- | |
167 | ||
b8f04990 GRG |
168 | // Get local time as seconds since 00:00:00, Jan 1st 1970 |
169 | long wxGetLocalTime() | |
c801d85f | 170 | { |
b8f04990 GRG |
171 | struct tm tm; |
172 | time_t t0, t1; | |
173 | ||
174 | // This cannot be made static because mktime can overwrite it. | |
175 | // | |
176 | memset(&tm, 0, sizeof(tm)); | |
177 | tm.tm_year = 70; | |
178 | tm.tm_mon = 0; | |
179 | tm.tm_mday = 5; // not Jan 1st 1970 due to mktime 'feature' | |
180 | tm.tm_hour = 0; | |
181 | tm.tm_min = 0; | |
182 | tm.tm_sec = 0; | |
183 | tm.tm_isdst = -1; // let mktime guess | |
184 | ||
185 | // Note that mktime assumes that the struct tm contains local time. | |
186 | // | |
187 | t1 = time(&t1); // now | |
188 | t0 = mktime(&tm); // origin | |
189 | ||
190 | // Return the difference in seconds. | |
191 | // | |
192 | if (( t0 != (time_t)-1 ) && ( t1 != (time_t)-1 )) | |
193 | return (long)difftime(t1, t0) + (60 * 60 * 24 * 4); | |
194 | ||
195 | wxLogSysError(_("Failed to get the local system time")); | |
196 | return -1; | |
197 | } | |
b76b015e | 198 | |
b8f04990 GRG |
199 | // Get UTC time as seconds since 00:00:00, Jan 1st 1970 |
200 | long wxGetUTCTime() | |
201 | { | |
202 | struct tm tm, *ptm; | |
203 | time_t t0, t1; | |
204 | ||
205 | // This cannot be made static because mktime can overwrite it | |
206 | // | |
207 | memset(&tm, 0, sizeof(tm)); | |
208 | tm.tm_year = 70; | |
209 | tm.tm_mon = 0; | |
210 | tm.tm_mday = 5; // not Jan 1st 1970 due to mktime 'feature' | |
211 | tm.tm_hour = 0; | |
212 | tm.tm_min = 0; | |
213 | tm.tm_sec = 0; | |
214 | tm.tm_isdst = -1; // let mktime guess | |
215 | ||
216 | // Note that mktime assumes that the struct tm contains local time. | |
217 | // | |
218 | t1 = time(&t1); // now | |
219 | t0 = mktime(&tm); // origin in localtime | |
220 | ||
221 | if (( t0 != (time_t)-1 ) && ( t1 != (time_t)-1 )) | |
0470b1e6 | 222 | { |
b8f04990 GRG |
223 | // To get t0 as GMT we convert to a struct tm with gmtime, |
224 | // and then back again. | |
225 | // | |
226 | ptm = gmtime(&t0); | |
0470b1e6 | 227 | |
b8f04990 | 228 | if (ptm) |
0470b1e6 | 229 | { |
b8f04990 GRG |
230 | memcpy(&tm, ptm, sizeof(tm)); |
231 | t0 = mktime(&tm); | |
232 | ||
233 | if (t0 != (time_t)-1 ) | |
234 | return (long)difftime(t1, t0) + (60 * 60 * 24 * 4); | |
2c8e4738 VZ |
235 | wxLogSysError(_("mktime() failed")); |
236 | } | |
237 | else | |
238 | { | |
239 | wxLogSysError(_("gmtime() failed")); | |
0470b1e6 VZ |
240 | } |
241 | } | |
2c8e4738 VZ |
242 | |
243 | wxLogError(_("Failed to get the UTC system time.")); | |
244 | ||
b8f04990 GRG |
245 | return -1; |
246 | } | |
247 | ||
248 | ||
249 | // Get local time as milliseconds since 00:00:00, Jan 1st 1970 | |
250 | wxLongLong wxGetLocalTimeMillis() | |
251 | { | |
0158f584 SB |
252 | wxLongLong val = 1000l; |
253 | ||
658c5400 GRG |
254 | // If possible, use a functin which avoids conversions from |
255 | // broken-up time structures to milliseconds, | |
256 | ||
0158f584 SB |
257 | #if defined(HAVE_GETTIMEOFDAY) |
258 | struct timeval tp; | |
259 | if ( wxGetTimeOfDay(&tp, (struct timezone *)NULL) != -1 ) | |
260 | { | |
261 | val *= tp.tv_sec; | |
262 | return (val + (tp.tv_usec / 1000)); | |
263 | } | |
91ff98b7 GRG |
264 | else |
265 | { | |
266 | wxLogError(_("wxGetTimeOfDay failed.")); | |
267 | return 0; | |
268 | } | |
658c5400 GRG |
269 | #elif defined(HAVE_FTIME) |
270 | struct timeb tp; | |
2c8e4738 | 271 | |
91ff98b7 GRG |
272 | // ftime() is void and not int in some mingw32 headers, so don't |
273 | // test the return code (well, it shouldn't fail anyhow...) | |
2c8e4738 VZ |
274 | (void)ftime(&tp); |
275 | val *= tp.time; | |
276 | return (val + tp.millitm); | |
0158f584 | 277 | #else |
b8f04990 GRG |
278 | // We use wxGetLocalTime() to get the seconds since |
279 | // 00:00:00 Jan 1st 1970 and then whatever is available | |
280 | // to get millisecond resolution. | |
658c5400 | 281 | // |
91ff98b7 GRG |
282 | // NOTE that this might lead to a problem if the clocks |
283 | // use different sources, so this approach should be | |
284 | // avoided where possible. | |
658c5400 | 285 | |
cd0b1709 | 286 | val *= wxGetLocalTime(); |
b8f04990 | 287 | |
91ff98b7 | 288 | #if defined(__VISAGECPP__) |
658c5400 | 289 | DATETIME dt; |
c67b773e | 290 | ::DosGetDateTime(&dt); |
658c5400 | 291 | val += (dt.hundredths*10); |
91ff98b7 GRG |
292 | #elif defined (__WIN32__) |
293 | #warning "Possible clock skew bug in wxStopWatch!" | |
294 | SYSTEMTIME st; | |
295 | ::GetLocalTime(&st); | |
296 | val += st.wMilliseconds; | |
658c5400 | 297 | #else |
5fa399c9 | 298 | #ifndef __VISUALC__ |
658c5400 | 299 | #warning "wxStopWatch will be up to second resolution!" |
5fa399c9 | 300 | #endif |
c801d85f | 301 | #endif |
c801d85f | 302 | |
b8f04990 | 303 | return val; |
91ff98b7 | 304 | |
658c5400 | 305 | #endif |
c801d85f | 306 | } |