wxTimer/timercmn.cpp change
[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
44 // TODO add test for broken compilers here if needed
45 #define WX_GMTOFF_IN_TM
46 #endif
47 #endif
48
49 #if defined(HAVE_GETTIMEOFDAY)
50 #include <sys/time.h>
51 #include <unistd.h>
52 #elif defined(HAVE_LOCALTIME)
53 #include <time.h>
54 #ifndef __WXMAC__
55 #include <sys/types.h> // for time_t
56 #endif
57 #elif defined(HAVE_FTIME)
58 #include <sys/timeb.h>
59 #else
60 #error "no function to find the current time on this system"
61 #endif
62
63 // ----------------------------------------------------------------------------
64 // macros
65 // ----------------------------------------------------------------------------
66
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
71 struct timezone;
72 #define wxGetTimeOfDay(tv, tz) gettimeofday(tv)
73 #else
74 #define wxGetTimeOfDay(tv, tz) gettimeofday((tv), (tz))
75 #endif
76 #endif // HAVE_GETTIMEOFDAY
77
78 // ============================================================================
79 // implementation
80 // ============================================================================
81
82 // ----------------------------------------------------------------------------
83 // wxStopWatch
84 // ----------------------------------------------------------------------------
85
86 void wxStopWatch::Start(long t)
87 {
88 m_t0 = wxGetCurrentMTime() - t;
89
90 m_pause = 0;
91 }
92
93 long wxStopWatch::Time() const
94 {
95 return m_pause ? m_pause : GetElapsedTime();
96 }
97
98 // ----------------------------------------------------------------------------
99 // old timer functions superceded by wxStopWatch
100 // ----------------------------------------------------------------------------
101
102 static long wxStartTime = 0;
103
104 // starts the global timer
105 void wxStartTimer()
106 {
107 wxStartTime = wxGetCurrentMTime();
108 }
109
110 // Returns elapsed time in milliseconds
111 long wxGetElapsedTime(bool resetTimer)
112 {
113 long oldTime = wxStartTime;
114 long newTime = wxGetCurrentMTime();
115
116 if ( resetTimer )
117 wxStartTime = newTime;
118
119 return newTime - oldTime;
120 }
121
122
123 // Get number of seconds since 00:00:00 GMT, Jan 1st 1970.
124 long wxGetCurrentTime()
125 {
126 return wxGetCurrentMTime() / 1000;
127 }
128
129 // ----------------------------------------------------------------------------
130 // the functions to get the current time and timezone info
131 // ----------------------------------------------------------------------------
132
133 // return GMT time in millisecond
134 long wxGetCurrentMTime()
135 {
136 #if defined(HAVE_LOCALTIME)
137 time_t t0 = time(&t0);
138 if ( t0 != (time_t)-1 )
139 {
140 struct tm *tp = localtime(&t0);
141
142 if ( tp )
143 {
144 return 1000*(60*(60*tp->tm_hour+tp->tm_min)+tp->tm_sec);
145 }
146 }
147 #elif defined(HAVE_GETTIMEOFDAY)
148 struct timeval tp;
149 if ( wxGetTimeOfDay(&tp, (struct timezone *)NULL) != -1 )
150 {
151 return (1000*tp.tv_sec + tp.tv_usec / 1000);
152 }
153 #elif defined(HAVE_FTIME)
154 struct timeb tp;
155 if ( ftime(&tp) == 0 )
156 {
157 return (1000*tp.time + tp.millitm);
158 }
159 #else
160 #error "no function to find the current time on this system"
161 #endif
162
163 wxLogSysError(_("Failed to get the system time"));
164
165 return -1;
166 }
167
168 bool wxGetLocalTime(long *timeZone, int *dstObserved)
169 {
170 #if defined(HAVE_LOCALTIME) && defined(WX_GMTOFF_IN_TM)
171 time_t t0 = time(&t0);
172 if ( t0 != (time_t)-1 )
173 {
174 struct tm *tm = localtime(&t0);
175
176 if ( tm )
177 {
178 *timeZone = tm->tm_gmtoff;
179 *dstObserved = tm->tm_isdst;
180
181 return TRUE;
182 }
183 }
184 #elif defined(HAVE_GETTIMEOFDAY) && !defined(WX_GETTIMEOFDAY_NO_TZ)
185 struct timeval tp;
186 struct timezone tz;
187 if ( gettimeofday(&tp, &tz) != -1 )
188 {
189 *timeZone = 60*tz.tz_minuteswest;
190 *dstObserved = tz.tz_dsttime;
191
192 return TRUE;
193 }
194 #elif defined(HAVE_FTIME)
195 struct timeb tb;
196 if ( ftime(&tb) == 0 )
197 {
198 *timeZone = 60*tb.timezone;
199 *dstObserved = tb.dstflag;
200 }
201 #else
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)
204
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;
214 #else
215 wxFAIL_MSG(_T("wxGetLocalTime() not implemented"));
216 #endif // compiler
217 #endif
218
219 return FALSE;
220 }