]> git.saurik.com Git - wxWidgets.git/blob - src/common/timercmn.cpp
Several changes and updates
[wxWidgets.git] / src / common / timercmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: timercmn.cpp
3 // Purpose: Common timer implementation
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 //#pragma implementation "timercmn.h"
14 #pragma implementation
15 #endif
16
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WX_PRECOMP
25 #include "wx/defs.h"
26 #include "wx/list.h"
27 #endif
28
29 #include "wx/timer.h"
30
31 #if defined(__SVR4__) && !defined(__SYSV__)
32 #define __SYSV__
33 #endif
34
35 #include <time.h>
36
37 #ifndef __WXMAC__
38 #include <sys/types.h>
39 #endif
40
41 #if (!defined(__SC__) && !defined(__SGI__) && !defined(__GNUWIN32__) && !defined(__MWERKS__)) || defined(__MINGW32__)
42 #include <sys/timeb.h>
43 #endif
44
45 #if defined(__linux__) || defined(__SVR4__) || defined(__SYSV__) || defined(__SGI__) || \
46 defined(__ALPHA__) || defined(__GNUWIN32__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
47 defined(__SALFORDC__) || defined(__EMX__)
48 #include <sys/time.h>
49 #endif
50
51 #ifdef __MINGW32__
52 #include "windows.h"
53 #endif
54
55 #if defined(__SUN__) || defined(__OSF__) || defined(__FreeBSD__)
56 // At least on Sun, ftime is undeclared.
57 // Need to be verified on other platforms.
58 extern "C" int ftime(struct timeb *tp);
59 //extern "C" int gettimeofday(struct timeval *tp, void *);
60 // extern "C" time_t time(time_t);
61 // #include <sys/timeb.h>
62 #if defined(__SVR4__) && !defined(__ALPHA__)
63 // ditto for gettimeofday on Solaris 2.x.
64 extern "C" int gettimeofday(struct timeval *tp, void *);
65 #endif
66 #endif
67
68 /*
69 * Timer functions
70 *
71 */
72
73 long wxStartTime = 0;
74 void wxStartTimer(void)
75 {
76 wxStartTime=wxGetCurrentMTime();
77 }
78
79 // Returns elapsed time in milliseconds
80 long wxGetElapsedTime(bool resetTimer)
81 {
82 long oldTime = wxStartTime;
83 long newTime=wxGetCurrentMTime();
84
85 if (resetTimer) wxStartTime = newTime;
86 return newTime - oldTime;
87 }
88
89
90 // Get number of seconds since 00:00:00 GMT, Jan 1st 1970.
91 long wxGetCurrentTime(void)
92 {
93 return wxGetCurrentMTime()/1000;
94 }
95
96 // return GMT time in millisecond
97 long wxGetCurrentMTime()
98 {
99 #if defined(__xlC__) || defined(__AIX__) || defined(__SVR4__) || defined(__SYSV__) || \
100 (defined(__GNUWIN32__) && !defined(__MINGW32__)) // || defined(__AIXV3__)
101 struct timeval tp;
102 #if defined(__SYSV__) || (defined (__GNUWIN32__) && !defined (__MINGW32__))
103 gettimeofday(&tp, (struct timezone *)NULL);
104 #else
105 gettimeofday(&tp);
106 #endif
107 return (1000*tp.tv_sec + tp.tv_usec / 1000);
108 #elif (defined(__SC__) || defined(__SGI__) || defined(___BSDI__) || defined(__ALPHA__) || \
109 defined(__MINGW32__)|| defined(__MWERKS__) || defined(__FreeBSD__))
110 time_t t0;
111 struct tm *tp;
112 time(&t0);
113 tp = localtime(&t0);
114 return 1000*(60*(60*tp->tm_hour+tp->tm_min)+tp->tm_sec);
115 #else
116 struct timeb tp;
117 ftime(&tp);
118 return (1000*tp.time + tp.millitm);
119 #endif
120 }
121
122 //---------------
123 // wxChrono class
124 // This class encapsulates the above fonctions,
125 // such that several wxChrono can be created
126 // simultaneously
127
128 wxChrono::wxChrono()
129 {
130 Start();
131 }
132
133 void wxChrono::Start(long t)
134 {
135 m_t0=wxGetCurrentMTime()-t;
136 m_pause=0;
137 }
138
139 void wxChrono::Pause()
140 {
141 m_pause=wxGetCurrentMTime()-m_t0;
142 }
143
144 void wxChrono::Resume()
145 {
146 m_t0=wxGetCurrentMTime()-m_pause;
147 m_pause=0;
148 }
149
150 long wxChrono::Time()
151 {
152 if (m_pause) return m_pause;
153 return wxGetCurrentMTime()-m_t0;
154 }
155
156
157
158 // EXPERIMENTAL: comment this out if it doesn't compile.
159 #ifndef __VMS__
160 bool wxGetLocalTime(long *timeZone, int *dstObserved)
161 {
162 #if defined(__MINGW32__)
163 time_t t0;
164 struct tm *tp;
165 time(&t0);
166 tp = localtime(&t0);
167 # if __GNUC__ == 2 && __GNUC_MINOR__ <= 8
168 // gcc 2.8.x or earlier
169 timeb tz;
170 ftime(& tz);
171 *timeZone = tz._timezone;
172 # else
173 // egcs or gcc 2.95
174 *timeZone = _timezone; // tp->tm_gmtoff; // ???
175 # endif
176 *dstObserved = tp->tm_isdst;
177 #else
178 // not mingw32...
179 #if (((defined(__SYSV__) && !defined(__HPUX__)) || defined(__MSDOS__) || defined(__WXMSW__) || defined(__WXPM__)) \
180 && !defined(__GNUWIN32__) && !defined(__MWERKS__) )
181 # if defined(__BORLANDC__)
182 /* Borland uses underscores */
183 *timeZone = _timezone;
184 *dstObserved = _daylight;
185 # elif defined(__SALFORDC__)
186 *timeZone = _timezone;
187 *dstObserved = daylight;
188 # elif defined(__VISAGECPP__)
189 *timeZone = _timezone;
190 *dstObserved = daylight;
191 # else
192 *timeZone = timezone;
193 *dstObserved = daylight;
194 # endif
195 #elif defined(__xlC__) || defined(__AIX__) || defined(__SVR4__) || defined(__SYSV__) || defined(__MWERKS__) || (defined(__GNUWIN32__) && !defined(__MINGW32__)) // || defined(__AIXV3__)
196 # ifndef __MWERKS__ // shouldn't this be one scope below ?
197 struct timeval tp;
198 # endif
199 # if defined(__SYSV__) || (defined(__GNUWIN32__) && !defined(__MINGW32))
200 struct timezone tz;
201 gettimeofday(&tp, &tz);
202 *timeZone = 60*(tz.tz_minuteswest);
203 *dstObserved = tz.tz_dsttime;
204 # else
205 time_t t0;
206 struct tm *tp;
207 time(&t0);
208 tp = localtime(&t0);
209 # ifndef __MWERKS__
210 *timeZone = tp->tm_gmtoff; // ???
211 # else
212 *timeZone = 0 ;
213 # endif
214 *dstObserved = tp->tm_isdst;
215 #endif
216 #elif defined(__WXSTUBS__)
217 return FALSE;
218 #else
219 // #error wxGetLocalTime not implemented.
220 struct timeval tp;
221 struct timezone tz;
222 gettimeofday(&tp, &tz);
223 *timeZone = 60*(tz.tz_minuteswest);
224 *dstObserved = tz.tz_dsttime;
225 #endif
226 #endif
227 // __MINGW32__
228 return TRUE;
229 }
230 #endif