]> git.saurik.com Git - wxWidgets.git/blame - src/common/timercmn.cpp
In SetProxy, add 1 to pos when parsing port since otherwise we include the colon.
[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
ba14d986 52#if defined(__WIN32__) && !defined(HAVE_FTIME) && !defined(__MWERKS__)
91ff98b7 53 #define HAVE_FTIME
91ff98b7
GRG
54#endif
55
47af9124
GRG
56#if defined(__VISAGECPP__) && !defined(HAVE_FTIME)
57 #define HAVE_FTIME
07d7f20b
DW
58# if __IBMCPP__ >= 400
59 # define ftime(x) _ftime(x)
60# endif
47af9124
GRG
61#endif
62
de85a884
VZ
63#if defined(__MWERKS__) && defined(__WXMSW__)
64# undef HAVE_FTIME
65# undef HAVE_GETTIMEOFDAY
66#endif
67
b8f04990
GRG
68#include <time.h>
69#ifndef __WXMAC__
70 #include <sys/types.h> // for time_t
07cf98cb
VZ
71#endif
72
0470b1e6
VZ
73#if defined(HAVE_GETTIMEOFDAY)
74 #include <sys/time.h>
75 #include <unistd.h>
0470b1e6
VZ
76#elif defined(HAVE_FTIME)
77 #include <sys/timeb.h>
c801d85f
KB
78#endif
79
ed791986
VZ
80// ----------------------------------------------------------------------------
81// wxWin macros
82// ----------------------------------------------------------------------------
83
1e6feb95 84#if wxUSE_GUI && wxUSE_TIMER
52a07708
VZ
85 IMPLEMENT_DYNAMIC_CLASS(wxTimerEvent, wxEvent)
86#endif // wxUSE_GUI
ed791986 87
0470b1e6
VZ
88// ----------------------------------------------------------------------------
89// macros
90// ----------------------------------------------------------------------------
ce3ed50d 91
0470b1e6
VZ
92// on some really old systems gettimeofday() doesn't have the second argument,
93// define wxGetTimeOfDay() to hide this difference
94#ifdef HAVE_GETTIMEOFDAY
95 #ifdef WX_GETTIMEOFDAY_NO_TZ
96 struct timezone;
97 #define wxGetTimeOfDay(tv, tz) gettimeofday(tv)
98 #else
99 #define wxGetTimeOfDay(tv, tz) gettimeofday((tv), (tz))
100 #endif
101#endif // HAVE_GETTIMEOFDAY
c801d85f 102
0470b1e6
VZ
103// ============================================================================
104// implementation
105// ============================================================================
c801d85f 106
ed791986
VZ
107// ----------------------------------------------------------------------------
108// wxTimerBase
109// ----------------------------------------------------------------------------
110
1e6feb95 111#if wxUSE_GUI && wxUSE_TIMER
91ff98b7 112
799ea011
GD
113wxTimerBase::~wxTimerBase()
114{
115 // this destructor is required for Darwin
116}
117
ed791986
VZ
118void wxTimerBase::Notify()
119{
120 // the base class version generates an event if it has owner - which it
121 // should because otherwise nobody can process timer events
122 wxCHECK_RET( m_owner, _T("wxTimer::Notify() should be overridden.") );
123
124 wxTimerEvent event(m_idTimer, m_milli);
125 (void)m_owner->ProcessEvent(event);
126}
b8f04990 127
99646f7e
VZ
128bool wxTimerBase::Start(int milliseconds, bool oneShot)
129{
130 if ( IsRunning() )
131 {
132 // not stopping the already running timer might work for some
133 // platforms (no problems under MSW) but leads to mysterious crashes
134 // on the others (GTK), so to be on the safe side do it here
135 Stop();
136 }
137
138 if ( milliseconds != -1 )
139 {
140 m_milli = milliseconds;
141 }
142
143 m_oneShot = oneShot;
144
145 return TRUE;
146}
147
52a07708
VZ
148#endif // wxUSE_GUI
149
0470b1e6
VZ
150// ----------------------------------------------------------------------------
151// wxStopWatch
152// ----------------------------------------------------------------------------
c801d85f 153
1e6feb95
VZ
154#if wxUSE_LONGLONG
155
0470b1e6
VZ
156void wxStopWatch::Start(long t)
157{
b8f04990 158 m_t0 = wxGetLocalTimeMillis() - t;
0470b1e6
VZ
159 m_pause = 0;
160}
161
92da8bde 162long wxStopWatch::GetElapsedTime() const
0470b1e6 163{
677eff07 164 return (wxGetLocalTimeMillis() - m_t0).GetLo();
b8f04990
GRG
165}
166
8242fb3b 167long wxStopWatch::Time() const
b8f04990 168{
677eff07 169 return m_pauseCount ? m_pause : GetElapsedTime();
0470b1e6
VZ
170}
171
1e6feb95
VZ
172#endif // wxUSE_LONGLONG
173
0470b1e6
VZ
174// ----------------------------------------------------------------------------
175// old timer functions superceded by wxStopWatch
176// ----------------------------------------------------------------------------
c801d85f 177
1e6feb95
VZ
178#if wxUSE_LONGLONG
179
cd0b1709 180static wxLongLong wxStartTime = 0l;
c801d85f 181
0470b1e6
VZ
182// starts the global timer
183void wxStartTimer()
c801d85f 184{
b8f04990 185 wxStartTime = wxGetLocalTimeMillis();
c801d85f
KB
186}
187
188// Returns elapsed time in milliseconds
189long wxGetElapsedTime(bool resetTimer)
f0599ea9 190{
b8f04990
GRG
191 wxLongLong oldTime = wxStartTime;
192 wxLongLong newTime = wxGetLocalTimeMillis();
f0599ea9 193
0470b1e6
VZ
194 if ( resetTimer )
195 wxStartTime = newTime;
196
b8f04990 197 return (newTime - oldTime).GetLo();
f0599ea9
SB
198}
199
1e6feb95 200#endif // wxUSE_LONGLONG
f0599ea9 201
0470b1e6
VZ
202// ----------------------------------------------------------------------------
203// the functions to get the current time and timezone info
204// ----------------------------------------------------------------------------
205
b8f04990
GRG
206// Get local time as seconds since 00:00:00, Jan 1st 1970
207long wxGetLocalTime()
c801d85f 208{
b8f04990
GRG
209 struct tm tm;
210 time_t t0, t1;
211
212 // This cannot be made static because mktime can overwrite it.
213 //
214 memset(&tm, 0, sizeof(tm));
215 tm.tm_year = 70;
216 tm.tm_mon = 0;
217 tm.tm_mday = 5; // not Jan 1st 1970 due to mktime 'feature'
218 tm.tm_hour = 0;
219 tm.tm_min = 0;
220 tm.tm_sec = 0;
221 tm.tm_isdst = -1; // let mktime guess
222
223 // Note that mktime assumes that the struct tm contains local time.
224 //
225 t1 = time(&t1); // now
226 t0 = mktime(&tm); // origin
227
228 // Return the difference in seconds.
229 //
230 if (( t0 != (time_t)-1 ) && ( t1 != (time_t)-1 ))
231 return (long)difftime(t1, t0) + (60 * 60 * 24 * 4);
232
233 wxLogSysError(_("Failed to get the local system time"));
234 return -1;
235}
b76b015e 236
b8f04990
GRG
237// Get UTC time as seconds since 00:00:00, Jan 1st 1970
238long wxGetUTCTime()
239{
33ac7e6f 240 struct tm tm;
1e6feb95 241 struct tm *ptm;
b8f04990
GRG
242 time_t t0, t1;
243
244 // This cannot be made static because mktime can overwrite it
245 //
246 memset(&tm, 0, sizeof(tm));
247 tm.tm_year = 70;
248 tm.tm_mon = 0;
249 tm.tm_mday = 5; // not Jan 1st 1970 due to mktime 'feature'
250 tm.tm_hour = 0;
251 tm.tm_min = 0;
252 tm.tm_sec = 0;
253 tm.tm_isdst = -1; // let mktime guess
254
255 // Note that mktime assumes that the struct tm contains local time.
256 //
257 t1 = time(&t1); // now
258 t0 = mktime(&tm); // origin in localtime
259
260 if (( t0 != (time_t)-1 ) && ( t1 != (time_t)-1 ))
0470b1e6 261 {
b8f04990
GRG
262 // To get t0 as GMT we convert to a struct tm with gmtime,
263 // and then back again.
264 //
265 ptm = gmtime(&t0);
0470b1e6 266
b8f04990 267 if (ptm)
0470b1e6 268 {
b8f04990
GRG
269 memcpy(&tm, ptm, sizeof(tm));
270 t0 = mktime(&tm);
271
272 if (t0 != (time_t)-1 )
273 return (long)difftime(t1, t0) + (60 * 60 * 24 * 4);
2c8e4738
VZ
274 wxLogSysError(_("mktime() failed"));
275 }
276 else
277 {
278 wxLogSysError(_("gmtime() failed"));
0470b1e6
VZ
279 }
280 }
2c8e4738
VZ
281
282 wxLogError(_("Failed to get the UTC system time."));
283
b8f04990
GRG
284 return -1;
285}
286
1e6feb95 287#if wxUSE_LONGLONG
b8f04990
GRG
288
289// Get local time as milliseconds since 00:00:00, Jan 1st 1970
290wxLongLong wxGetLocalTimeMillis()
291{
0158f584
SB
292 wxLongLong val = 1000l;
293
9e3cb9ee
JS
294 // If possible, use a function which avoids conversions from
295 // broken-up time structures to milliseconds
658c5400 296
ba14d986
VZ
297#if defined(__WXMSW__) && defined(__MWERKS__)
298 // This should probably be the way all WXMSW compilers should do it
299 // Go direct to the OS for time
300
301 SYSTEMTIME thenst = { 1970, 1, 4, 1, 0, 0, 0, 0 }; // 00:00:00 Jan 1st 1970
302 FILETIME thenft;
303 SystemTimeToFileTime( &thenst, &thenft );
304 wxLongLong then( thenft.dwHighDateTime, thenft.dwLowDateTime ); // time in 100 nanoseconds
305
306 SYSTEMTIME nowst;
307 GetLocalTime( &nowst );
308 FILETIME nowft;
309 SystemTimeToFileTime( &nowst, &nowft );
310 wxLongLong now( nowft.dwHighDateTime, nowft.dwLowDateTime ); // time in 100 nanoseconds
311
312 return ( now - then ) / 10000.0; // time from 00:00:00 Jan 1st 1970 to now in milliseconds
313
314#elif defined(HAVE_GETTIMEOFDAY)
0158f584
SB
315 struct timeval tp;
316 if ( wxGetTimeOfDay(&tp, (struct timezone *)NULL) != -1 )
317 {
318 val *= tp.tv_sec;
319 return (val + (tp.tv_usec / 1000));
320 }
91ff98b7
GRG
321 else
322 {
323 wxLogError(_("wxGetTimeOfDay failed."));
324 return 0;
325 }
658c5400
GRG
326#elif defined(HAVE_FTIME)
327 struct timeb tp;
2c8e4738 328
91ff98b7
GRG
329 // ftime() is void and not int in some mingw32 headers, so don't
330 // test the return code (well, it shouldn't fail anyhow...)
2c8e4738
VZ
331 (void)ftime(&tp);
332 val *= tp.time;
333 return (val + tp.millitm);
f6bcfd97 334#else // no gettimeofday() nor ftime()
b8f04990
GRG
335 // We use wxGetLocalTime() to get the seconds since
336 // 00:00:00 Jan 1st 1970 and then whatever is available
337 // to get millisecond resolution.
658c5400 338 //
91ff98b7
GRG
339 // NOTE that this might lead to a problem if the clocks
340 // use different sources, so this approach should be
341 // avoided where possible.
658c5400 342
cd0b1709 343 val *= wxGetLocalTime();
b8f04990 344
47af9124
GRG
345// GRG: This will go soon as all WIN32 seem to have ftime
346#if defined (__WIN32__)
f6bcfd97
BP
347 // If your platform/compiler needs to use two different functions
348 // to get ms resolution, please do NOT just shut off these warnings,
349 // drop me a line instead at <guille@iies.es>
350 #warning "Possible clock skew bug in wxGetLocalTimeMillis()!"
351
91ff98b7
GRG
352 SYSTEMTIME st;
353 ::GetLocalTime(&st);
354 val += st.wMilliseconds;
f6bcfd97
BP
355#else // !Win32
356 // If your platform/compiler does not support ms resolution please
357 // do NOT just shut off these warnings, drop me a line instead at
358 // <guille@iies.es>
359
19193a2c 360 #if defined(__VISUALC__) || defined (__WATCOMC__)
f6bcfd97
BP
361 #pragma message("wxStopWatch will be up to second resolution!")
362 #elif defined(__BORLANDC__)
363 #pragma message "wxStopWatch will be up to second resolution!"
ec7cd4c4 364 #else
f6bcfd97
BP
365 #warning "wxStopWatch will be up to second resolution!"
366 #endif // compiler
c801d85f 367#endif
c801d85f 368
b8f04990 369 return val;
91ff98b7 370
f6bcfd97 371#endif // time functions
c801d85f 372}
1e6feb95
VZ
373
374#endif // wxUSE_LONGLONG
375