]> git.saurik.com Git - wxWidgets.git/blob - src/common/timercmn.cpp
1. regenerated makefiles
[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 #if defined(__EMX__) || defined(__xlC__) || defined(__AIX__) || defined(__SVR4__) || defined(__SYSV__) || \
77 (defined(__GNUWIN32__) && !defined(__MINGW32__)) // || defined(__AIXV3__)
78 struct timeval tp;
79 #if defined(__EMX__) || defined(__SYSV__) || (defined (__GNUWIN32__) && !defined (__MINGW32__))
80 gettimeofday(&tp, (struct timezone *)NULL);
81 #else
82 gettimeofday(&tp);
83 #endif
84 wxStartTime = 1000*tp.tv_sec + tp.tv_usec/1000;
85 #elif (defined(__SC__) || defined(__SGI__) || defined(___BSDI__) || defined(__ALPHA__) || \
86 defined(__MINGW32__) || defined(__MWERKS__) || defined(__FreeBSD__) )
87 time_t t0;
88 struct tm *tp;
89 time(&t0);
90 tp = localtime(&t0);
91 wxStartTime = 1000*(60*(60*tp->tm_hour+tp->tm_min)+tp->tm_sec);
92 #else
93 struct timeb tp;
94 ftime(&tp);
95 wxStartTime = 1000*tp.time + tp.millitm;
96 #endif
97 }
98
99 // Returns elapsed time in milliseconds
100 long wxGetElapsedTime(bool resetTimer)
101 {
102 #if defined(__xlC__) || defined(__AIX__) || defined(__SVR4__) || defined(__SYSV__) || \
103 (defined(__GNUWIN32__) && !defined(__MINGW32__)) // || defined(__AIXV3__)
104 struct timeval tp;
105 #if defined(__SYSV__) || (defined (__GNUWIN32__) && !defined (__MINGW32__))
106 gettimeofday(&tp, (struct timezone *)NULL);
107 #else
108 gettimeofday(&tp);
109 #endif
110 long oldTime = wxStartTime;
111 long newTime = 1000*tp.tv_sec + tp.tv_usec / 1000;
112 if (resetTimer)
113 wxStartTime = newTime;
114 #elif (defined(__SC__) || defined(__SGI__) || defined(___BSDI__) || defined(__ALPHA__) || \
115 defined(__MINGW32__)|| defined(__MWERKS__) || defined(__FreeBSD__))
116 time_t t0;
117 struct tm *tp;
118 time(&t0);
119 tp = localtime(&t0);
120 long oldTime = wxStartTime;
121 long newTime = 1000*(60*(60*tp->tm_hour+tp->tm_min)+tp->tm_sec);
122 if (resetTimer)
123 wxStartTime = newTime;
124 #else
125 struct timeb tp;
126 ftime(&tp);
127 long oldTime = wxStartTime;
128 long newTime = 1000*tp.time + tp.millitm;
129 if (resetTimer)
130 wxStartTime = newTime;
131 #endif
132 return newTime - oldTime;
133 }
134
135 // EXPERIMENTAL: comment this out if it doesn't compile.
136 #ifndef __VMS__
137 bool wxGetLocalTime(long *timeZone, int *dstObserved)
138 {
139 #if defined(__MINGW32__)
140 time_t t0;
141 struct tm *tp;
142 time(&t0);
143 tp = localtime(&t0);
144 *timeZone = _timezone; // tp->tm_gmtoff; // ???
145 *dstObserved = tp->tm_isdst;
146 #elif 0
147 /* HH: This code apparently was needed by very old Mingw-gcc versions
148 * Modern mingw's don't need it. Since old gcc isn't supported anyway,
149 * I think this stuff can go */
150 time_t t0;
151 struct tm *tp;
152 time(&t0);
153 tp = localtime(&t0);
154 timeb tz;
155 ftime(& tz);
156 *timeZone = tz._timezone;
157 *dstObserved = tp->tm_isdst;
158 #else
159
160 #if (((defined(__SYSV__) && !defined(__HPUX__)) || defined(__MSDOS__) || defined(__WXMSW__) || defined(__WXPM__)) \
161 && !defined(__GNUWIN32__) && !defined(__MWERKS__) )
162 # if defined(__BORLANDC__)
163 /* Borland uses underscores */
164 *timeZone = _timezone;
165 *dstObserved = _daylight;
166 # elif defined(__SALFORDC__)
167 *timeZone = _timezone;
168 *dstObserved = daylight;
169 # elif defined(__VISAGECPP__)
170 *timeZone = _timezone;
171 *dstObserved = daylight;
172 # else
173 *timeZone = timezone;
174 *dstObserved = daylight;
175 # endif
176 #elif defined(__xlC__) || defined(__AIX__) || defined(__SVR4__) || defined(__SYSV__) || defined(__MWERKS__) || (defined(__GNUWIN32__) && !defined(__MINGW32__)) // || defined(__AIXV3__)
177 # ifndef __MWERKS__ // shouldn't this be one scope below ?
178 struct timeval tp;
179 # endif
180 # if defined(__SYSV__) || (defined(__GNUWIN32__) && !defined(__MINGW32))
181 struct timezone tz;
182 gettimeofday(&tp, &tz);
183 *timeZone = 60*(tz.tz_minuteswest);
184 *dstObserved = tz.tz_dsttime;
185 # else
186 time_t t0;
187 struct tm *tp;
188 time(&t0);
189 tp = localtime(&t0);
190 # ifndef __MWERKS__
191 *timeZone = tp->tm_gmtoff; // ???
192 # else
193 *timeZone = 0 ;
194 # endif
195 *dstObserved = tp->tm_isdst;
196 #endif
197 #elif defined(__WXSTUBS__)
198 return FALSE;
199 #else
200 // #error wxGetLocalTime not implemented.
201 struct timeval tp;
202 struct timezone tz;
203 gettimeofday(&tp, &tz);
204 *timeZone = 60*(tz.tz_minuteswest);
205 *dstObserved = tz.tz_dsttime;
206 #endif
207 #endif
208 // __MINGW32__
209 return TRUE;
210 }
211 #endif
212
213 // Get number of seconds since 00:00:00 GMT, Jan 1st 1970.
214 long wxGetCurrentTime(void)
215 {
216 #if defined(__xlC__) || defined(__AIX__) || defined(__SVR4__) || defined(__SYSV__) // || defined(__AIXV3__)
217 struct timeval tp;
218 #if defined(__SYSV__) || (defined (__GNUWIN32__) && !defined (__MINGW32__) || defined(__FreeBSD__))
219 gettimeofday(&tp, (struct timezone *)NULL);
220 #else
221 gettimeofday(&tp);
222 #endif
223 return tp.tv_sec;
224 #else // (defined(__SC__) || defined(__SGI__) || defined(___BSDI__) || defined(__ALPHA__))
225 return time(0);
226 #endif
227 /*
228 #else
229 struct timeb tp;
230 ftime(&tp);
231 return tp.time;
232 #endif
233 */
234 }
235