]>
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 (original version)
5 // Modified by: Vadim Zeitlin on 12.11.99 to get rid of all ifdefs
6 // Sylvain Bougnoux to add wxStopWatch class
8 // Completely rewritten from scratch on Dic/99 by
9 // Guillermo Rodriguez <guille@iies.es>
13 // Copyright: (c) Julian Smart and Markus Holzem
14 // (c) 1999 Guillermo Rodriguez <guille@iies.es>
15 // Licence: wxWindows license
16 /////////////////////////////////////////////////////////////////////////////
18 // ============================================================================
20 // ============================================================================
22 // ----------------------------------------------------------------------------
24 // ----------------------------------------------------------------------------
27 #pragma implementation "timerbase.h"
30 // For compilers that support precompilation, includes "wx.h".
31 #include "wx/wxprec.h"
43 #include "wx/longlong.h"
45 #if defined(__WIN32__)
51 #include <sys/types.h> // for time_t
54 #if defined(HAVE_GETTIMEOFDAY)
57 #elif defined(HAVE_FTIME)
58 #include <sys/timeb.h>
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 // on some really old systems gettimeofday() doesn't have the second argument,
66 // define wxGetTimeOfDay() to hide this difference
67 #ifdef HAVE_GETTIMEOFDAY
68 #ifdef WX_GETTIMEOFDAY_NO_TZ
70 #define wxGetTimeOfDay(tv, tz) gettimeofday(tv)
72 #define wxGetTimeOfDay(tv, tz) gettimeofday((tv), (tz))
74 #endif // HAVE_GETTIMEOFDAY
76 // ============================================================================
78 // ============================================================================
80 wxLongLong
wxGetLocalTimeMillis();
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 void wxStopWatch::Start(long t
)
88 m_t0
= wxGetLocalTimeMillis() - t
;
93 long wxStopWatch::Time() const
95 return (m_pause
? m_pause
: GetElapsedTime());
98 long wxStopWatch::GetElapsedTime() const
100 return (wxGetLocalTimeMillis() - m_t0
).GetLo();
103 // ----------------------------------------------------------------------------
104 // old timer functions superceded by wxStopWatch
105 // ----------------------------------------------------------------------------
107 static wxLongLong wxStartTime
= 0;
109 // starts the global timer
112 wxStartTime
= wxGetLocalTimeMillis();
115 // Returns elapsed time in milliseconds
116 long wxGetElapsedTime(bool resetTimer
)
118 wxLongLong oldTime
= wxStartTime
;
119 wxLongLong newTime
= wxGetLocalTimeMillis();
122 wxStartTime
= newTime
;
124 return (newTime
- oldTime
).GetLo();
128 // ----------------------------------------------------------------------------
129 // the functions to get the current time and timezone info
130 // ----------------------------------------------------------------------------
132 // Get local time as seconds since 00:00:00, Jan 1st 1970
133 long wxGetLocalTime()
138 // This cannot be made static because mktime can overwrite it.
140 memset(&tm
, 0, sizeof(tm
));
143 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
147 tm
.tm_isdst
= -1; // let mktime guess
149 // Note that mktime assumes that the struct tm contains local time.
151 t1
= time(&t1
); // now
152 t0
= mktime(&tm
); // origin
154 // Return the difference in seconds.
156 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
157 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
159 wxLogSysError(_("Failed to get the local system time"));
163 // Get UTC time as seconds since 00:00:00, Jan 1st 1970
169 // This cannot be made static because mktime can overwrite it
171 memset(&tm
, 0, sizeof(tm
));
174 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
178 tm
.tm_isdst
= -1; // let mktime guess
180 // Note that mktime assumes that the struct tm contains local time.
182 t1
= time(&t1
); // now
183 t0
= mktime(&tm
); // origin in localtime
185 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
187 // To get t0 as GMT we convert to a struct tm with gmtime,
188 // and then back again.
194 memcpy(&tm
, ptm
, sizeof(tm
));
197 if (t0
!= (time_t)-1 )
198 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
199 wxLogSysError(_("Failed 2nd mktime"));
201 wxLogSysError(_("Failed gmtime"));
203 wxLogSysError(_("Failed to get the UTC system time"));
208 // Get local time as milliseconds since 00:00:00, Jan 1st 1970
209 wxLongLong
wxGetLocalTimeMillis()
211 // We use wxGetLocalTime() to get the seconds since
212 // 00:00:00 Jan 1st 1970 and then whatever is available
213 // to get millisecond resolution.
215 wxLongLong val
= 1000 * wxGetLocalTime();
217 // If we got here, do not fail even if we can't get
218 // millisecond resolution.
220 #if defined(__WIN32__)
223 return (val
+ st
.wMilliseconds
);
224 #elif defined(HAVE_GETTIMEOFDAY)
226 if ( wxGetTimeOfDay(&tp
, (struct timezone
*)NULL
) != -1 )
228 return (val
+ (tp
.tv_usec
/ 1000));
230 #elif defined(HAVE_FTIME)
232 if ( ftime(&tp
) == 0 )
234 return (val
+ tp
.millitm
);