]> git.saurik.com Git - wxWidgets.git/blob - src/common/stopwatch.cpp
wxBase/GUI separation: 1st step, wxMSW should build, all the rest is broken
[wxWidgets.git] / src / common / stopwatch.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: common/stopwatch.cpp
3 // Purpose: wxStopWatch and other non-GUI stuff from wx/timer.h
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: 20.06.2003 (extracted from common/timercmn.cpp)
11 // RCS-ID: $Id$
12 // Copyright: (c) 1998-2003 wxWindows Team
13 // License: wxWindows license
14 ///////////////////////////////////////////////////////////////////////////////
15
16 // ============================================================================
17 // declarations
18 // ============================================================================
19
20 // ----------------------------------------------------------------------------
21 // headers
22 // ----------------------------------------------------------------------------
23
24 // for compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #endif //WX_PRECOMP
33
34 #include "wx/timer.h"
35 #include "wx/longlong.h"
36
37 // ----------------------------------------------------------------------------
38 // System headers
39 // ----------------------------------------------------------------------------
40
41 #if defined(__WIN32__)
42 #include <windows.h>
43 #endif
44
45 #if defined(__WIN32__) && !defined(HAVE_FTIME) && !defined(__MWERKS__)
46 #define HAVE_FTIME
47 #endif
48
49 #if defined(__VISAGECPP__) && !defined(HAVE_FTIME)
50 #define HAVE_FTIME
51 # if __IBMCPP__ >= 400
52 # define ftime(x) _ftime(x)
53 # endif
54 #endif
55
56 #if defined(__MWERKS__) && defined(__WXMSW__)
57 # undef HAVE_FTIME
58 # undef HAVE_GETTIMEOFDAY
59 #endif
60
61 #include <time.h>
62 #ifndef __WXMAC__
63 #include <sys/types.h> // for time_t
64 #endif
65
66 #if defined(HAVE_GETTIMEOFDAY)
67 #include <sys/time.h>
68 #include <unistd.h>
69 #elif defined(HAVE_FTIME)
70 #include <sys/timeb.h>
71 #endif
72
73 #ifdef __WXMAC__
74 #include <Timer.h>
75 #include <DriverServices.h>
76 #endif
77
78 // ----------------------------------------------------------------------------
79 // macros
80 // ----------------------------------------------------------------------------
81
82 // on some really old systems gettimeofday() doesn't have the second argument,
83 // define wxGetTimeOfDay() to hide this difference
84 #ifdef HAVE_GETTIMEOFDAY
85 #ifdef WX_GETTIMEOFDAY_NO_TZ
86 struct timezone;
87 #define wxGetTimeOfDay(tv, tz) gettimeofday(tv)
88 #else
89 #define wxGetTimeOfDay(tv, tz) gettimeofday((tv), (tz))
90 #endif
91 #endif // HAVE_GETTIMEOFDAY
92
93 // ============================================================================
94 // implementation
95 // ============================================================================
96
97 // ----------------------------------------------------------------------------
98 // wxStopWatch
99 // ----------------------------------------------------------------------------
100
101 #if wxUSE_STOPWATCH
102
103 void wxStopWatch::Start(long t)
104 {
105 m_t0 = wxGetLocalTimeMillis() - t;
106 m_pause = 0;
107 m_pauseCount = 0;
108 }
109
110 long wxStopWatch::GetElapsedTime() const
111 {
112 return (wxGetLocalTimeMillis() - m_t0).GetLo();
113 }
114
115 long wxStopWatch::Time() const
116 {
117 return m_pauseCount ? m_pause : GetElapsedTime();
118 }
119
120 #endif // wxUSE_STOPWATCH
121
122 // ----------------------------------------------------------------------------
123 // old timer functions superceded by wxStopWatch
124 // ----------------------------------------------------------------------------
125
126 #if wxUSE_LONGLONG
127
128 static wxLongLong wxStartTime = 0l;
129
130 // starts the global timer
131 void wxStartTimer()
132 {
133 wxStartTime = wxGetLocalTimeMillis();
134 }
135
136 // Returns elapsed time in milliseconds
137 long wxGetElapsedTime(bool resetTimer)
138 {
139 wxLongLong oldTime = wxStartTime;
140 wxLongLong newTime = wxGetLocalTimeMillis();
141
142 if ( resetTimer )
143 wxStartTime = newTime;
144
145 return (newTime - oldTime).GetLo();
146 }
147
148 #endif // wxUSE_LONGLONG
149
150 // ----------------------------------------------------------------------------
151 // the functions to get the current time and timezone info
152 // ----------------------------------------------------------------------------
153
154 // Get local time as seconds since 00:00:00, Jan 1st 1970
155 long wxGetLocalTime()
156 {
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 }
184
185 // Get UTC time as seconds since 00:00:00, Jan 1st 1970
186 long wxGetUTCTime()
187 {
188 struct tm tm;
189 struct tm *ptm;
190 time_t t0, t1;
191
192 // This cannot be made static because mktime can overwrite it
193 //
194 memset(&tm, 0, sizeof(tm));
195 tm.tm_year = 70;
196 tm.tm_mon = 0;
197 tm.tm_mday = 5; // not Jan 1st 1970 due to mktime 'feature'
198 tm.tm_hour = 0;
199 tm.tm_min = 0;
200 tm.tm_sec = 0;
201 tm.tm_isdst = -1; // let mktime guess
202
203 // Note that mktime assumes that the struct tm contains local time.
204 //
205 t1 = time(&t1); // now
206 t0 = mktime(&tm); // origin in localtime
207
208 if (( t0 != (time_t)-1 ) && ( t1 != (time_t)-1 ))
209 {
210 // To get t0 as GMT we convert to a struct tm with gmtime,
211 // and then back again.
212 //
213 ptm = gmtime(&t0);
214
215 if (ptm)
216 {
217 memcpy(&tm, ptm, sizeof(tm));
218 t0 = mktime(&tm);
219
220 if (t0 != (time_t)-1 )
221 return (long)difftime(t1, t0) + (60 * 60 * 24 * 4);
222 wxLogSysError(_("mktime() failed"));
223 }
224 else
225 {
226 wxLogSysError(_("gmtime() failed"));
227 }
228 }
229
230 wxLogError(_("Failed to get the UTC system time."));
231
232 return -1;
233 }
234
235 #if wxUSE_LONGLONG
236
237 // Get local time as milliseconds since 00:00:00, Jan 1st 1970
238 wxLongLong wxGetLocalTimeMillis()
239 {
240 wxLongLong val = 1000l;
241
242 // If possible, use a function which avoids conversions from
243 // broken-up time structures to milliseconds
244
245 #if defined(__WXMSW__) && defined(__MWERKS__)
246 // This should probably be the way all WXMSW compilers should do it
247 // Go direct to the OS for time
248
249 SYSTEMTIME thenst = { 1970, 1, 4, 1, 0, 0, 0, 0 }; // 00:00:00 Jan 1st 1970
250 FILETIME thenft;
251 SystemTimeToFileTime( &thenst, &thenft );
252 wxLongLong then( thenft.dwHighDateTime, thenft.dwLowDateTime ); // time in 100 nanoseconds
253
254 SYSTEMTIME nowst;
255 GetLocalTime( &nowst );
256 FILETIME nowft;
257 SystemTimeToFileTime( &nowst, &nowft );
258 wxLongLong now( nowft.dwHighDateTime, nowft.dwLowDateTime ); // time in 100 nanoseconds
259
260 return ( now - then ) / 10000.0; // time from 00:00:00 Jan 1st 1970 to now in milliseconds
261
262 #elif defined(HAVE_GETTIMEOFDAY)
263 struct timeval tp;
264 if ( wxGetTimeOfDay(&tp, (struct timezone *)NULL) != -1 )
265 {
266 val *= tp.tv_sec;
267 return (val + (tp.tv_usec / 1000));
268 }
269 else
270 {
271 wxLogError(_("wxGetTimeOfDay failed."));
272 return 0;
273 }
274 #elif defined(HAVE_FTIME)
275 struct timeb tp;
276
277 // ftime() is void and not int in some mingw32 headers, so don't
278 // test the return code (well, it shouldn't fail anyhow...)
279 (void)ftime(&tp);
280 val *= tp.time;
281 return (val + tp.millitm);
282 #elif defined(__WXMAC__)
283
284 static UInt64 gMilliAtStart = 0;
285
286 Nanoseconds upTime = AbsoluteToNanoseconds( UpTime() );
287
288 if ( gMilliAtStart == 0 )
289 {
290 time_t start = time(NULL);
291 gMilliAtStart = ((UInt64) start) * 1000000L;
292 gMilliAtStart -= upTime.lo / 1000 ;
293 gMilliAtStart -= ( ( (UInt64) upTime.hi ) << 32 ) / (1000 * 1000);
294 }
295
296 UInt64 millival = gMilliAtStart;
297 millival += upTime.lo / (1000 * 1000);
298 millival += ( ( (UInt64) upTime.hi ) << 32 ) / (1000 * 1000);
299 val = millival;
300
301 return val;
302 #else // no gettimeofday() nor ftime()
303 // We use wxGetLocalTime() to get the seconds since
304 // 00:00:00 Jan 1st 1970 and then whatever is available
305 // to get millisecond resolution.
306 //
307 // NOTE that this might lead to a problem if the clocks
308 // use different sources, so this approach should be
309 // avoided where possible.
310
311 val *= wxGetLocalTime();
312
313 // GRG: This will go soon as all WIN32 seem to have ftime
314 #if defined (__WIN32__)
315 // If your platform/compiler needs to use two different functions
316 // to get ms resolution, please do NOT just shut off these warnings,
317 // drop me a line instead at <guille@iies.es>
318 #warning "Possible clock skew bug in wxGetLocalTimeMillis()!"
319
320 SYSTEMTIME st;
321 ::GetLocalTime(&st);
322 val += st.wMilliseconds;
323 #else // !Win32
324 // If your platform/compiler does not support ms resolution please
325 // do NOT just shut off these warnings, drop me a line instead at
326 // <guille@iies.es>
327
328 #if defined(__VISUALC__) || defined (__WATCOMC__)
329 #pragma message("wxStopWatch will be up to second resolution!")
330 #elif defined(__BORLANDC__)
331 #pragma message "wxStopWatch will be up to second resolution!"
332 #else
333 #warning "wxStopWatch will be up to second resolution!"
334 #endif // compiler
335 #endif
336
337 return val;
338
339 #endif // time functions
340 }
341
342 #endif // wxUSE_LONGLONG
343
344