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