]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/stopwatch.cpp
fix for bug 1371386, with some minor mods and cleanup
[wxWidgets.git] / src / common / stopwatch.cpp
index f11495333211ad3a537ec6e7959e361597030993..cd13eb058882e55cee011b27bd59f164cbde7523 100644 (file)
@@ -9,7 +9,7 @@
 // Modified by:
 // Created:     20.06.2003 (extracted from common/timercmn.cpp)
 // RCS-ID:      $Id$
-// Copyright:   (c) 1998-2003 wxWindows Team
+// Copyright:   (c) 1998-2003 wxWidgets Team
 // License:     wxWindows license
 ///////////////////////////////////////////////////////////////////////////////
 
     #include "wx/log.h"
 #endif //WX_PRECOMP
 
-#include "wx/timer.h"
 #include "wx/longlong.h"
+#include "wx/stopwatch.h"
 
 // ----------------------------------------------------------------------------
 // System headers
 // ----------------------------------------------------------------------------
 
 #if defined(__WIN32__)
-    #include <windows.h>
+    #include "wx/msw/wrapwin.h"
 #endif
 
 #if defined(__WIN32__) && !defined(HAVE_FTIME) && !defined(__MWERKS__) && !defined(__WXWINCE__)
 #endif
 
 #ifdef __WXMAC__
+#ifndef __DARWIN__
     #include <Timer.h>
     #include <DriverServices.h>
+#else
+    #include <Carbon/Carbon.h>
+#endif
+#endif
+
+#ifdef __WXPALMOS__
+    #include <DateTime.h>
+    #include <TimeMgr.h>
+    #include <SystemMgr.h>
 #endif
 
 // ----------------------------------------------------------------------------
 
 void wxStopWatch::Start(long t)
 {
+#if 0
+// __WXMSW__
+    LARGE_INTEGER frequency_li;
+    ::QueryPerformanceFrequency( &frequency_li );
+    m_frequency = frequency_li.QuadPart;
+    if (m_frequency == 0)
+    {
+        m_t0 = wxGetLocalTimeMillis() - t;
+    }
+    else
+    {
+        LARGE_INTEGER counter_li;
+        ::QueryPerformanceCounter( &counter_li );
+        wxLongLong counter = counter_li.QuadPart;
+        m_t0 = (counter * 10000 / m_frequency) - t*10;
+    }
+#else
     m_t0 = wxGetLocalTimeMillis() - t;
+#endif
     m_pause = 0;
     m_pauseCount = 0;
 }
 
 long wxStopWatch::GetElapsedTime() const
 {
+#if 0
+//__WXMSW__
+    if (m_frequency == 0)
+    {
+        return (wxGetLocalTimeMillis() - m_t0).GetLo();
+    }
+    else
+    {
+        LARGE_INTEGER counter_li;
+        ::QueryPerformanceCounter( &counter_li );
+        wxLongLong counter = counter_li.QuadPart;
+        wxLongLong res = (counter * 10000 / m_frequency) - m_t0;
+        return res.GetLo() / 10;
+    }
+#else
     return (wxGetLocalTimeMillis() - m_t0).GetLo();
+#endif
 }
 
 long wxStopWatch::Time() const
@@ -193,51 +237,7 @@ long wxGetLocalTime()
 // Get UTC time as seconds since 00:00:00, Jan 1st 1970
 long wxGetUTCTime()
 {
-    struct tm tm;
-    struct tm *ptm;
-    time_t t0, t1;
-
-    // This cannot be made static because mktime can overwrite it
-    //
-    memset(&tm, 0, sizeof(tm));
-    tm.tm_year  = 70;
-    tm.tm_mon   = 0;
-    tm.tm_mday  = 5;        // not Jan 1st 1970 due to mktime 'feature'
-    tm.tm_hour  = 0;
-    tm.tm_min   = 0;
-    tm.tm_sec   = 0;
-    tm.tm_isdst = -1;       // let mktime guess
-
-    // Note that mktime assumes that the struct tm contains local time.
-    //
-    t1 = time(&t1);         // now
-    t0 = mktime(&tm);       // origin in localtime
-
-    if (( t0 != (time_t)-1 ) && ( t1 != (time_t)-1 ))
-    {
-        // To get t0 as GMT we convert to a struct tm with gmtime,
-        // and then back again.
-        //
-        ptm = gmtime(&t0);
-
-        if (ptm)
-        {
-            memcpy(&tm, ptm, sizeof(tm));
-            t0 = mktime(&tm);
-
-            if (t0 != (time_t)-1 )
-                return (long)difftime(t1, t0) + (60 * 60 * 24 * 4);
-            wxLogSysError(_("mktime() failed"));
-        }
-        else
-        {
-            wxLogSysError(_("gmtime() failed"));
-        }
-    }
-
-    wxLogError(_("Failed to get the UTC system time."));
-
-    return -1;
+    return (long)time(NULL);
 }
 
 #if wxUSE_LONGLONG
@@ -250,7 +250,19 @@ wxLongLong wxGetLocalTimeMillis()
     // If possible, use a function which avoids conversions from
     // broken-up time structures to milliseconds
 
-#if defined(__WXMSW__) && defined(__MWERKS__)
+#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
 
@@ -266,7 +278,7 @@ wxLongLong wxGetLocalTimeMillis()
     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, (struct timezone *)NULL) != -1 )
@@ -284,11 +296,11 @@ wxLongLong wxGetLocalTimeMillis()
 
     // 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);
+    (void)::ftime(&tp);
     val *= tp.time;
     return (val + tp.millitm);
 #elif defined(__WXMAC__)
-    
+
     static UInt64 gMilliAtStart = 0;
 
     Nanoseconds upTime = AbsoluteToNanoseconds( UpTime() );