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