]> git.saurik.com Git - wxWidgets.git/blob - src/common/timercmn.cpp
db.h
[wxWidgets.git] / src / common / timercmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/timercmn.cpp
3 // Purpose: Common timer implementation
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: 04/01/98
11 // RCS-ID: $Id$
12 // Copyright: (c) Julian Smart and Markus Holzem
13 // (c) 1999 Guillermo Rodriguez <guille@iies.es>
14 // Licence: wxWindows license
15 /////////////////////////////////////////////////////////////////////////////
16
17 // ============================================================================
18 // declarations
19 // ============================================================================
20
21 // ----------------------------------------------------------------------------
22 // wxWin headers
23 // ----------------------------------------------------------------------------
24
25 #ifdef __GNUG__
26 #pragma implementation "timerbase.h"
27 #endif
28
29 // For compilers that support precompilation, includes "wx.h".
30 #include "wx/wxprec.h"
31
32 #ifdef __BORLANDC__
33 #pragma hdrstop
34 #endif
35
36 #ifndef WX_PRECOMP
37 #include "wx/intl.h"
38 #include "wx/log.h"
39 #endif
40
41 #include "wx/timer.h"
42 #include "wx/longlong.h"
43
44 // ----------------------------------------------------------------------------
45 // System headers
46 // ----------------------------------------------------------------------------
47
48 #if defined(__WIN32__)
49 #include <windows.h>
50 #endif
51
52 #if defined(__WIN32__) && !defined(HAVE_FTIME)
53 #define HAVE_FTIME
54 #endif
55
56 #include <time.h>
57 #ifndef __WXMAC__
58 #include <sys/types.h> // for time_t
59 #endif
60
61 #if defined(HAVE_GETTIMEOFDAY)
62 #include <sys/time.h>
63 #include <unistd.h>
64 #elif defined(HAVE_FTIME)
65 #include <sys/timeb.h>
66 #endif
67
68 // ----------------------------------------------------------------------------
69 // wxWin macros
70 // ----------------------------------------------------------------------------
71
72 #if wxUSE_GUI
73 IMPLEMENT_DYNAMIC_CLASS(wxTimerEvent, wxEvent)
74 #endif // wxUSE_GUI
75
76 // ----------------------------------------------------------------------------
77 // macros
78 // ----------------------------------------------------------------------------
79
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
90
91 // ----------------------------------------------------------------------------
92 // prototypes
93 // ----------------------------------------------------------------------------
94
95 wxLongLong wxGetLocalTimeMillis();
96
97 // ============================================================================
98 // implementation
99 // ============================================================================
100
101 // ----------------------------------------------------------------------------
102 // wxTimerBase
103 // ----------------------------------------------------------------------------
104
105 #if wxUSE_GUI
106
107 void wxTimerBase::Notify()
108 {
109 // the base class version generates an event if it has owner - which it
110 // should because otherwise nobody can process timer events
111 wxCHECK_RET( m_owner, _T("wxTimer::Notify() should be overridden.") );
112
113 wxTimerEvent event(m_idTimer, m_milli);
114 (void)m_owner->ProcessEvent(event);
115 }
116
117 #endif // wxUSE_GUI
118
119 // ----------------------------------------------------------------------------
120 // wxStopWatch
121 // ----------------------------------------------------------------------------
122
123 void wxStopWatch::Start(long t)
124 {
125 m_t0 = wxGetLocalTimeMillis() - t;
126 m_pause = 0;
127 }
128
129 long wxStopWatch::GetElapsedTime() const
130 {
131 return (wxGetLocalTimeMillis() - m_t0).GetLo();
132 }
133
134 long wxStopWatch::Time() const
135 {
136 return (m_pause ? m_pause : GetElapsedTime());
137 }
138
139 // ----------------------------------------------------------------------------
140 // old timer functions superceded by wxStopWatch
141 // ----------------------------------------------------------------------------
142
143 static wxLongLong wxStartTime = 0l;
144
145 // starts the global timer
146 void wxStartTimer()
147 {
148 wxStartTime = wxGetLocalTimeMillis();
149 }
150
151 // Returns elapsed time in milliseconds
152 long wxGetElapsedTime(bool resetTimer)
153 {
154 wxLongLong oldTime = wxStartTime;
155 wxLongLong newTime = wxGetLocalTimeMillis();
156
157 if ( resetTimer )
158 wxStartTime = newTime;
159
160 return (newTime - oldTime).GetLo();
161 }
162
163
164 // ----------------------------------------------------------------------------
165 // the functions to get the current time and timezone info
166 // ----------------------------------------------------------------------------
167
168 // Get local time as seconds since 00:00:00, Jan 1st 1970
169 long wxGetLocalTime()
170 {
171 struct tm tm;
172 time_t t0, t1;
173
174 // This cannot be made static because mktime can overwrite it.
175 //
176 memset(&tm, 0, sizeof(tm));
177 tm.tm_year = 70;
178 tm.tm_mon = 0;
179 tm.tm_mday = 5; // not Jan 1st 1970 due to mktime 'feature'
180 tm.tm_hour = 0;
181 tm.tm_min = 0;
182 tm.tm_sec = 0;
183 tm.tm_isdst = -1; // let mktime guess
184
185 // Note that mktime assumes that the struct tm contains local time.
186 //
187 t1 = time(&t1); // now
188 t0 = mktime(&tm); // origin
189
190 // Return the difference in seconds.
191 //
192 if (( t0 != (time_t)-1 ) && ( t1 != (time_t)-1 ))
193 return (long)difftime(t1, t0) + (60 * 60 * 24 * 4);
194
195 wxLogSysError(_("Failed to get the local system time"));
196 return -1;
197 }
198
199 // Get UTC time as seconds since 00:00:00, Jan 1st 1970
200 long wxGetUTCTime()
201 {
202 struct tm tm, *ptm;
203 time_t t0, t1;
204
205 // This cannot be made static because mktime can overwrite it
206 //
207 memset(&tm, 0, sizeof(tm));
208 tm.tm_year = 70;
209 tm.tm_mon = 0;
210 tm.tm_mday = 5; // not Jan 1st 1970 due to mktime 'feature'
211 tm.tm_hour = 0;
212 tm.tm_min = 0;
213 tm.tm_sec = 0;
214 tm.tm_isdst = -1; // let mktime guess
215
216 // Note that mktime assumes that the struct tm contains local time.
217 //
218 t1 = time(&t1); // now
219 t0 = mktime(&tm); // origin in localtime
220
221 if (( t0 != (time_t)-1 ) && ( t1 != (time_t)-1 ))
222 {
223 // To get t0 as GMT we convert to a struct tm with gmtime,
224 // and then back again.
225 //
226 ptm = gmtime(&t0);
227
228 if (ptm)
229 {
230 memcpy(&tm, ptm, sizeof(tm));
231 t0 = mktime(&tm);
232
233 if (t0 != (time_t)-1 )
234 return (long)difftime(t1, t0) + (60 * 60 * 24 * 4);
235 wxLogSysError(_("mktime() failed"));
236 }
237 else
238 {
239 wxLogSysError(_("gmtime() failed"));
240 }
241 }
242
243 wxLogError(_("Failed to get the UTC system time."));
244
245 return -1;
246 }
247
248
249 // Get local time as milliseconds since 00:00:00, Jan 1st 1970
250 wxLongLong wxGetLocalTimeMillis()
251 {
252 wxLongLong val = 1000l;
253
254 // If possible, use a functin which avoids conversions from
255 // broken-up time structures to milliseconds,
256
257 #if defined(HAVE_GETTIMEOFDAY)
258 struct timeval tp;
259 if ( wxGetTimeOfDay(&tp, (struct timezone *)NULL) != -1 )
260 {
261 val *= tp.tv_sec;
262 return (val + (tp.tv_usec / 1000));
263 }
264 else
265 {
266 wxLogError(_("wxGetTimeOfDay failed."));
267 return 0;
268 }
269 #elif defined(HAVE_FTIME)
270 struct timeb tp;
271
272 // ftime() is void and not int in some mingw32 headers, so don't
273 // test the return code (well, it shouldn't fail anyhow...)
274 (void)ftime(&tp);
275 val *= tp.time;
276 return (val + tp.millitm);
277 #else
278 // We use wxGetLocalTime() to get the seconds since
279 // 00:00:00 Jan 1st 1970 and then whatever is available
280 // to get millisecond resolution.
281 //
282 // NOTE that this might lead to a problem if the clocks
283 // use different sources, so this approach should be
284 // avoided where possible.
285
286 val *= wxGetLocalTime();
287
288 #if defined(__VISAGECPP__)
289 DATETIME dt;
290 ::DosGetDateTime(&dt);
291 val += (dt.hundredths*10);
292 #elif defined (__WIN32__)
293 #warning "Possible clock skew bug in wxStopWatch!"
294 SYSTEMTIME st;
295 ::GetLocalTime(&st);
296 val += st.wMilliseconds;
297 #else
298 #ifndef __VISUALC__
299 #warning "wxStopWatch will be up to second resolution!"
300 #endif
301 #endif
302
303 return val;
304
305 #endif
306 }