]>
git.saurik.com Git - wxWidgets.git/blob - src/common/timercmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/timercmn.cpp
3 // Purpose: Common timer implementation
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin on 12.11.99 to get rid of all ifdefs
6 // Sylvain Bougnoux on ?? to add wxStopWatch class
7 // Guillermo Rodriguez <guille@iies.es>, 12/99 complete rewrite.
10 // Copyright: (c) Julian Smart and Markus Holzem
11 // Licence: wxWindows license
12 /////////////////////////////////////////////////////////////////////////////
14 // ============================================================================
16 // ============================================================================
18 // ----------------------------------------------------------------------------
20 // ----------------------------------------------------------------------------
23 #pragma implementation "timerbase.h"
26 // For compilers that support precompilation, includes "wx.h".
27 #include "wx/wxprec.h"
39 #include "wx/longlong.h"
41 #if defined(__WIN32__)
47 #include <sys/types.h> // for time_t
50 #if defined(HAVE_GETTIMEOFDAY)
53 #elif defined(HAVE_FTIME)
54 #include <sys/timeb.h>
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 // on some really old systems gettimeofday() doesn't have the second argument,
62 // define wxGetTimeOfDay() to hide this difference
63 #ifdef HAVE_GETTIMEOFDAY
64 #ifdef WX_GETTIMEOFDAY_NO_TZ
66 #define wxGetTimeOfDay(tv, tz) gettimeofday(tv)
68 #define wxGetTimeOfDay(tv, tz) gettimeofday((tv), (tz))
70 #endif // HAVE_GETTIMEOFDAY
72 // ============================================================================
74 // ============================================================================
76 wxLongLong
wxGetLocalTimeMillis();
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 void wxStopWatch::Start(long t
)
84 m_t0
= wxGetLocalTimeMillis() - t
;
89 long wxStopWatch::Time() const
91 return (m_pause
? m_pause
: GetElapsedTime());
94 long wxStopWatch::GetElapsedTime() const
96 return (wxGetLocalTimeMillis() - m_t0
).GetLo();
99 // ----------------------------------------------------------------------------
100 // old timer functions superceded by wxStopWatch
101 // ----------------------------------------------------------------------------
103 static wxLongLong wxStartTime
= 0;
105 // starts the global timer
108 wxStartTime
= wxGetLocalTimeMillis();
111 // Returns elapsed time in milliseconds
112 long wxGetElapsedTime(bool resetTimer
)
114 wxLongLong oldTime
= wxStartTime
;
115 wxLongLong newTime
= wxGetLocalTimeMillis();
118 wxStartTime
= newTime
;
120 return (newTime
- oldTime
).GetLo();
124 // ----------------------------------------------------------------------------
125 // the functions to get the current time and timezone info
126 // ----------------------------------------------------------------------------
128 // Get local time as seconds since 00:00:00, Jan 1st 1970
129 long wxGetLocalTime()
134 // This cannot be made static because mktime can overwrite it.
136 memset(&tm
, 0, sizeof(tm
));
139 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
143 tm
.tm_isdst
= -1; // let mktime guess
145 // Note that mktime assumes that the struct tm contains local time.
147 t1
= time(&t1
); // now
148 t0
= mktime(&tm
); // origin
150 // Return the difference in seconds.
152 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
153 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
155 wxLogSysError(_("Failed to get the local system time"));
159 // Get UTC time as seconds since 00:00:00, Jan 1st 1970
165 // This cannot be made static because mktime can overwrite it
167 memset(&tm
, 0, sizeof(tm
));
170 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
174 tm
.tm_isdst
= -1; // let mktime guess
176 // Note that mktime assumes that the struct tm contains local time.
178 t1
= time(&t1
); // now
179 t0
= mktime(&tm
); // origin in localtime
181 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
183 // To get t0 as GMT we convert to a struct tm with gmtime,
184 // and then back again.
190 memcpy(&tm
, ptm
, sizeof(tm
));
193 if (t0
!= (time_t)-1 )
194 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
195 wxLogSysError(_("Failed 2nd mktime"));
197 wxLogSysError(_("Failed gmtime"));
199 wxLogSysError(_("Failed to get the UTC system time"));
204 // Get local time as milliseconds since 00:00:00, Jan 1st 1970
205 wxLongLong
wxGetLocalTimeMillis()
207 // We use wxGetLocalTime() to get the seconds since
208 // 00:00:00 Jan 1st 1970 and then whatever is available
209 // to get millisecond resolution.
211 wxLongLong val
= 1000 * wxGetLocalTime();
213 // If we got here, do not fail even if we can't get
214 // millisecond resolution.
216 #if defined(__WIN32__)
219 return (val
+ st
.wMilliseconds
);
220 #elif defined(HAVE_GETTIMEOFDAY)
222 if ( wxGetTimeOfDay(&tp
, (struct timezone
*)NULL
) != -1 )
224 return (val
+ (tp
.tv_usec
/ 1000));
226 #elif defined(HAVE_FTIME)
228 if ( ftime(&tp
) == 0 )
230 return (val
+ tp
.millitm
);