]>
git.saurik.com Git - wxWidgets.git/blob - src/common/timercmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/timercmn.cpp
3 // Purpose: Common timer implementation
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)
12 // Copyright: (c) Julian Smart and Markus Holzem
13 // (c) 1999 Guillermo Rodriguez <guille@iies.es>
14 // Licence: wxWindows license
15 /////////////////////////////////////////////////////////////////////////////
17 // ============================================================================
19 // ============================================================================
21 // ----------------------------------------------------------------------------
23 // ----------------------------------------------------------------------------
26 #pragma implementation "timerbase.h"
29 // For compilers that support precompilation, includes "wx.h".
30 #include "wx/wxprec.h"
42 #include "wx/longlong.h"
44 #if defined(__WIN32__)
50 #include <sys/types.h> // for time_t
53 #if defined(HAVE_GETTIMEOFDAY)
56 #elif defined(HAVE_FTIME)
57 #include <sys/timeb.h>
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 // on some really old systems gettimeofday() doesn't have the second argument,
65 // define wxGetTimeOfDay() to hide this difference
66 #ifdef HAVE_GETTIMEOFDAY
67 #ifdef WX_GETTIMEOFDAY_NO_TZ
69 #define wxGetTimeOfDay(tv, tz) gettimeofday(tv)
71 #define wxGetTimeOfDay(tv, tz) gettimeofday((tv), (tz))
73 #endif // HAVE_GETTIMEOFDAY
75 // ============================================================================
77 // ============================================================================
79 wxLongLong
wxGetLocalTimeMillis();
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 void wxStopWatch::Start(long t
)
87 m_t0
= wxGetLocalTimeMillis() - t
;
92 inline long wxStopWatch::GetElapsedTime() const
94 return (wxGetLocalTimeMillis() - m_t0
).GetLo();
97 long wxStopWatch::Time() const
99 return (m_pause
? m_pause
: GetElapsedTime());
102 // ----------------------------------------------------------------------------
103 // old timer functions superceded by wxStopWatch
104 // ----------------------------------------------------------------------------
106 static wxLongLong wxStartTime
= 0l;
108 // starts the global timer
111 wxStartTime
= wxGetLocalTimeMillis();
114 // Returns elapsed time in milliseconds
115 long wxGetElapsedTime(bool resetTimer
)
117 wxLongLong oldTime
= wxStartTime
;
118 wxLongLong newTime
= wxGetLocalTimeMillis();
121 wxStartTime
= newTime
;
123 return (newTime
- oldTime
).GetLo();
127 // ----------------------------------------------------------------------------
128 // the functions to get the current time and timezone info
129 // ----------------------------------------------------------------------------
131 // Get local time as seconds since 00:00:00, Jan 1st 1970
132 long wxGetLocalTime()
137 // This cannot be made static because mktime can overwrite it.
139 memset(&tm
, 0, sizeof(tm
));
142 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
146 tm
.tm_isdst
= -1; // let mktime guess
148 // Note that mktime assumes that the struct tm contains local time.
150 t1
= time(&t1
); // now
151 t0
= mktime(&tm
); // origin
153 // Return the difference in seconds.
155 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
156 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
158 wxLogSysError(_("Failed to get the local system time"));
162 // Get UTC time as seconds since 00:00:00, Jan 1st 1970
168 // This cannot be made static because mktime can overwrite it
170 memset(&tm
, 0, sizeof(tm
));
173 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
177 tm
.tm_isdst
= -1; // let mktime guess
179 // Note that mktime assumes that the struct tm contains local time.
181 t1
= time(&t1
); // now
182 t0
= mktime(&tm
); // origin in localtime
184 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
186 // To get t0 as GMT we convert to a struct tm with gmtime,
187 // and then back again.
193 memcpy(&tm
, ptm
, sizeof(tm
));
196 if (t0
!= (time_t)-1 )
197 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
198 wxLogSysError(_("Failed 2nd mktime"));
200 wxLogSysError(_("Failed gmtime"));
202 wxLogSysError(_("Failed to get the UTC system time"));
207 // Get local time as milliseconds since 00:00:00, Jan 1st 1970
208 wxLongLong
wxGetLocalTimeMillis()
210 // We use wxGetLocalTime() to get the seconds since
211 // 00:00:00 Jan 1st 1970 and then whatever is available
212 // to get millisecond resolution.
214 wxLongLong val
= 1000l;
215 val
*= 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
);
237 #warning "wxStopWatch will be up to second resolution!"