]>
git.saurik.com Git - wxWidgets.git/blob - src/common/timercmn.cpp
e2ac6496aed56eb739a7fac24e67936b1a04896f
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
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "timerbase.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
38 // I'm told VMS is POSIX, so should have localtime()
39 #if defined(__WXMSW__) || defined(__VMS__)
40 // configure might have found it already for us
41 #ifndef HAVE_LOCALTIME
42 #define HAVE_LOCALTIME
44 // TODO add test for broken compilers here if needed
45 #define WX_GMTOFF_IN_TM
49 #if defined(HAVE_GETTIMEOFDAY)
52 #elif defined(HAVE_LOCALTIME)
55 #include <sys/types.h> // for time_t
57 #elif defined(HAVE_FTIME)
58 #include <sys/timeb.h>
60 #error "no function to find the current time on this system"
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 // on some really old systems gettimeofday() doesn't have the second argument,
68 // define wxGetTimeOfDay() to hide this difference
69 #ifdef HAVE_GETTIMEOFDAY
70 #ifdef WX_GETTIMEOFDAY_NO_TZ
72 #define wxGetTimeOfDay(tv, tz) gettimeofday(tv)
74 #define wxGetTimeOfDay(tv, tz) gettimeofday((tv), (tz))
76 #endif // HAVE_GETTIMEOFDAY
78 // ============================================================================
80 // ============================================================================
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 void wxStopWatch::Start(long t
)
88 m_t0
= wxGetCurrentMTime() - t
;
93 long wxStopWatch::Time() const
95 return m_pause
? m_pause
: GetElapsedTime();
98 // ----------------------------------------------------------------------------
99 // old timer functions superceded by wxStopWatch
100 // ----------------------------------------------------------------------------
102 static long wxStartTime
= 0;
104 // starts the global timer
107 wxStartTime
= wxGetCurrentMTime();
110 // Returns elapsed time in milliseconds
111 long wxGetElapsedTime(bool resetTimer
)
113 long oldTime
= wxStartTime
;
114 long newTime
= wxGetCurrentMTime();
117 wxStartTime
= newTime
;
119 return newTime
- oldTime
;
123 // Get number of seconds since 00:00:00 GMT, Jan 1st 1970.
124 long wxGetCurrentTime()
126 return wxGetCurrentMTime() / 1000;
129 // ----------------------------------------------------------------------------
130 // the functions to get the current time and timezone info
131 // ----------------------------------------------------------------------------
133 // return GMT time in millisecond
134 long wxGetCurrentMTime()
136 #if defined(HAVE_LOCALTIME)
137 time_t t0
= time(&t0
);
138 if ( t0
!= (time_t)-1 )
140 struct tm
*tp
= localtime(&t0
);
144 return 1000*(60*(60*tp
->tm_hour
+tp
->tm_min
)+tp
->tm_sec
);
147 #elif defined(HAVE_GETTIMEOFDAY)
149 if ( wxGetTimeOfDay(&tp
, (struct timezone
*)NULL
) != -1 )
151 return (1000*tp
.tv_sec
+ tp
.tv_usec
/ 1000);
153 #elif defined(HAVE_FTIME)
155 if ( ftime(&tp
) == 0 )
157 return (1000*tp
.time
+ tp
.millitm
);
160 #error "no function to find the current time on this system"
163 wxLogSysError(_("Failed to get the system time"));
168 bool wxGetLocalTime(long *timeZone
, int *dstObserved
)
170 #if defined(HAVE_LOCALTIME) && defined(WX_GMTOFF_IN_TM)
171 time_t t0
= time(&t0
);
172 if ( t0
!= (time_t)-1 )
174 struct tm
*tm
= localtime(&t0
);
178 *timeZone
= tm
->tm_gmtoff
;
179 *dstObserved
= tm
->tm_isdst
;
184 #elif defined(HAVE_GETTIMEOFDAY) && !defined(WX_GETTIMEOFDAY_NO_TZ)
187 if ( gettimeofday(&tp
, &tz
) != -1 )
189 *timeZone
= 60*tz
.tz_minuteswest
;
190 *dstObserved
= tz
.tz_dsttime
;
194 #elif defined(HAVE_FTIME)
196 if ( ftime(&tb
) == 0 )
198 *timeZone
= 60*tb
.timezone
;
199 *dstObserved
= tb
.dstflag
;
202 // special hacks for known compilers - I wonder if this is still needed,
203 // i.e. if there are any of them which don't support localtime()? (VZ)
205 #if defined(__BORLANDC__)
206 *timeZone
= _timezone
;
207 *dstObserved
= _daylight
;
208 #elif defined(__SALFORDC__)
209 *timeZone
= _timezone
;
210 *dstObserved
= daylight
;
211 #elif defined(__VISAGECPP__)
212 *timeZone
= _timezone
;
213 *dstObserved
= daylight
;
215 wxFAIL_MSG(_T("wxGetLocalTime() not implemented"));