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