1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/time.cpp
3 // Purpose: Implementation of time-related functions.
4 // Author: Vadim Zeitlin
6 // RCS-ID: $Id: wxhead.cpp,v 1.11 2010-04-22 12:44:51 zeitlin Exp $
7 // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
30 #include "wx/msw/wrapwin.h"
36 #ifndef WX_GMTOFF_IN_TM
37 // Define it for some systems which don't (always) use configure but are
38 // known to have tm_gmtoff field.
39 #if defined(__WXPALMOS__) || defined(__DARWIN__)
40 #define WX_GMTOFF_IN_TM
44 #if defined(__VISAGECPP__) && !defined(HAVE_FTIME)
46 # if __IBMCPP__ >= 400
47 # define ftime(x) _ftime(x)
51 #if defined(__MWERKS__) && defined(__WXMSW__)
53 # undef HAVE_GETTIMEOFDAY
60 #include "wx/msw/private.h"
61 #include "wx/msw/wince/time.h"
63 #endif // __WXPALMOS5__
66 #if !defined(__WXMAC__) && !defined(__WXWINCE__)
67 #include <sys/types.h> // for time_t
70 #if defined(HAVE_GETTIMEOFDAY)
73 #elif defined(HAVE_FTIME)
74 #include <sys/timeb.h>
80 #include <SystemMgr.h>
83 #if defined(__MWERKS__) && wxUSE_UNICODE
87 #if defined(__DJGPP__) || defined(__WINE__)
88 #include <sys/timeb.h>
95 const int MILLISECONDS_PER_SECOND
= 1000;
96 const int MICROSECONDS_PER_MILLISECOND
= 1000;
97 const int MICROSECONDS_PER_SECOND
= 1000*1000;
99 } // anonymous namespace
101 // ============================================================================
103 // ============================================================================
105 // NB: VC8 safe time functions could/should be used for wxMSW as well probably
106 #if defined(__WXWINCE__) && defined(__VISUALC8__)
108 struct tm
*wxLocaltime_r(const time_t *t
, struct tm
* tm
)
111 return _localtime64_s(tm
, &t64
) == 0 ? tm
: NULL
;
114 struct tm
*wxGmtime_r(const time_t* t
, struct tm
* tm
)
117 return _gmtime64_s(tm
, &t64
) == 0 ? tm
: NULL
;
120 #else // !wxWinCE with VC8
122 #if (!defined(HAVE_LOCALTIME_R) || !defined(HAVE_GMTIME_R)) && wxUSE_THREADS && !defined(__WINDOWS__)
123 static wxMutex timeLock
;
126 #ifndef HAVE_LOCALTIME_R
127 struct tm
*wxLocaltime_r(const time_t* ticks
, struct tm
* temp
)
129 #if wxUSE_THREADS && !defined(__WINDOWS__)
130 // No need to waste time with a mutex on windows since it's using
131 // thread local storage for localtime anyway.
132 wxMutexLocker
locker(timeLock
);
135 // Borland CRT crashes when passed 0 ticks for some reason, see SF bug 1704438
141 const tm
* const t
= localtime(ticks
);
145 memcpy(temp
, t
, sizeof(struct tm
));
148 #endif // !HAVE_LOCALTIME_R
150 #ifndef HAVE_GMTIME_R
151 struct tm
*wxGmtime_r(const time_t* ticks
, struct tm
* temp
)
153 #if wxUSE_THREADS && !defined(__WINDOWS__)
154 // No need to waste time with a mutex on windows since it's
155 // using thread local storage for gmtime anyway.
156 wxMutexLocker
locker(timeLock
);
164 const tm
* const t
= gmtime(ticks
);
168 memcpy(temp
, gmtime(ticks
), sizeof(struct tm
));
171 #endif // !HAVE_GMTIME_R
173 #endif // wxWinCE with VC8/other platforms
175 // returns the time zone in the C sense, i.e. the difference UTC - local
179 #ifdef WX_GMTOFF_IN_TM
180 // set to true when the timezone is set
181 static bool s_timezoneSet
= false;
182 static long gmtoffset
= LONG_MAX
; // invalid timezone
184 // ensure that the timezone variable is set by calling wxLocaltime_r
185 if ( !s_timezoneSet
)
187 // just call wxLocaltime_r() instead of figuring out whether this
188 // system supports tzset(), _tzset() or something else
189 time_t t
= time(NULL
);
192 wxLocaltime_r(&t
, &tm
);
193 s_timezoneSet
= true;
195 // note that GMT offset is the opposite of time zone and so to return
196 // consistent results in both WX_GMTOFF_IN_TM and !WX_GMTOFF_IN_TM
197 // cases we have to negate it
198 gmtoffset
= -tm
.tm_gmtoff
;
200 // this function is supposed to return the same value whether DST is
201 // enabled or not, so we need to use an additional offset if DST is on
202 // as tm_gmtoff already does include it
206 return (int)gmtoffset
;
207 #elif defined(__DJGPP__) || defined(__WINE__)
210 return tb
.timezone
*60;
211 #elif defined(__VISUALC__)
212 // We must initialize the time zone information before using it (this will
213 // be done only once internally).
216 // Starting with VC++ 8 timezone variable is deprecated and is not even
217 // available in some standard library version so use the new function for
218 // accessing it instead.
219 #if wxCHECK_VISUALC_VERSION(8)
226 #elif defined(WX_TIMEZONE) // If WX_TIMEZONE was defined by configure, use it.
228 #elif defined(__BORLANDC__) || defined(__MINGW32__) || defined(__VISAGECPP__)
230 #elif defined(__MWERKS__)
232 #else // unknown platform -- assume it has timezone
234 #endif // WX_GMTOFF_IN_TM/!WX_GMTOFF_IN_TM
237 // Get local time as seconds since 00:00:00, Jan 1st 1970
238 long wxGetLocalTime()
243 // This cannot be made static because mktime can overwrite it.
245 memset(&tm
, 0, sizeof(tm
));
248 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
252 tm
.tm_isdst
= -1; // let mktime guess
254 // Note that mktime assumes that the struct tm contains local time.
256 t1
= time(&t1
); // now
257 t0
= mktime(&tm
); // origin
259 // Return the difference in seconds.
261 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
262 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
264 wxLogSysError(_("Failed to get the local system time"));
268 // Get UTC time as seconds since 00:00:00, Jan 1st 1970
271 return (long)time(NULL
);
276 wxLongLong
wxGetUTCTimeUSec()
278 #if defined(__WXMSW__)
280 ::GetSystemTimeAsFileTime(&ft
);
282 // FILETIME is in 100ns or 0.1us since 1601-01-01, transform to us since
284 wxLongLong
t(ft
.dwHighDateTime
, ft
.dwLowDateTime
);
286 t
-= wxLL(11644473600000000); // Unix - Windows epochs difference in us.
290 #ifdef HAVE_GETTIMEOFDAY
292 if ( wxGetTimeOfDay(&tv
) != -1 )
294 wxLongLong
val(tv
.tv_sec
);
295 val
*= MICROSECONDS_PER_SECOND
;
299 #endif // HAVE_GETTIMEOFDAY
301 // Fall back to lesser precision function.
302 return wxGetUTCTimeMillis()*MICROSECONDS_PER_MILLISECOND
;
306 // Get local time as milliseconds since 00:00:00, Jan 1st 1970
307 wxLongLong
wxGetUTCTimeMillis()
309 wxLongLong val
= MILLISECONDS_PER_SECOND
;
311 // If possible, use a function which avoids conversions from
312 // broken-up time structures to milliseconds
313 #if defined(__WXPALMOS__)
322 uint32_t now
= TimGetSeconds();
323 uint32_t then
= TimDateTimeToSeconds (&thenst
);
324 return SysTimeToMilliSecs(SysTimeInSecs(now
- then
));
325 #elif defined(__WXMSW__)
327 ::GetSystemTimeAsFileTime(&ft
);
329 // FILETIME is expressed in 100ns (or 0.1us) units since 1601-01-01,
330 // transform them to ms since 1970-01-01.
331 wxLongLong
t(ft
.dwHighDateTime
, ft
.dwLowDateTime
);
333 t
-= wxLL(11644473600000); // Unix - Windows epochs difference in ms.
335 #elif defined(HAVE_GETTIMEOFDAY)
337 if ( wxGetTimeOfDay(&tp
) != -1 )
340 return (val
+ (tp
.tv_usec
/ MICROSECONDS_PER_MILLISECOND
));
344 wxLogError(_("wxGetTimeOfDay failed."));
347 #elif defined(HAVE_FTIME)
350 // ftime() is void and not int in some mingw32 headers, so don't
351 // test the return code (well, it shouldn't fail anyhow...)
354 return (val
+ tp
.millitm
);
355 #else // no gettimeofday() nor ftime()
356 // If your platform/compiler does not support ms resolution please
357 // do NOT just shut off these warnings, drop me a line instead at
360 #if defined(__VISUALC__) || defined (__WATCOMC__)
361 #pragma message("wxStopWatch will be up to second resolution!")
362 #elif defined(__BORLANDC__)
363 #pragma message "wxStopWatch will be up to second resolution!"
365 #warning "wxStopWatch will be up to second resolution!"
368 val
*= wxGetUTCTime();
370 #endif // time functions
373 wxLongLong
wxGetLocalTimeMillis()
375 return wxGetUTCTimeMillis() - wxGetTimeZone()*MILLISECONDS_PER_SECOND
;
378 #else // !wxUSE_LONGLONG
380 double wxGetLocalTimeMillis(void)
382 return (double(clock()) / double(CLOCKS_PER_SEC
)) * 1000.0;
385 #endif // wxUSE_LONGLONG/!wxUSE_LONGLONG