]> git.saurik.com Git - wxWidgets.git/blob - src/common/timercmn.cpp
add SQL_CATALOG and SQL_LOG filename variables
[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 // 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 #if defined(__WIN32__)
45 #include <windows.h>
46 #endif
47
48 #include <time.h>
49 #ifndef __WXMAC__
50 #include <sys/types.h> // for time_t
51 #endif
52
53 #if defined(HAVE_GETTIMEOFDAY)
54 #include <sys/time.h>
55 #include <unistd.h>
56 #elif defined(HAVE_FTIME)
57 #include <sys/timeb.h>
58 #endif
59
60 // ----------------------------------------------------------------------------
61 // wxWin macros
62 // ----------------------------------------------------------------------------
63
64 IMPLEMENT_DYNAMIC_CLASS(wxTimerEvent, wxEvent)
65
66 // ----------------------------------------------------------------------------
67 // macros
68 // ----------------------------------------------------------------------------
69
70 // on some really old systems gettimeofday() doesn't have the second argument,
71 // define wxGetTimeOfDay() to hide this difference
72 #ifdef HAVE_GETTIMEOFDAY
73 #ifdef WX_GETTIMEOFDAY_NO_TZ
74 struct timezone;
75 #define wxGetTimeOfDay(tv, tz) gettimeofday(tv)
76 #else
77 #define wxGetTimeOfDay(tv, tz) gettimeofday((tv), (tz))
78 #endif
79 #endif // HAVE_GETTIMEOFDAY
80
81 // ----------------------------------------------------------------------------
82 // prototypes
83 // ----------------------------------------------------------------------------
84
85 wxLongLong wxGetLocalTimeMillis();
86
87 // ============================================================================
88 // implementation
89 // ============================================================================
90
91 // ----------------------------------------------------------------------------
92 // wxTimerBase
93 // ----------------------------------------------------------------------------
94
95 void wxTimerBase::Notify()
96 {
97 // the base class version generates an event if it has owner - which it
98 // should because otherwise nobody can process timer events
99 wxCHECK_RET( m_owner, _T("wxTimer::Notify() should be overridden.") );
100
101 wxTimerEvent event(m_idTimer, m_milli);
102 (void)m_owner->ProcessEvent(event);
103 }
104
105 // ----------------------------------------------------------------------------
106 // wxStopWatch
107 // ----------------------------------------------------------------------------
108
109 void wxStopWatch::Start(long t)
110 {
111 m_t0 = wxGetLocalTimeMillis() - t;
112
113 m_pause = 0;
114 }
115
116 inline long wxStopWatch::GetElapsedTime() const
117 {
118 return (wxGetLocalTimeMillis() - m_t0).GetLo();
119 }
120
121 long wxStopWatch::Time() const
122 {
123 return (m_pause ? m_pause : GetElapsedTime());
124 }
125
126 // ----------------------------------------------------------------------------
127 // old timer functions superceded by wxStopWatch
128 // ----------------------------------------------------------------------------
129
130 static wxLongLong wxStartTime = 0l;
131
132 // starts the global timer
133 void wxStartTimer()
134 {
135 wxStartTime = wxGetLocalTimeMillis();
136 }
137
138 // Returns elapsed time in milliseconds
139 long wxGetElapsedTime(bool resetTimer)
140 {
141 wxLongLong oldTime = wxStartTime;
142 wxLongLong newTime = wxGetLocalTimeMillis();
143
144 if ( resetTimer )
145 wxStartTime = newTime;
146
147 return (newTime - oldTime).GetLo();
148 }
149
150
151 // ----------------------------------------------------------------------------
152 // the functions to get the current time and timezone info
153 // ----------------------------------------------------------------------------
154
155 // Get local time as seconds since 00:00:00, Jan 1st 1970
156 long wxGetLocalTime()
157 {
158 struct tm tm;
159 time_t t0, t1;
160
161 // This cannot be made static because mktime can overwrite it.
162 //
163 memset(&tm, 0, sizeof(tm));
164 tm.tm_year = 70;
165 tm.tm_mon = 0;
166 tm.tm_mday = 5; // not Jan 1st 1970 due to mktime 'feature'
167 tm.tm_hour = 0;
168 tm.tm_min = 0;
169 tm.tm_sec = 0;
170 tm.tm_isdst = -1; // let mktime guess
171
172 // Note that mktime assumes that the struct tm contains local time.
173 //
174 t1 = time(&t1); // now
175 t0 = mktime(&tm); // origin
176
177 // Return the difference in seconds.
178 //
179 if (( t0 != (time_t)-1 ) && ( t1 != (time_t)-1 ))
180 return (long)difftime(t1, t0) + (60 * 60 * 24 * 4);
181
182 wxLogSysError(_("Failed to get the local system time"));
183 return -1;
184 }
185
186 // Get UTC time as seconds since 00:00:00, Jan 1st 1970
187 long wxGetUTCTime()
188 {
189 struct tm tm, *ptm;
190 time_t t0, t1;
191
192 // This cannot be made static because mktime can overwrite it
193 //
194 memset(&tm, 0, sizeof(tm));
195 tm.tm_year = 70;
196 tm.tm_mon = 0;
197 tm.tm_mday = 5; // not Jan 1st 1970 due to mktime 'feature'
198 tm.tm_hour = 0;
199 tm.tm_min = 0;
200 tm.tm_sec = 0;
201 tm.tm_isdst = -1; // let mktime guess
202
203 // Note that mktime assumes that the struct tm contains local time.
204 //
205 t1 = time(&t1); // now
206 t0 = mktime(&tm); // origin in localtime
207
208 if (( t0 != (time_t)-1 ) && ( t1 != (time_t)-1 ))
209 {
210 // To get t0 as GMT we convert to a struct tm with gmtime,
211 // and then back again.
212 //
213 ptm = gmtime(&t0);
214
215 if (ptm)
216 {
217 memcpy(&tm, ptm, sizeof(tm));
218 t0 = mktime(&tm);
219
220 if (t0 != (time_t)-1 )
221 return (long)difftime(t1, t0) + (60 * 60 * 24 * 4);
222 wxLogSysError(_("Failed 2nd mktime"));
223 }
224 wxLogSysError(_("Failed gmtime"));
225 }
226 wxLogSysError(_("Failed to get the UTC system time"));
227 return -1;
228 }
229
230
231 // Get local time as milliseconds since 00:00:00, Jan 1st 1970
232 wxLongLong wxGetLocalTimeMillis()
233 {
234 // We use wxGetLocalTime() to get the seconds since
235 // 00:00:00 Jan 1st 1970 and then whatever is available
236 // to get millisecond resolution.
237 //
238 wxLongLong val = 1000l;
239 val *= wxGetLocalTime();
240
241 // If we got here, do not fail even if we can't get
242 // millisecond resolution.
243 //
244 #if defined(__WIN32__)
245 SYSTEMTIME st;
246 ::GetLocalTime(&st);
247 return (val + st.wMilliseconds);
248 #elif defined(HAVE_GETTIMEOFDAY)
249 struct timeval tp;
250 if ( wxGetTimeOfDay(&tp, (struct timezone *)NULL) != -1 )
251 {
252 return (val + (tp.tv_usec / 1000));
253 }
254 #elif defined(HAVE_FTIME)
255 struct timeb tp;
256 if ( ftime(&tp) == 0 )
257 {
258 return (val + tp.millitm);
259 }
260 #else
261 #ifndef __BORLANDC__
262 #warning "wxStopWatch will be up to second resolution!"
263 #endif
264 #endif
265
266 return val;
267 }