]> git.saurik.com Git - wxWidgets.git/blob - src/common/timercmn.cpp
Watcom C++ fixup in tbar95.cpp; removed WXWIN_COMPATIBILITY for 'old' menu
[wxWidgets.git] / 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 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "timerbase.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/intl.h"
33 #include "wx/log.h"
34 #endif
35
36 #include "wx/timer.h"
37
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
43 #endif
44 #endif
45
46 // TODO: #define WX_GMTOFF_IN_TM for Windows compilers which have it here
47
48 #if defined(__WIN32__) && !defined(WX_GMTOFF_IN_TM)
49 #include <winbase.h>
50 #endif
51
52 #if defined(HAVE_GETTIMEOFDAY)
53 #include <sys/time.h>
54 #include <unistd.h>
55 #elif defined(HAVE_LOCALTIME)
56 #include <time.h>
57 #ifndef __WXMAC__
58 #include <sys/types.h> // for time_t
59 #endif
60 #elif defined(HAVE_FTIME)
61 #include <sys/timeb.h>
62 #else
63 #error "no function to find the current time on this system"
64 #endif
65
66 // ----------------------------------------------------------------------------
67 // macros
68 // ----------------------------------------------------------------------------
69
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
74 struct timezone;
75 #define wxGetTimeOfDay(tv, tz) gettimeofday(tv)
76 #else
77 #define wxGetTimeOfDay(tv, tz) gettimeofday((tv), (tz))
78 #endif
79 #endif // HAVE_GETTIMEOFDAY
80
81 // ============================================================================
82 // implementation
83 // ============================================================================
84
85 // ----------------------------------------------------------------------------
86 // wxStopWatch
87 // ----------------------------------------------------------------------------
88
89 void wxStopWatch::Start(long t)
90 {
91 m_t0 = wxGetCurrentMTime() - t;
92
93 m_pause = 0;
94 }
95
96 long wxStopWatch::Time() const
97 {
98 return m_pause ? m_pause : GetElapsedTime();
99 }
100
101 // ----------------------------------------------------------------------------
102 // old timer functions superceded by wxStopWatch
103 // ----------------------------------------------------------------------------
104
105 static long wxStartTime = 0;
106
107 // starts the global timer
108 void wxStartTimer()
109 {
110 wxStartTime = wxGetCurrentMTime();
111 }
112
113 // Returns elapsed time in milliseconds
114 long wxGetElapsedTime(bool resetTimer)
115 {
116 long oldTime = wxStartTime;
117 long newTime = wxGetCurrentMTime();
118
119 if ( resetTimer )
120 wxStartTime = newTime;
121
122 return newTime - oldTime;
123 }
124
125
126 // Get number of seconds since 00:00:00 GMT, Jan 1st 1970.
127 long wxGetCurrentTime()
128 {
129 return wxGetCurrentMTime() / 1000;
130 }
131
132 // ----------------------------------------------------------------------------
133 // the functions to get the current time and timezone info
134 // ----------------------------------------------------------------------------
135
136 // return GMT time in millisecond
137 long wxGetCurrentMTime()
138 {
139 #if defined(HAVE_LOCALTIME)
140 time_t t0 = time(&t0);
141 if ( t0 != (time_t)-1 )
142 {
143 struct tm *tp = localtime(&t0);
144
145 if ( tp )
146 {
147 return 1000*(60*(60*tp->tm_hour+tp->tm_min)+tp->tm_sec);
148 }
149 }
150 #elif defined(HAVE_GETTIMEOFDAY)
151 struct timeval tp;
152 if ( wxGetTimeOfDay(&tp, (struct timezone *)NULL) != -1 )
153 {
154 return (1000*tp.tv_sec + tp.tv_usec / 1000);
155 }
156 #elif defined(HAVE_FTIME)
157 struct timeb tp;
158 if ( ftime(&tp) == 0 )
159 {
160 return (1000*tp.time + tp.millitm);
161 }
162 #else
163 #error "no function to find the current time on this system"
164 #endif
165
166 wxLogSysError(_("Failed to get the system time"));
167
168 return -1;
169 }
170
171 bool wxGetLocalTime(long *timeZone, int *dstObserved)
172 {
173 #if defined(HAVE_LOCALTIME) && defined(WX_GMTOFF_IN_TM)
174 time_t t0 = time(&t0);
175 if ( t0 != (time_t)-1 )
176 {
177 struct tm *tm = localtime(&t0);
178
179 if ( tm )
180 {
181 *timeZone = tm->tm_gmtoff;
182 *dstObserved = tm->tm_isdst;
183
184 return TRUE;
185 }
186 }
187 #elif defined(HAVE_GETTIMEOFDAY) && !defined(WX_GETTIMEOFDAY_NO_TZ)
188 struct timeval tp;
189 struct timezone tz;
190 if ( gettimeofday(&tp, &tz) != -1 )
191 {
192 *timeZone = 60*tz.tz_minuteswest;
193 *dstObserved = tz.tz_dsttime;
194
195 return TRUE;
196 }
197 #elif defined(HAVE_FTIME)
198 struct timeb tb;
199 if ( ftime(&tb) == 0 )
200 {
201 *timeZone = 60*tb.timezone;
202 *dstObserved = tb.dstflag;
203 }
204 #else // no standard function return tz info
205 // special hacks for known compilers
206 #if defined(__BORLANDC__) || defined(__VISUALC__)
207 *timeZone = _timezone;
208 *dstObserved = _daylight;
209 #elif defined(__SALFORDC__)
210 *timeZone = _timezone;
211 *dstObserved = daylight;
212 #elif defined(__VISAGECPP__)
213 *timeZone = _timezone;
214 *dstObserved = daylight;
215 #elif defined(__WIN32__)
216 TIME_ZONE_INFORMATION tzInfo;
217 switch ( GetTimeZoneInformation(&tzInfo) )
218 {
219 default:
220 wxFAIL_MSG(_T("unknown GetTimeZoneInformation return code"));
221 // fall through
222
223 case TIME_ZONE_ID_UNKNOWN:
224 case TIME_ZONE_ID_STANDARD:
225 *dstObserved = FALSE;
226 break;
227
228 case TIME_ZONE_ID_DAYLIGHT:
229 *dstObserved = TRUE;
230 break;
231 }
232
233 *timeZone = 60*tzInfo.Bias;
234 #else
235 wxFAIL_MSG(_T("wxGetLocalTime() not implemented"));
236 #endif // compiler
237 #endif // all ways in the known Universe to get tz info
238
239 return FALSE;
240 }