wxLongLong wxGetUTCTimeUSec() { #if defined(__WINDOWS__) FILETIME ft; ::GetSystemTimeAsFileTime(&ft); // FILETIME is in 100ns or 0.1us since 1601-01-01, transform to us since // 1970-01-01. wxLongLong t(ft.dwHighDateTime, ft.dwLowDateTime); t /= 10; t -= wxLL(11644473600000000); // Unix - Windows epochs difference in us. return t; #else // non-MSW #ifdef HAVE_GETTIMEOFDAY timeval tv; if ( wxGetTimeOfDay(&tv) != -1 ) { wxLongLong val(tv.tv_sec); val *= MICROSECONDS_PER_SECOND; val += tv.tv_usec; return val; } #endif // HAVE_GETTIMEOFDAY // Fall back to lesser precision function. return wxGetUTCTimeMillis()*MICROSECONDS_PER_MILLISECOND; #endif // MSW/!MSW }
// Get local time as milliseconds since 00:00:00, Jan 1st 1970 wxLongLong wxGetUTCTimeMillis() { // If possible, use a function which avoids conversions from // broken-up time structures to milliseconds #if defined(__WINDOWS__) FILETIME ft; ::GetSystemTimeAsFileTime(&ft); // FILETIME is expressed in 100ns (or 0.1us) units since 1601-01-01, // transform them to ms since 1970-01-01. wxLongLong t(ft.dwHighDateTime, ft.dwLowDateTime); t /= 10000; t -= wxLL(11644473600000); // Unix - Windows epochs difference in ms. return t; #else // !__WINDOWS__ wxLongLong val = MILLISECONDS_PER_SECOND; #if defined(HAVE_GETTIMEOFDAY) struct timeval tp; if ( wxGetTimeOfDay(&tp) != -1 ) { val *= tp.tv_sec; return (val + (tp.tv_usec / MICROSECONDS_PER_MILLISECOND)); } else { wxLogError(_("wxGetTimeOfDay failed.")); return 0; } #elif defined(HAVE_FTIME) struct timeb tp; // ftime() is void and not int in some mingw32 headers, so don't // test the return code (well, it shouldn't fail anyhow...) (void)::ftime(&tp); val *= tp.time; return (val + tp.millitm); #else // no gettimeofday() nor ftime() // If your platform/compiler does not support ms resolution please // do NOT just shut off these warnings, drop me a line instead at // <*****@*****.**> #if defined(__VISUALC__) #pragma message("wxStopWatch will be up to second resolution!") #elif defined(__BORLANDC__) #pragma message "wxStopWatch will be up to second resolution!" #else #warning "wxStopWatch will be up to second resolution!" #endif // compiler val *= wxGetUTCTime(); return val; #endif // time functions #endif // __WINDOWS__/!__WINDOWS__ }
wxUsecClock_t wxGetLocalTimeUsec() { #ifdef HAVE_GETTIMEOFDAY struct timeval tv; if ( wxGetTimeOfDay(&tv) != -1 ) { wxUsecClock_t val = 1000000L; // usec/sec val *= tv.tv_sec; return val + tv.tv_usec; } #endif // HAVE_GETTIMEOFDAY return wxGetLocalTimeMillis() * 1000L; }
wxMutexError wxMutexInternal::Lock(unsigned long ms) { #ifdef HAVE_PTHREAD_MUTEX_TIMEDLOCK static const long MSEC_IN_SEC = 1000; static const long NSEC_IN_MSEC = 1000000; static const long NSEC_IN_USEC = 1000; static const long NSEC_IN_SEC = MSEC_IN_SEC * NSEC_IN_MSEC; time_t seconds = ms/MSEC_IN_SEC; long nanoseconds = (ms % MSEC_IN_SEC) * NSEC_IN_MSEC; timespec ts = { 0, 0 }; // normally we should use clock_gettime(CLOCK_REALTIME) here but this // function is in librt and we don't link with it currently, so use // gettimeofday() instead -- if it turns out that this is really too // imprecise, we should modify configure to check if clock_gettime() is // available and whether it requires -lrt and use it instead #if 0 if ( clock_gettime(CLOCK_REALTIME, &ts) == 0 ) { } #else struct timeval tv; if ( wxGetTimeOfDay(&tv) != -1 ) { ts.tv_sec = tv.tv_sec; ts.tv_nsec = tv.tv_usec*NSEC_IN_USEC; } #endif else // fall back on system timer { ts.tv_sec = time(NULL); } ts.tv_sec += seconds; ts.tv_nsec += nanoseconds; if ( ts.tv_nsec > NSEC_IN_SEC ) { ts.tv_sec += 1; ts.tv_nsec -= NSEC_IN_SEC; } return HandleLockResult(pthread_mutex_timedlock(&m_mutex, &ts)); #else // !HAVE_PTHREAD_MUTEX_TIMEDLOCK wxUnusedVar(ms); return wxMUTEX_MISC_ERROR; #endif // HAVE_PTHREAD_MUTEX_TIMEDLOCK/!HAVE_PTHREAD_MUTEX_TIMEDLOCK }
// Get local time as milliseconds since 00:00:00, Jan 1st 1970 wxLongLong wxGetLocalTimeMillis() { wxLongLong val = 1000l; // If possible, use a function which avoids conversions from // broken-up time structures to milliseconds #if defined(__WXPALMOS__) DateTimeType thenst; thenst.second = 0; thenst.minute = 0; thenst.hour = 0; thenst.day = 1; thenst.month = 1; thenst.year = 1970; thenst.weekDay = 5; uint32_t now = TimGetSeconds(); uint32_t then = TimDateTimeToSeconds (&thenst); return SysTimeToMilliSecs(SysTimeInSecs(now - then)); #elif defined(__WXMSW__) && (defined(__WINE__) || defined(__MWERKS__)) // This should probably be the way all WXMSW compilers should do it // Go direct to the OS for time SYSTEMTIME thenst = { 1970, 1, 4, 1, 0, 0, 0, 0 }; // 00:00:00 Jan 1st 1970 FILETIME thenft; SystemTimeToFileTime( &thenst, &thenft ); wxLongLong then( thenft.dwHighDateTime, thenft.dwLowDateTime ); // time in 100 nanoseconds SYSTEMTIME nowst; GetLocalTime( &nowst ); FILETIME nowft; SystemTimeToFileTime( &nowst, &nowft ); wxLongLong now( nowft.dwHighDateTime, nowft.dwLowDateTime ); // time in 100 nanoseconds return ( now - then ) / 10000.0; // time from 00:00:00 Jan 1st 1970 to now in milliseconds #elif defined(HAVE_GETTIMEOFDAY) struct timeval tp; if ( wxGetTimeOfDay(&tp) != -1 ) { val *= tp.tv_sec; return (val + (tp.tv_usec / 1000)); } else { wxLogError(_("wxGetTimeOfDay failed.")); return 0; } #elif defined(HAVE_FTIME) struct timeb tp; // ftime() is void and not int in some mingw32 headers, so don't // test the return code (well, it shouldn't fail anyhow...) (void)::ftime(&tp); val *= tp.time; return (val + tp.millitm); #else // no gettimeofday() nor ftime() // We use wxGetLocalTime() to get the seconds since // 00:00:00 Jan 1st 1970 and then whatever is available // to get millisecond resolution. // // NOTE that this might lead to a problem if the clocks // use different sources, so this approach should be // avoided where possible. val *= wxGetLocalTime(); // GRG: This will go soon as all WIN32 seem to have ftime // JACS: unfortunately not. WinCE doesn't have it. #if defined (__WIN32__) // If your platform/compiler needs to use two different functions // to get ms resolution, please do NOT just shut off these warnings, // drop me a line instead at <*****@*****.**> // FIXME #ifndef __WXWINCE__ #warning "Possible clock skew bug in wxGetLocalTimeMillis()!" #endif SYSTEMTIME st; ::GetLocalTime(&st); val += st.wMilliseconds; #else // !Win32 // If your platform/compiler does not support ms resolution please // do NOT just shut off these warnings, drop me a line instead at // <*****@*****.**> #if defined(__VISUALC__) || defined (__WATCOMC__) #pragma message("wxStopWatch will be up to second resolution!") #elif defined(__BORLANDC__) #pragma message "wxStopWatch will be up to second resolution!" #else #warning "wxStopWatch will be up to second resolution!" #endif // compiler #endif return val; #endif // time functions }