]>
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 | // ---------------------------------------------------------------------------- | |
22 | // headers | |
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 | |
b8f04990 GRG |
44 | #if defined(__WIN32__) |
45 | #include <windows.h> | |
0470b1e6 VZ |
46 | #endif |
47 | ||
b8f04990 GRG |
48 | #include <time.h> |
49 | #ifndef __WXMAC__ | |
50 | #include <sys/types.h> // for time_t | |
07cf98cb VZ |
51 | #endif |
52 | ||
0470b1e6 VZ |
53 | #if defined(HAVE_GETTIMEOFDAY) |
54 | #include <sys/time.h> | |
55 | #include <unistd.h> | |
0470b1e6 VZ |
56 | #elif defined(HAVE_FTIME) |
57 | #include <sys/timeb.h> | |
c801d85f KB |
58 | #endif |
59 | ||
ed791986 VZ |
60 | // ---------------------------------------------------------------------------- |
61 | // wxWin macros | |
62 | // ---------------------------------------------------------------------------- | |
63 | ||
52a07708 VZ |
64 | #if wxUSE_GUI |
65 | IMPLEMENT_DYNAMIC_CLASS(wxTimerEvent, wxEvent) | |
66 | #endif // wxUSE_GUI | |
ed791986 | 67 | |
0470b1e6 VZ |
68 | // ---------------------------------------------------------------------------- |
69 | // macros | |
70 | // ---------------------------------------------------------------------------- | |
ce3ed50d | 71 | |
0470b1e6 VZ |
72 | // on some really old systems gettimeofday() doesn't have the second argument, |
73 | // define wxGetTimeOfDay() to hide this difference | |
74 | #ifdef HAVE_GETTIMEOFDAY | |
75 | #ifdef WX_GETTIMEOFDAY_NO_TZ | |
76 | struct timezone; | |
77 | #define wxGetTimeOfDay(tv, tz) gettimeofday(tv) | |
78 | #else | |
79 | #define wxGetTimeOfDay(tv, tz) gettimeofday((tv), (tz)) | |
80 | #endif | |
81 | #endif // HAVE_GETTIMEOFDAY | |
c801d85f | 82 | |
ed791986 VZ |
83 | // ---------------------------------------------------------------------------- |
84 | // prototypes | |
85 | // ---------------------------------------------------------------------------- | |
86 | ||
87 | wxLongLong wxGetLocalTimeMillis(); | |
88 | ||
0470b1e6 VZ |
89 | // ============================================================================ |
90 | // implementation | |
91 | // ============================================================================ | |
c801d85f | 92 | |
52a07708 VZ |
93 | #if wxUSE_GUI |
94 | ||
ed791986 VZ |
95 | // ---------------------------------------------------------------------------- |
96 | // wxTimerBase | |
97 | // ---------------------------------------------------------------------------- | |
98 | ||
99 | void wxTimerBase::Notify() | |
100 | { | |
101 | // the base class version generates an event if it has owner - which it | |
102 | // should because otherwise nobody can process timer events | |
103 | wxCHECK_RET( m_owner, _T("wxTimer::Notify() should be overridden.") ); | |
104 | ||
105 | wxTimerEvent event(m_idTimer, m_milli); | |
106 | (void)m_owner->ProcessEvent(event); | |
107 | } | |
b8f04990 | 108 | |
52a07708 VZ |
109 | #endif // wxUSE_GUI |
110 | ||
0470b1e6 VZ |
111 | // ---------------------------------------------------------------------------- |
112 | // wxStopWatch | |
113 | // ---------------------------------------------------------------------------- | |
c801d85f | 114 | |
0470b1e6 VZ |
115 | void wxStopWatch::Start(long t) |
116 | { | |
b8f04990 | 117 | m_t0 = wxGetLocalTimeMillis() - t; |
0470b1e6 VZ |
118 | m_pause = 0; |
119 | } | |
120 | ||
92da8bde | 121 | long wxStopWatch::GetElapsedTime() const |
0470b1e6 | 122 | { |
fd7cf532 | 123 | return (wxGetLocalTimeMillis() - m_t0).GetLo(); |
b8f04990 GRG |
124 | } |
125 | ||
8242fb3b | 126 | long wxStopWatch::Time() const |
b8f04990 | 127 | { |
8242fb3b | 128 | return (m_pause ? m_pause : GetElapsedTime()); |
0470b1e6 VZ |
129 | } |
130 | ||
131 | // ---------------------------------------------------------------------------- | |
132 | // old timer functions superceded by wxStopWatch | |
133 | // ---------------------------------------------------------------------------- | |
c801d85f | 134 | |
cd0b1709 | 135 | static wxLongLong wxStartTime = 0l; |
c801d85f | 136 | |
0470b1e6 VZ |
137 | // starts the global timer |
138 | void wxStartTimer() | |
c801d85f | 139 | { |
b8f04990 | 140 | wxStartTime = wxGetLocalTimeMillis(); |
c801d85f KB |
141 | } |
142 | ||
143 | // Returns elapsed time in milliseconds | |
144 | long wxGetElapsedTime(bool resetTimer) | |
f0599ea9 | 145 | { |
b8f04990 GRG |
146 | wxLongLong oldTime = wxStartTime; |
147 | wxLongLong newTime = wxGetLocalTimeMillis(); | |
f0599ea9 | 148 | |
0470b1e6 VZ |
149 | if ( resetTimer ) |
150 | wxStartTime = newTime; | |
151 | ||
b8f04990 | 152 | return (newTime - oldTime).GetLo(); |
f0599ea9 SB |
153 | } |
154 | ||
155 | ||
0470b1e6 VZ |
156 | // ---------------------------------------------------------------------------- |
157 | // the functions to get the current time and timezone info | |
158 | // ---------------------------------------------------------------------------- | |
159 | ||
b8f04990 GRG |
160 | // Get local time as seconds since 00:00:00, Jan 1st 1970 |
161 | long wxGetLocalTime() | |
c801d85f | 162 | { |
b8f04990 GRG |
163 | struct tm tm; |
164 | time_t t0, t1; | |
165 | ||
166 | // This cannot be made static because mktime can overwrite it. | |
167 | // | |
168 | memset(&tm, 0, sizeof(tm)); | |
169 | tm.tm_year = 70; | |
170 | tm.tm_mon = 0; | |
171 | tm.tm_mday = 5; // not Jan 1st 1970 due to mktime 'feature' | |
172 | tm.tm_hour = 0; | |
173 | tm.tm_min = 0; | |
174 | tm.tm_sec = 0; | |
175 | tm.tm_isdst = -1; // let mktime guess | |
176 | ||
177 | // Note that mktime assumes that the struct tm contains local time. | |
178 | // | |
179 | t1 = time(&t1); // now | |
180 | t0 = mktime(&tm); // origin | |
181 | ||
182 | // Return the difference in seconds. | |
183 | // | |
184 | if (( t0 != (time_t)-1 ) && ( t1 != (time_t)-1 )) | |
185 | return (long)difftime(t1, t0) + (60 * 60 * 24 * 4); | |
186 | ||
187 | wxLogSysError(_("Failed to get the local system time")); | |
188 | return -1; | |
189 | } | |
b76b015e | 190 | |
b8f04990 GRG |
191 | // Get UTC time as seconds since 00:00:00, Jan 1st 1970 |
192 | long wxGetUTCTime() | |
193 | { | |
194 | struct tm tm, *ptm; | |
195 | time_t t0, t1; | |
196 | ||
197 | // This cannot be made static because mktime can overwrite it | |
198 | // | |
199 | memset(&tm, 0, sizeof(tm)); | |
200 | tm.tm_year = 70; | |
201 | tm.tm_mon = 0; | |
202 | tm.tm_mday = 5; // not Jan 1st 1970 due to mktime 'feature' | |
203 | tm.tm_hour = 0; | |
204 | tm.tm_min = 0; | |
205 | tm.tm_sec = 0; | |
206 | tm.tm_isdst = -1; // let mktime guess | |
207 | ||
208 | // Note that mktime assumes that the struct tm contains local time. | |
209 | // | |
210 | t1 = time(&t1); // now | |
211 | t0 = mktime(&tm); // origin in localtime | |
212 | ||
213 | if (( t0 != (time_t)-1 ) && ( t1 != (time_t)-1 )) | |
0470b1e6 | 214 | { |
b8f04990 GRG |
215 | // To get t0 as GMT we convert to a struct tm with gmtime, |
216 | // and then back again. | |
217 | // | |
218 | ptm = gmtime(&t0); | |
0470b1e6 | 219 | |
b8f04990 | 220 | if (ptm) |
0470b1e6 | 221 | { |
b8f04990 GRG |
222 | memcpy(&tm, ptm, sizeof(tm)); |
223 | t0 = mktime(&tm); | |
224 | ||
225 | if (t0 != (time_t)-1 ) | |
226 | return (long)difftime(t1, t0) + (60 * 60 * 24 * 4); | |
227 | wxLogSysError(_("Failed 2nd mktime")); | |
0470b1e6 | 228 | } |
b8f04990 | 229 | wxLogSysError(_("Failed gmtime")); |
0470b1e6 | 230 | } |
b8f04990 GRG |
231 | wxLogSysError(_("Failed to get the UTC system time")); |
232 | return -1; | |
233 | } | |
234 | ||
235 | ||
236 | // Get local time as milliseconds since 00:00:00, Jan 1st 1970 | |
237 | wxLongLong wxGetLocalTimeMillis() | |
238 | { | |
0158f584 SB |
239 | wxLongLong val = 1000l; |
240 | ||
241 | #if defined(HAVE_GETTIMEOFDAY) | |
242 | struct timeval tp; | |
243 | if ( wxGetTimeOfDay(&tp, (struct timezone *)NULL) != -1 ) | |
244 | { | |
245 | val *= tp.tv_sec; | |
246 | return (val + (tp.tv_usec / 1000)); | |
247 | } | |
248 | #else | |
249 | ||
b8f04990 GRG |
250 | // We use wxGetLocalTime() to get the seconds since |
251 | // 00:00:00 Jan 1st 1970 and then whatever is available | |
252 | // to get millisecond resolution. | |
0158f584 | 253 | // THIS LEADS TO A BUG SINCE REFERENCE TIME ARE DIFFERENT |
cd0b1709 | 254 | val *= wxGetLocalTime(); |
b8f04990 GRG |
255 | |
256 | // If we got here, do not fail even if we can't get | |
257 | // millisecond resolution. | |
258 | // | |
259 | #if defined(__WIN32__) | |
260 | SYSTEMTIME st; | |
261 | ::GetLocalTime(&st); | |
262 | return (val + st.wMilliseconds); | |
c67b773e DW |
263 | #elif defined(__VISAGECPP__) |
264 | DATETIME dt; | |
265 | ::DosGetDateTime(&dt); | |
266 | return (val + dt.hundredths*10); | |
0470b1e6 VZ |
267 | #elif defined(HAVE_FTIME) |
268 | struct timeb tp; | |
269 | if ( ftime(&tp) == 0 ) | |
270 | { | |
b8f04990 | 271 | return (val + tp.millitm); |
0470b1e6 | 272 | } |
0158f584 | 273 | #elif !defined(__BORLANDC__) && !(defined(__VISUALC__) && defined(__WIN16__)) |
2286341c | 274 | #warning "wxStopWatch will be up to second resolution!" |
790ad94f | 275 | #endif |
c801d85f | 276 | #endif |
c801d85f | 277 | |
b8f04990 | 278 | return val; |
c801d85f | 279 | } |