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