]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/common/time.cpp | |
3 | // Purpose: Implementation of time-related functions. | |
4 | // Author: Vadim Zeitlin | |
5 | // Created: 2011-11-26 | |
6 | // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org> | |
7 | // Licence: wxWindows licence | |
8 | /////////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // ============================================================================ | |
11 | // declarations | |
12 | // ============================================================================ | |
13 | ||
14 | // ---------------------------------------------------------------------------- | |
15 | // headers | |
16 | // ---------------------------------------------------------------------------- | |
17 | ||
18 | // for compilers that support precompilation, includes "wx.h". | |
19 | #include "wx/wxprec.h" | |
20 | ||
21 | #ifdef __BORLANDC__ | |
22 | #pragma hdrstop | |
23 | #endif | |
24 | ||
25 | #include "wx/time.h" | |
26 | ||
27 | #ifndef WX_PRECOMP | |
28 | #ifdef __WINDOWS__ | |
29 | #include "wx/msw/wrapwin.h" | |
30 | #endif | |
31 | #include "wx/intl.h" | |
32 | #include "wx/log.h" | |
33 | #endif | |
34 | ||
35 | #ifndef WX_GMTOFF_IN_TM | |
36 | // Define it for some systems which don't (always) use configure but are | |
37 | // known to have tm_gmtoff field. | |
38 | #if defined(__DARWIN__) | |
39 | #define WX_GMTOFF_IN_TM | |
40 | #endif | |
41 | #endif | |
42 | ||
43 | #if defined(__VISAGECPP__) && !defined(HAVE_FTIME) | |
44 | #define HAVE_FTIME | |
45 | # if __IBMCPP__ >= 400 | |
46 | # define ftime(x) _ftime(x) | |
47 | # endif | |
48 | #endif | |
49 | ||
50 | #ifndef __WXWINCE__ | |
51 | #include <time.h> | |
52 | #else | |
53 | #include "wx/msw/private.h" | |
54 | #include "wx/msw/wince/time.h" | |
55 | #endif | |
56 | ||
57 | ||
58 | #if !defined(__WXMAC__) && !defined(__WXWINCE__) | |
59 | #include <sys/types.h> // for time_t | |
60 | #endif | |
61 | ||
62 | #if defined(HAVE_GETTIMEOFDAY) | |
63 | #include <sys/time.h> | |
64 | #include <unistd.h> | |
65 | #elif defined(HAVE_FTIME) | |
66 | #include <sys/timeb.h> | |
67 | #endif | |
68 | ||
69 | #if defined(__DJGPP__) || defined(__WINE__) | |
70 | #include <sys/timeb.h> | |
71 | #include <values.h> | |
72 | #endif | |
73 | ||
74 | namespace | |
75 | { | |
76 | ||
77 | const int MILLISECONDS_PER_SECOND = 1000; | |
78 | const int MICROSECONDS_PER_MILLISECOND = 1000; | |
79 | const int MICROSECONDS_PER_SECOND = 1000*1000; | |
80 | ||
81 | } // anonymous namespace | |
82 | ||
83 | // ============================================================================ | |
84 | // implementation | |
85 | // ============================================================================ | |
86 | ||
87 | // NB: VC8 safe time functions could/should be used for wxMSW as well probably | |
88 | #if defined(__WXWINCE__) && defined(__VISUALC8__) | |
89 | ||
90 | struct tm *wxLocaltime_r(const time_t *t, struct tm* tm) | |
91 | { | |
92 | __time64_t t64 = *t; | |
93 | return _localtime64_s(tm, &t64) == 0 ? tm : NULL; | |
94 | } | |
95 | ||
96 | struct tm *wxGmtime_r(const time_t* t, struct tm* tm) | |
97 | { | |
98 | __time64_t t64 = *t; | |
99 | return _gmtime64_s(tm, &t64) == 0 ? tm : NULL; | |
100 | } | |
101 | ||
102 | #else // !wxWinCE with VC8 | |
103 | ||
104 | #if (!defined(HAVE_LOCALTIME_R) || !defined(HAVE_GMTIME_R)) && wxUSE_THREADS && !defined(__WINDOWS__) | |
105 | static wxMutex timeLock; | |
106 | #endif | |
107 | ||
108 | #ifndef HAVE_LOCALTIME_R | |
109 | struct tm *wxLocaltime_r(const time_t* ticks, struct tm* temp) | |
110 | { | |
111 | #if wxUSE_THREADS && !defined(__WINDOWS__) | |
112 | // No need to waste time with a mutex on windows since it's using | |
113 | // thread local storage for localtime anyway. | |
114 | wxMutexLocker locker(timeLock); | |
115 | #endif | |
116 | ||
117 | // Borland CRT crashes when passed 0 ticks for some reason, see SF bug 1704438 | |
118 | #ifdef __BORLANDC__ | |
119 | if ( !*ticks ) | |
120 | return NULL; | |
121 | #endif | |
122 | ||
123 | const tm * const t = localtime(ticks); | |
124 | if ( !t ) | |
125 | return NULL; | |
126 | ||
127 | memcpy(temp, t, sizeof(struct tm)); | |
128 | return temp; | |
129 | } | |
130 | #endif // !HAVE_LOCALTIME_R | |
131 | ||
132 | #ifndef HAVE_GMTIME_R | |
133 | struct tm *wxGmtime_r(const time_t* ticks, struct tm* temp) | |
134 | { | |
135 | #if wxUSE_THREADS && !defined(__WINDOWS__) | |
136 | // No need to waste time with a mutex on windows since it's | |
137 | // using thread local storage for gmtime anyway. | |
138 | wxMutexLocker locker(timeLock); | |
139 | #endif | |
140 | ||
141 | #ifdef __BORLANDC__ | |
142 | if ( !*ticks ) | |
143 | return NULL; | |
144 | #endif | |
145 | ||
146 | const tm * const t = gmtime(ticks); | |
147 | if ( !t ) | |
148 | return NULL; | |
149 | ||
150 | memcpy(temp, gmtime(ticks), sizeof(struct tm)); | |
151 | return temp; | |
152 | } | |
153 | #endif // !HAVE_GMTIME_R | |
154 | ||
155 | #endif // wxWinCE with VC8/other platforms | |
156 | ||
157 | // returns the time zone in the C sense, i.e. the difference UTC - local | |
158 | // (in seconds) | |
159 | int wxGetTimeZone() | |
160 | { | |
161 | #ifdef WX_GMTOFF_IN_TM | |
162 | // set to true when the timezone is set | |
163 | static bool s_timezoneSet = false; | |
164 | static long gmtoffset = LONG_MAX; // invalid timezone | |
165 | ||
166 | // ensure that the timezone variable is set by calling wxLocaltime_r | |
167 | if ( !s_timezoneSet ) | |
168 | { | |
169 | // just call wxLocaltime_r() instead of figuring out whether this | |
170 | // system supports tzset(), _tzset() or something else | |
171 | time_t t = time(NULL); | |
172 | struct tm tm; | |
173 | ||
174 | wxLocaltime_r(&t, &tm); | |
175 | s_timezoneSet = true; | |
176 | ||
177 | // note that GMT offset is the opposite of time zone and so to return | |
178 | // consistent results in both WX_GMTOFF_IN_TM and !WX_GMTOFF_IN_TM | |
179 | // cases we have to negate it | |
180 | gmtoffset = -tm.tm_gmtoff; | |
181 | ||
182 | // this function is supposed to return the same value whether DST is | |
183 | // enabled or not, so we need to use an additional offset if DST is on | |
184 | // as tm_gmtoff already does include it | |
185 | if ( tm.tm_isdst ) | |
186 | gmtoffset += 3600; | |
187 | } | |
188 | return (int)gmtoffset; | |
189 | #elif defined(__DJGPP__) || defined(__WINE__) | |
190 | struct timeb tb; | |
191 | ftime(&tb); | |
192 | return tb.timezone*60; | |
193 | #elif defined(__VISUALC__) | |
194 | // We must initialize the time zone information before using it (this will | |
195 | // be done only once internally). | |
196 | _tzset(); | |
197 | ||
198 | // Starting with VC++ 8 timezone variable is deprecated and is not even | |
199 | // available in some standard library version so use the new function for | |
200 | // accessing it instead. | |
201 | #if wxCHECK_VISUALC_VERSION(8) | |
202 | long t; | |
203 | _get_timezone(&t); | |
204 | return t; | |
205 | #else // VC++ < 8 | |
206 | return timezone; | |
207 | #endif | |
208 | #else // Use some kind of time zone variable. | |
209 | // In any case we must initialize the time zone before using it. | |
210 | tzset(); | |
211 | ||
212 | #if defined(WX_TIMEZONE) // If WX_TIMEZONE was defined by configure, use it. | |
213 | return WX_TIMEZONE; | |
214 | #elif defined(__BORLANDC__) || defined(__MINGW32__) || defined(__VISAGECPP__) | |
215 | return _timezone; | |
216 | #else // unknown platform -- assume it has timezone | |
217 | return timezone; | |
218 | #endif // different time zone variables | |
219 | #endif // different ways to determine time zone | |
220 | } | |
221 | ||
222 | // Get local time as seconds since 00:00:00, Jan 1st 1970 | |
223 | long wxGetLocalTime() | |
224 | { | |
225 | struct tm tm; | |
226 | time_t t0, t1; | |
227 | ||
228 | // This cannot be made static because mktime can overwrite it. | |
229 | // | |
230 | memset(&tm, 0, sizeof(tm)); | |
231 | tm.tm_year = 70; | |
232 | tm.tm_mon = 0; | |
233 | tm.tm_mday = 5; // not Jan 1st 1970 due to mktime 'feature' | |
234 | tm.tm_hour = 0; | |
235 | tm.tm_min = 0; | |
236 | tm.tm_sec = 0; | |
237 | tm.tm_isdst = -1; // let mktime guess | |
238 | ||
239 | // Note that mktime assumes that the struct tm contains local time. | |
240 | // | |
241 | t1 = time(&t1); // now | |
242 | t0 = mktime(&tm); // origin | |
243 | ||
244 | // Return the difference in seconds. | |
245 | // | |
246 | if (( t0 != (time_t)-1 ) && ( t1 != (time_t)-1 )) | |
247 | return (long)difftime(t1, t0) + (60 * 60 * 24 * 4); | |
248 | ||
249 | wxLogSysError(_("Failed to get the local system time")); | |
250 | return -1; | |
251 | } | |
252 | ||
253 | // Get UTC time as seconds since 00:00:00, Jan 1st 1970 | |
254 | long wxGetUTCTime() | |
255 | { | |
256 | return (long)time(NULL); | |
257 | } | |
258 | ||
259 | #if wxUSE_LONGLONG | |
260 | ||
261 | wxLongLong wxGetUTCTimeUSec() | |
262 | { | |
263 | #if defined(__WINDOWS__) | |
264 | FILETIME ft; | |
265 | ::GetSystemTimeAsFileTime(&ft); | |
266 | ||
267 | // FILETIME is in 100ns or 0.1us since 1601-01-01, transform to us since | |
268 | // 1970-01-01. | |
269 | wxLongLong t(ft.dwHighDateTime, ft.dwLowDateTime); | |
270 | t /= 10; | |
271 | t -= wxLL(11644473600000000); // Unix - Windows epochs difference in us. | |
272 | return t; | |
273 | #else // non-MSW | |
274 | ||
275 | #ifdef HAVE_GETTIMEOFDAY | |
276 | timeval tv; | |
277 | if ( wxGetTimeOfDay(&tv) != -1 ) | |
278 | { | |
279 | wxLongLong val(tv.tv_sec); | |
280 | val *= MICROSECONDS_PER_SECOND; | |
281 | val += tv.tv_usec; | |
282 | return val; | |
283 | } | |
284 | #endif // HAVE_GETTIMEOFDAY | |
285 | ||
286 | // Fall back to lesser precision function. | |
287 | return wxGetUTCTimeMillis()*MICROSECONDS_PER_MILLISECOND; | |
288 | #endif // MSW/!MSW | |
289 | } | |
290 | ||
291 | // Get local time as milliseconds since 00:00:00, Jan 1st 1970 | |
292 | wxLongLong wxGetUTCTimeMillis() | |
293 | { | |
294 | // If possible, use a function which avoids conversions from | |
295 | // broken-up time structures to milliseconds | |
296 | #if defined(__WINDOWS__) | |
297 | FILETIME ft; | |
298 | ::GetSystemTimeAsFileTime(&ft); | |
299 | ||
300 | // FILETIME is expressed in 100ns (or 0.1us) units since 1601-01-01, | |
301 | // transform them to ms since 1970-01-01. | |
302 | wxLongLong t(ft.dwHighDateTime, ft.dwLowDateTime); | |
303 | t /= 10000; | |
304 | t -= wxLL(11644473600000); // Unix - Windows epochs difference in ms. | |
305 | return t; | |
306 | #else // !__WINDOWS__ | |
307 | wxLongLong val = MILLISECONDS_PER_SECOND; | |
308 | ||
309 | #if defined(HAVE_GETTIMEOFDAY) | |
310 | struct timeval tp; | |
311 | if ( wxGetTimeOfDay(&tp) != -1 ) | |
312 | { | |
313 | val *= tp.tv_sec; | |
314 | return (val + (tp.tv_usec / MICROSECONDS_PER_MILLISECOND)); | |
315 | } | |
316 | else | |
317 | { | |
318 | wxLogError(_("wxGetTimeOfDay failed.")); | |
319 | return 0; | |
320 | } | |
321 | #elif defined(HAVE_FTIME) | |
322 | struct timeb tp; | |
323 | ||
324 | // ftime() is void and not int in some mingw32 headers, so don't | |
325 | // test the return code (well, it shouldn't fail anyhow...) | |
326 | (void)::ftime(&tp); | |
327 | val *= tp.time; | |
328 | return (val + tp.millitm); | |
329 | #else // no gettimeofday() nor ftime() | |
330 | // If your platform/compiler does not support ms resolution please | |
331 | // do NOT just shut off these warnings, drop me a line instead at | |
332 | // <guille@iies.es> | |
333 | ||
334 | #if defined(__VISUALC__) || defined (__WATCOMC__) | |
335 | #pragma message("wxStopWatch will be up to second resolution!") | |
336 | #elif defined(__BORLANDC__) | |
337 | #pragma message "wxStopWatch will be up to second resolution!" | |
338 | #else | |
339 | #warning "wxStopWatch will be up to second resolution!" | |
340 | #endif // compiler | |
341 | ||
342 | val *= wxGetUTCTime(); | |
343 | return val; | |
344 | #endif // time functions | |
345 | ||
346 | #endif // __WINDOWS__/!__WINDOWS__ | |
347 | } | |
348 | ||
349 | wxLongLong wxGetLocalTimeMillis() | |
350 | { | |
351 | return wxGetUTCTimeMillis() - wxGetTimeZone()*MILLISECONDS_PER_SECOND; | |
352 | } | |
353 | ||
354 | #else // !wxUSE_LONGLONG | |
355 | ||
356 | double wxGetLocalTimeMillis(void) | |
357 | { | |
358 | return (double(clock()) / double(CLOCKS_PER_SEC)) * 1000.0; | |
359 | } | |
360 | ||
361 | #endif // wxUSE_LONGLONG/!wxUSE_LONGLONG |