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__) || defined(__WXPM__) || defined(__WXMAC__)
40 // configure might have found it already for us
41 #ifndef HAVE_LOCALTIME
42 #define HAVE_LOCALTIME
46 // TODO: #define WX_GMTOFF_IN_TM for Windows compilers which have it here
48 #if defined(__WIN32__) && !defined(WX_GMTOFF_IN_TM)
52 #if defined(HAVE_GETTIMEOFDAY)
55 #elif defined(HAVE_LOCALTIME)
58 #include <sys/types.h> // for time_t
60 #elif defined(HAVE_FTIME)
61 #include <sys/timeb.h>
63 #error "no function to find the current time on this system"
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 // on some really old systems gettimeofday() doesn't have the second argument,
71 // define wxGetTimeOfDay() to hide this difference
72 #ifdef HAVE_GETTIMEOFDAY
73 #ifdef WX_GETTIMEOFDAY_NO_TZ
75 #define wxGetTimeOfDay(tv, tz) gettimeofday(tv)
77 #define wxGetTimeOfDay(tv, tz) gettimeofday((tv), (tz))
79 #endif // HAVE_GETTIMEOFDAY
81 // ============================================================================
83 // ============================================================================
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 void wxStopWatch::Start(long t
)
91 m_t0
= wxGetCurrentMTime() - t
;
96 long wxStopWatch::Time() const
98 return m_pause
? m_pause
: GetElapsedTime();
101 // ----------------------------------------------------------------------------
102 // old timer functions superceded by wxStopWatch
103 // ----------------------------------------------------------------------------
105 static long wxStartTime
= 0;
107 // starts the global timer
110 wxStartTime
= wxGetCurrentMTime();
113 // Returns elapsed time in milliseconds
114 long wxGetElapsedTime(bool resetTimer
)
116 long oldTime
= wxStartTime
;
117 long newTime
= wxGetCurrentMTime();
120 wxStartTime
= newTime
;
122 return newTime
- oldTime
;
126 // Get number of seconds since 00:00:00 GMT, Jan 1st 1970.
127 long wxGetCurrentTime()
129 return wxGetCurrentMTime() / 1000;
132 // ----------------------------------------------------------------------------
133 // the functions to get the current time and timezone info
134 // ----------------------------------------------------------------------------
136 // return GMT time in millisecond
137 long wxGetCurrentMTime()
139 #if defined(__WIN32__)
143 return 1000*(60*(60*st
.wHour
+st
.wMinute
)+st
.wSecond
)+st
.wMilliseconds
;
145 #if defined(HAVE_LOCALTIME)
146 time_t t0
= time(&t0
);
147 if ( t0
!= (time_t)-1 )
149 struct tm
*tp
= localtime(&t0
);
153 return 1000*(60*(60*tp
->tm_hour
+tp
->tm_min
)+tp
->tm_sec
);
156 #elif defined(HAVE_GETTIMEOFDAY)
158 if ( wxGetTimeOfDay(&tp
, (struct timezone
*)NULL
) != -1 )
160 return (1000*tp
.tv_sec
+ tp
.tv_usec
/ 1000);
162 #elif defined(HAVE_FTIME)
164 if ( ftime(&tp
) == 0 )
166 return (1000*tp
.time
+ tp
.millitm
);
169 #error "no function to find the current time on this system"
172 wxLogSysError(_("Failed to get the system time"));
175 #endif // __WIN32__/!__WIN32__
178 bool wxGetLocalTime(long *timeZone
, int *dstObserved
)
180 #if defined(HAVE_LOCALTIME) && defined(WX_GMTOFF_IN_TM)
181 time_t t0
= time(&t0
);
182 if ( t0
!= (time_t)-1 )
184 struct tm
*tm
= localtime(&t0
);
188 *timeZone
= tm
->tm_gmtoff
;
189 *dstObserved
= tm
->tm_isdst
;
194 #elif defined(HAVE_GETTIMEOFDAY) && !defined(WX_GETTIMEOFDAY_NO_TZ)
197 if ( gettimeofday(&tp
, &tz
) != -1 )
199 *timeZone
= 60*tz
.tz_minuteswest
;
200 *dstObserved
= tz
.tz_dsttime
;
204 #elif defined(HAVE_FTIME)
206 if ( ftime(&tb
) == 0 )
208 *timeZone
= 60*tb
.timezone
;
209 *dstObserved
= tb
.dstflag
;
211 #else // no standard function return tz info
212 // special hacks for known compilers
213 #if defined(__BORLANDC__) || defined(__VISUALC__)
214 *timeZone
= _timezone
;
215 *dstObserved
= _daylight
;
216 #elif defined(__SALFORDC__)
217 *timeZone
= _timezone
;
218 *dstObserved
= daylight
;
219 #elif defined(__VISAGECPP__)
220 *timeZone
= _timezone
;
221 *dstObserved
= daylight
;
222 #elif defined(__WIN32__)
223 TIME_ZONE_INFORMATION tzInfo
;
224 switch ( GetTimeZoneInformation(&tzInfo
) )
227 wxFAIL_MSG(_T("unknown GetTimeZoneInformation return code"));
230 case TIME_ZONE_ID_UNKNOWN
:
231 case TIME_ZONE_ID_STANDARD
:
232 *dstObserved
= FALSE
;
235 case TIME_ZONE_ID_DAYLIGHT
:
240 *timeZone
= 60*tzInfo
.Bias
;
242 wxFAIL_MSG(_T("wxGetLocalTime() not implemented"));
244 #endif // all ways in the known Universe to get tz info