]>
Commit | Line | Data |
---|---|---|
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> |
c801d85f KB |
14 | // Licence: wxWindows license |
15 | ///////////////////////////////////////////////////////////////////////////// | |
16 | ||
0470b1e6 VZ |
17 | // ============================================================================ |
18 | // declarations | |
19 | // ============================================================================ | |
20 | ||
21 | // ---------------------------------------------------------------------------- | |
22 | // headers | |
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 | |
b8f04990 GRG |
44 | #if defined(__WIN32__) |
45 | #include <windows.h> | |
0470b1e6 VZ |
46 | #endif |
47 | ||
b8f04990 GRG |
48 | #include <time.h> |
49 | #ifndef __WXMAC__ | |
50 | #include <sys/types.h> // for time_t | |
07cf98cb VZ |
51 | #endif |
52 | ||
0470b1e6 VZ |
53 | #if defined(HAVE_GETTIMEOFDAY) |
54 | #include <sys/time.h> | |
55 | #include <unistd.h> | |
0470b1e6 VZ |
56 | #elif defined(HAVE_FTIME) |
57 | #include <sys/timeb.h> | |
c801d85f KB |
58 | #endif |
59 | ||
0470b1e6 VZ |
60 | // ---------------------------------------------------------------------------- |
61 | // macros | |
62 | // ---------------------------------------------------------------------------- | |
ce3ed50d | 63 | |
0470b1e6 VZ |
64 | // on some really old systems gettimeofday() doesn't have the second argument, |
65 | // define wxGetTimeOfDay() to hide this difference | |
66 | #ifdef HAVE_GETTIMEOFDAY | |
67 | #ifdef WX_GETTIMEOFDAY_NO_TZ | |
68 | struct timezone; | |
69 | #define wxGetTimeOfDay(tv, tz) gettimeofday(tv) | |
70 | #else | |
71 | #define wxGetTimeOfDay(tv, tz) gettimeofday((tv), (tz)) | |
72 | #endif | |
73 | #endif // HAVE_GETTIMEOFDAY | |
c801d85f | 74 | |
0470b1e6 VZ |
75 | // ============================================================================ |
76 | // implementation | |
77 | // ============================================================================ | |
c801d85f | 78 | |
b8f04990 GRG |
79 | wxLongLong wxGetLocalTimeMillis(); |
80 | ||
0470b1e6 VZ |
81 | // ---------------------------------------------------------------------------- |
82 | // wxStopWatch | |
83 | // ---------------------------------------------------------------------------- | |
c801d85f | 84 | |
0470b1e6 VZ |
85 | void wxStopWatch::Start(long t) |
86 | { | |
b8f04990 | 87 | m_t0 = wxGetLocalTimeMillis() - t; |
c801d85f | 88 | |
0470b1e6 VZ |
89 | m_pause = 0; |
90 | } | |
91 | ||
92 | long wxStopWatch::Time() const | |
93 | { | |
b8f04990 GRG |
94 | return (m_pause ? m_pause : GetElapsedTime()); |
95 | } | |
96 | ||
97 | long wxStopWatch::GetElapsedTime() const | |
98 | { | |
99 | return (wxGetLocalTimeMillis() - m_t0).GetLo(); | |
0470b1e6 VZ |
100 | } |
101 | ||
102 | // ---------------------------------------------------------------------------- | |
103 | // old timer functions superceded by wxStopWatch | |
104 | // ---------------------------------------------------------------------------- | |
c801d85f | 105 | |
cd0b1709 | 106 | static wxLongLong wxStartTime = 0l; |
c801d85f | 107 | |
0470b1e6 VZ |
108 | // starts the global timer |
109 | void wxStartTimer() | |
c801d85f | 110 | { |
b8f04990 | 111 | wxStartTime = wxGetLocalTimeMillis(); |
c801d85f KB |
112 | } |
113 | ||
114 | // Returns elapsed time in milliseconds | |
115 | long wxGetElapsedTime(bool resetTimer) | |
f0599ea9 | 116 | { |
b8f04990 GRG |
117 | wxLongLong oldTime = wxStartTime; |
118 | wxLongLong newTime = wxGetLocalTimeMillis(); | |
f0599ea9 | 119 | |
0470b1e6 VZ |
120 | if ( resetTimer ) |
121 | wxStartTime = newTime; | |
122 | ||
b8f04990 | 123 | return (newTime - oldTime).GetLo(); |
f0599ea9 SB |
124 | } |
125 | ||
126 | ||
0470b1e6 VZ |
127 | // ---------------------------------------------------------------------------- |
128 | // the functions to get the current time and timezone info | |
129 | // ---------------------------------------------------------------------------- | |
130 | ||
b8f04990 GRG |
131 | // Get local time as seconds since 00:00:00, Jan 1st 1970 |
132 | long wxGetLocalTime() | |
c801d85f | 133 | { |
b8f04990 GRG |
134 | struct tm tm; |
135 | time_t t0, t1; | |
136 | ||
137 | // This cannot be made static because mktime can overwrite it. | |
138 | // | |
139 | memset(&tm, 0, sizeof(tm)); | |
140 | tm.tm_year = 70; | |
141 | tm.tm_mon = 0; | |
142 | tm.tm_mday = 5; // not Jan 1st 1970 due to mktime 'feature' | |
143 | tm.tm_hour = 0; | |
144 | tm.tm_min = 0; | |
145 | tm.tm_sec = 0; | |
146 | tm.tm_isdst = -1; // let mktime guess | |
147 | ||
148 | // Note that mktime assumes that the struct tm contains local time. | |
149 | // | |
150 | t1 = time(&t1); // now | |
151 | t0 = mktime(&tm); // origin | |
152 | ||
153 | // Return the difference in seconds. | |
154 | // | |
155 | if (( t0 != (time_t)-1 ) && ( t1 != (time_t)-1 )) | |
156 | return (long)difftime(t1, t0) + (60 * 60 * 24 * 4); | |
157 | ||
158 | wxLogSysError(_("Failed to get the local system time")); | |
159 | return -1; | |
160 | } | |
b76b015e | 161 | |
b8f04990 GRG |
162 | // Get UTC time as seconds since 00:00:00, Jan 1st 1970 |
163 | long wxGetUTCTime() | |
164 | { | |
165 | struct tm tm, *ptm; | |
166 | time_t t0, t1; | |
167 | ||
168 | // This cannot be made static because mktime can overwrite it | |
169 | // | |
170 | memset(&tm, 0, sizeof(tm)); | |
171 | tm.tm_year = 70; | |
172 | tm.tm_mon = 0; | |
173 | tm.tm_mday = 5; // not Jan 1st 1970 due to mktime 'feature' | |
174 | tm.tm_hour = 0; | |
175 | tm.tm_min = 0; | |
176 | tm.tm_sec = 0; | |
177 | tm.tm_isdst = -1; // let mktime guess | |
178 | ||
179 | // Note that mktime assumes that the struct tm contains local time. | |
180 | // | |
181 | t1 = time(&t1); // now | |
182 | t0 = mktime(&tm); // origin in localtime | |
183 | ||
184 | if (( t0 != (time_t)-1 ) && ( t1 != (time_t)-1 )) | |
0470b1e6 | 185 | { |
b8f04990 GRG |
186 | // To get t0 as GMT we convert to a struct tm with gmtime, |
187 | // and then back again. | |
188 | // | |
189 | ptm = gmtime(&t0); | |
0470b1e6 | 190 | |
b8f04990 | 191 | if (ptm) |
0470b1e6 | 192 | { |
b8f04990 GRG |
193 | memcpy(&tm, ptm, sizeof(tm)); |
194 | t0 = mktime(&tm); | |
195 | ||
196 | if (t0 != (time_t)-1 ) | |
197 | return (long)difftime(t1, t0) + (60 * 60 * 24 * 4); | |
198 | wxLogSysError(_("Failed 2nd mktime")); | |
0470b1e6 | 199 | } |
b8f04990 | 200 | wxLogSysError(_("Failed gmtime")); |
0470b1e6 | 201 | } |
b8f04990 GRG |
202 | wxLogSysError(_("Failed to get the UTC system time")); |
203 | return -1; | |
204 | } | |
205 | ||
206 | ||
207 | // Get local time as milliseconds since 00:00:00, Jan 1st 1970 | |
208 | wxLongLong wxGetLocalTimeMillis() | |
209 | { | |
210 | // We use wxGetLocalTime() to get the seconds since | |
211 | // 00:00:00 Jan 1st 1970 and then whatever is available | |
212 | // to get millisecond resolution. | |
213 | // | |
cd0b1709 VZ |
214 | wxLongLong val = 1000l; |
215 | val *= wxGetLocalTime(); | |
b8f04990 GRG |
216 | |
217 | // If we got here, do not fail even if we can't get | |
218 | // millisecond resolution. | |
219 | // | |
220 | #if defined(__WIN32__) | |
221 | SYSTEMTIME st; | |
222 | ::GetLocalTime(&st); | |
223 | return (val + st.wMilliseconds); | |
0470b1e6 VZ |
224 | #elif defined(HAVE_GETTIMEOFDAY) |
225 | struct timeval tp; | |
226 | if ( wxGetTimeOfDay(&tp, (struct timezone *)NULL) != -1 ) | |
227 | { | |
b8f04990 | 228 | return (val + (tp.tv_usec / 1000)); |
0470b1e6 VZ |
229 | } |
230 | #elif defined(HAVE_FTIME) | |
231 | struct timeb tp; | |
232 | if ( ftime(&tp) == 0 ) | |
233 | { | |
b8f04990 | 234 | return (val + tp.millitm); |
0470b1e6 | 235 | } |
c801d85f | 236 | #endif |
c801d85f | 237 | |
b8f04990 | 238 | return val; |
c801d85f | 239 | } |