| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/common/stopwatch.cpp |
| 3 | // Purpose: wxStopWatch and other non-GUI stuff from wx/timer.h |
| 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: 20.06.2003 (extracted from common/timercmn.cpp) |
| 11 | // RCS-ID: $Id$ |
| 12 | // Copyright: (c) 1998-2003 wxWidgets Team |
| 13 | // License: wxWindows license |
| 14 | /////////////////////////////////////////////////////////////////////////////// |
| 15 | |
| 16 | // ============================================================================ |
| 17 | // declarations |
| 18 | // ============================================================================ |
| 19 | |
| 20 | // ---------------------------------------------------------------------------- |
| 21 | // headers |
| 22 | // ---------------------------------------------------------------------------- |
| 23 | |
| 24 | // for compilers that support precompilation, includes "wx.h". |
| 25 | #include "wx/wxprec.h" |
| 26 | |
| 27 | #ifdef __BORLANDC__ |
| 28 | #pragma hdrstop |
| 29 | #endif |
| 30 | |
| 31 | #include "wx/stopwatch.h" |
| 32 | |
| 33 | #ifndef WX_PRECOMP |
| 34 | #ifdef __WXMSW__ |
| 35 | #include "wx/msw/wrapwin.h" |
| 36 | #endif |
| 37 | #include "wx/intl.h" |
| 38 | #include "wx/log.h" |
| 39 | #endif //WX_PRECOMP |
| 40 | |
| 41 | // ---------------------------------------------------------------------------- |
| 42 | // System headers |
| 43 | // ---------------------------------------------------------------------------- |
| 44 | |
| 45 | #if defined(__WIN32__) && !defined(HAVE_FTIME) && !defined(__MWERKS__) && !defined(__WXWINCE__) |
| 46 | #define HAVE_FTIME |
| 47 | #endif |
| 48 | |
| 49 | #if defined(__VISAGECPP__) && !defined(HAVE_FTIME) |
| 50 | #define HAVE_FTIME |
| 51 | # if __IBMCPP__ >= 400 |
| 52 | # define ftime(x) _ftime(x) |
| 53 | # endif |
| 54 | #endif |
| 55 | |
| 56 | #if defined(__MWERKS__) && defined(__WXMSW__) |
| 57 | # undef HAVE_FTIME |
| 58 | # undef HAVE_GETTIMEOFDAY |
| 59 | #endif |
| 60 | |
| 61 | #ifndef __WXPALMOS5__ |
| 62 | #ifndef __WXWINCE__ |
| 63 | #include <time.h> |
| 64 | #else |
| 65 | #include "wx/msw/private.h" |
| 66 | #include "wx/msw/wince/time.h" |
| 67 | #endif |
| 68 | #endif // __WXPALMOS5__ |
| 69 | |
| 70 | |
| 71 | #if !defined(__WXMAC__) && !defined(__WXWINCE__) |
| 72 | #include <sys/types.h> // for time_t |
| 73 | #endif |
| 74 | |
| 75 | #if defined(HAVE_GETTIMEOFDAY) |
| 76 | #include <sys/time.h> |
| 77 | #include <unistd.h> |
| 78 | #elif defined(HAVE_FTIME) |
| 79 | #include <sys/timeb.h> |
| 80 | #endif |
| 81 | |
| 82 | #ifdef __WXPALMOS__ |
| 83 | #include <DateTime.h> |
| 84 | #include <TimeMgr.h> |
| 85 | #include <SystemMgr.h> |
| 86 | #endif |
| 87 | |
| 88 | // ============================================================================ |
| 89 | // implementation |
| 90 | // ============================================================================ |
| 91 | |
| 92 | // ---------------------------------------------------------------------------- |
| 93 | // wxStopWatch |
| 94 | // ---------------------------------------------------------------------------- |
| 95 | |
| 96 | #if wxUSE_STOPWATCH |
| 97 | |
| 98 | void wxStopWatch::Start(long t) |
| 99 | { |
| 100 | #if 0 |
| 101 | // __WXMSW__ |
| 102 | LARGE_INTEGER frequency_li; |
| 103 | ::QueryPerformanceFrequency( &frequency_li ); |
| 104 | m_frequency = frequency_li.QuadPart; |
| 105 | if (m_frequency == 0) |
| 106 | { |
| 107 | m_t0 = wxGetLocalTimeMillis() - t; |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | LARGE_INTEGER counter_li; |
| 112 | ::QueryPerformanceCounter( &counter_li ); |
| 113 | wxLongLong counter = counter_li.QuadPart; |
| 114 | m_t0 = (counter * 10000 / m_frequency) - t*10; |
| 115 | } |
| 116 | #else |
| 117 | m_t0 = wxGetLocalTimeMillis() - t; |
| 118 | #endif |
| 119 | m_pause = 0; |
| 120 | m_pauseCount = 0; |
| 121 | } |
| 122 | |
| 123 | long wxStopWatch::GetElapsedTime() const |
| 124 | { |
| 125 | #if 0 |
| 126 | //__WXMSW__ |
| 127 | if (m_frequency == 0) |
| 128 | { |
| 129 | return (wxGetLocalTimeMillis() - m_t0).GetLo(); |
| 130 | } |
| 131 | else |
| 132 | { |
| 133 | LARGE_INTEGER counter_li; |
| 134 | ::QueryPerformanceCounter( &counter_li ); |
| 135 | wxLongLong counter = counter_li.QuadPart; |
| 136 | wxLongLong res = (counter * 10000 / m_frequency) - m_t0; |
| 137 | return res.GetLo() / 10; |
| 138 | } |
| 139 | #else |
| 140 | return (wxGetLocalTimeMillis() - m_t0).GetLo(); |
| 141 | #endif |
| 142 | } |
| 143 | |
| 144 | long wxStopWatch::Time() const |
| 145 | { |
| 146 | return m_pauseCount ? m_pause : GetElapsedTime(); |
| 147 | } |
| 148 | |
| 149 | #endif // wxUSE_STOPWATCH |
| 150 | |
| 151 | // ---------------------------------------------------------------------------- |
| 152 | // old timer functions superceded by wxStopWatch |
| 153 | // ---------------------------------------------------------------------------- |
| 154 | |
| 155 | #if wxUSE_LONGLONG |
| 156 | |
| 157 | static wxLongLong wxStartTime = 0l; |
| 158 | |
| 159 | // starts the global timer |
| 160 | void wxStartTimer() |
| 161 | { |
| 162 | wxStartTime = wxGetLocalTimeMillis(); |
| 163 | } |
| 164 | |
| 165 | // Returns elapsed time in milliseconds |
| 166 | long wxGetElapsedTime(bool resetTimer) |
| 167 | { |
| 168 | wxLongLong oldTime = wxStartTime; |
| 169 | wxLongLong newTime = wxGetLocalTimeMillis(); |
| 170 | |
| 171 | if ( resetTimer ) |
| 172 | wxStartTime = newTime; |
| 173 | |
| 174 | return (newTime - oldTime).GetLo(); |
| 175 | } |
| 176 | |
| 177 | #endif // wxUSE_LONGLONG |
| 178 | |
| 179 | // ---------------------------------------------------------------------------- |
| 180 | // the functions to get the current time and timezone info |
| 181 | // ---------------------------------------------------------------------------- |
| 182 | |
| 183 | // Get local time as seconds since 00:00:00, Jan 1st 1970 |
| 184 | long wxGetLocalTime() |
| 185 | { |
| 186 | struct tm tm; |
| 187 | time_t t0, t1; |
| 188 | |
| 189 | // This cannot be made static because mktime can overwrite it. |
| 190 | // |
| 191 | memset(&tm, 0, sizeof(tm)); |
| 192 | tm.tm_year = 70; |
| 193 | tm.tm_mon = 0; |
| 194 | tm.tm_mday = 5; // not Jan 1st 1970 due to mktime 'feature' |
| 195 | tm.tm_hour = 0; |
| 196 | tm.tm_min = 0; |
| 197 | tm.tm_sec = 0; |
| 198 | tm.tm_isdst = -1; // let mktime guess |
| 199 | |
| 200 | // Note that mktime assumes that the struct tm contains local time. |
| 201 | // |
| 202 | t1 = time(&t1); // now |
| 203 | t0 = mktime(&tm); // origin |
| 204 | |
| 205 | // Return the difference in seconds. |
| 206 | // |
| 207 | if (( t0 != (time_t)-1 ) && ( t1 != (time_t)-1 )) |
| 208 | return (long)difftime(t1, t0) + (60 * 60 * 24 * 4); |
| 209 | |
| 210 | wxLogSysError(_("Failed to get the local system time")); |
| 211 | return -1; |
| 212 | } |
| 213 | |
| 214 | // Get UTC time as seconds since 00:00:00, Jan 1st 1970 |
| 215 | long wxGetUTCTime() |
| 216 | { |
| 217 | return (long)time(NULL); |
| 218 | } |
| 219 | |
| 220 | #if wxUSE_LONGLONG |
| 221 | |
| 222 | // Get local time as milliseconds since 00:00:00, Jan 1st 1970 |
| 223 | wxLongLong wxGetLocalTimeMillis() |
| 224 | { |
| 225 | wxLongLong val = 1000l; |
| 226 | |
| 227 | // If possible, use a function which avoids conversions from |
| 228 | // broken-up time structures to milliseconds |
| 229 | |
| 230 | #if defined(__WXPALMOS__) |
| 231 | DateTimeType thenst; |
| 232 | thenst.second = 0; |
| 233 | thenst.minute = 0; |
| 234 | thenst.hour = 0; |
| 235 | thenst.day = 1; |
| 236 | thenst.month = 1; |
| 237 | thenst.year = 1970; |
| 238 | thenst.weekDay = 5; |
| 239 | uint32_t now = TimGetSeconds(); |
| 240 | uint32_t then = TimDateTimeToSeconds (&thenst); |
| 241 | return SysTimeToMilliSecs(SysTimeInSecs(now - then)); |
| 242 | #elif defined(__WXMSW__) && (defined(__WINE__) || defined(__MWERKS__)) |
| 243 | // This should probably be the way all WXMSW compilers should do it |
| 244 | // Go direct to the OS for time |
| 245 | |
| 246 | SYSTEMTIME thenst = { 1970, 1, 4, 1, 0, 0, 0, 0 }; // 00:00:00 Jan 1st 1970 |
| 247 | FILETIME thenft; |
| 248 | SystemTimeToFileTime( &thenst, &thenft ); |
| 249 | wxLongLong then( thenft.dwHighDateTime, thenft.dwLowDateTime ); // time in 100 nanoseconds |
| 250 | |
| 251 | SYSTEMTIME nowst; |
| 252 | GetLocalTime( &nowst ); |
| 253 | FILETIME nowft; |
| 254 | SystemTimeToFileTime( &nowst, &nowft ); |
| 255 | wxLongLong now( nowft.dwHighDateTime, nowft.dwLowDateTime ); // time in 100 nanoseconds |
| 256 | |
| 257 | return ( now - then ) / 10000.0; // time from 00:00:00 Jan 1st 1970 to now in milliseconds |
| 258 | |
| 259 | #elif defined(HAVE_GETTIMEOFDAY) |
| 260 | struct timeval tp; |
| 261 | if ( wxGetTimeOfDay(&tp) != -1 ) |
| 262 | { |
| 263 | val *= tp.tv_sec; |
| 264 | return (val + (tp.tv_usec / 1000)); |
| 265 | } |
| 266 | else |
| 267 | { |
| 268 | wxLogError(_("wxGetTimeOfDay failed.")); |
| 269 | return 0; |
| 270 | } |
| 271 | #elif defined(HAVE_FTIME) |
| 272 | struct timeb tp; |
| 273 | |
| 274 | // ftime() is void and not int in some mingw32 headers, so don't |
| 275 | // test the return code (well, it shouldn't fail anyhow...) |
| 276 | (void)::ftime(&tp); |
| 277 | val *= tp.time; |
| 278 | return (val + tp.millitm); |
| 279 | #else // no gettimeofday() nor ftime() |
| 280 | // We use wxGetLocalTime() to get the seconds since |
| 281 | // 00:00:00 Jan 1st 1970 and then whatever is available |
| 282 | // to get millisecond resolution. |
| 283 | // |
| 284 | // NOTE that this might lead to a problem if the clocks |
| 285 | // use different sources, so this approach should be |
| 286 | // avoided where possible. |
| 287 | |
| 288 | val *= wxGetLocalTime(); |
| 289 | |
| 290 | // GRG: This will go soon as all WIN32 seem to have ftime |
| 291 | // JACS: unfortunately not. WinCE doesn't have it. |
| 292 | #if defined (__WIN32__) |
| 293 | // If your platform/compiler needs to use two different functions |
| 294 | // to get ms resolution, please do NOT just shut off these warnings, |
| 295 | // drop me a line instead at <guille@iies.es> |
| 296 | |
| 297 | // FIXME |
| 298 | #ifndef __WXWINCE__ |
| 299 | #warning "Possible clock skew bug in wxGetLocalTimeMillis()!" |
| 300 | #endif |
| 301 | |
| 302 | SYSTEMTIME st; |
| 303 | ::GetLocalTime(&st); |
| 304 | val += st.wMilliseconds; |
| 305 | #else // !Win32 |
| 306 | // If your platform/compiler does not support ms resolution please |
| 307 | // do NOT just shut off these warnings, drop me a line instead at |
| 308 | // <guille@iies.es> |
| 309 | |
| 310 | #if defined(__VISUALC__) || defined (__WATCOMC__) |
| 311 | #pragma message("wxStopWatch will be up to second resolution!") |
| 312 | #elif defined(__BORLANDC__) |
| 313 | #pragma message "wxStopWatch will be up to second resolution!" |
| 314 | #else |
| 315 | #warning "wxStopWatch will be up to second resolution!" |
| 316 | #endif // compiler |
| 317 | #endif |
| 318 | |
| 319 | return val; |
| 320 | |
| 321 | #endif // time functions |
| 322 | } |
| 323 | |
| 324 | #else // !wxUSE_LONGLONG |
| 325 | |
| 326 | double wxGetLocalTimeMillis(void) |
| 327 | { |
| 328 | return (double(clock()) / double(CLOCKS_PER_SEC)) * 1000.0; |
| 329 | } |
| 330 | |
| 331 | #endif // wxUSE_LONGLONG/!wxUSE_LONGLONG |