]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
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 | ||
227b5cd7 | 31 | #if defined(__SVR4__) && !defined(__SYSV__) |
c801d85f KB |
32 | #define __SYSV__ |
33 | #endif | |
34 | ||
35 | #include <time.h> | |
ce3ed50d | 36 | |
17dff81c | 37 | #ifndef __WXMAC__ |
c801d85f | 38 | #include <sys/types.h> |
17dff81c | 39 | #endif |
c801d85f | 40 | |
469e1e5c | 41 | #if (!defined(__SC__) && !defined(__SGI__) && !defined(__GNUWIN32__) && !defined(__MWERKS__)) || defined(__MINGW32__) |
c801d85f KB |
42 | #include <sys/timeb.h> |
43 | #endif | |
44 | ||
edaa81ae | 45 | #if defined(__linux__) || defined(__SVR4__) || defined(__SYSV__) || defined(__SGI__) || \ |
ce3ed50d | 46 | defined(__ALPHA__) || defined(__GNUWIN32__) || defined(__FreeBSD__) || defined(__NetBSD__) || \ |
91b8de8d | 47 | defined(__SALFORDC__) || defined(__EMX__) |
c801d85f KB |
48 | #include <sys/time.h> |
49 | #endif | |
50 | ||
51 | #ifdef __MINGW32__ | |
52 | #include "windows.h" | |
53 | #endif | |
54 | ||
54f04bc0 | 55 | #if defined(__SUN__) || defined(__OSF__) || defined(__FreeBSD__) |
c801d85f KB |
56 | // At least on Sun, ftime is undeclared. |
57 | // Need to be verified on other platforms. | |
58 | extern "C" int ftime(struct timeb *tp); | |
54f04bc0 | 59 | //extern "C" int gettimeofday(struct timeval *tp, void *); |
c801d85f KB |
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 | { | |
91b8de8d RR |
76 | #if defined(__EMX__) || defined(__xlC__) || defined(__AIX__) || defined(__SVR4__) || defined(__SYSV__) || \ |
77 | (defined(__GNUWIN32__) && !defined(__MINGW32__)) // || defined(__AIXV3__) | |
c801d85f | 78 | struct timeval tp; |
91b8de8d | 79 | #if defined(__EMX__) || defined(__SYSV__) || (defined (__GNUWIN32__) && !defined (__MINGW32__)) |
c801d85f KB |
80 | gettimeofday(&tp, (struct timezone *)NULL); |
81 | #else | |
82 | gettimeofday(&tp); | |
83 | #endif | |
84 | wxStartTime = 1000*tp.tv_sec + tp.tv_usec/1000; | |
91b8de8d | 85 | #elif (defined(__SC__) || defined(__SGI__) || defined(___BSDI__) || defined(__ALPHA__) || \ |
717b9bf2 | 86 | defined(__MINGW32__) || defined(__MWERKS__) || defined(__FreeBSD__) ) |
c801d85f KB |
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 | { | |
91b8de8d RR |
102 | #if defined(__xlC__) || defined(__AIX__) || defined(__SVR4__) || defined(__SYSV__) || \ |
103 | (defined(__GNUWIN32__) && !defined(__MINGW32__)) // || defined(__AIXV3__) | |
c801d85f | 104 | struct timeval tp; |
f57fe24c | 105 | #if defined(__SYSV__) || (defined (__GNUWIN32__) && !defined (__MINGW32__)) |
c801d85f KB |
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; | |
91b8de8d RR |
114 | #elif (defined(__SC__) || defined(__SGI__) || defined(___BSDI__) || defined(__ALPHA__) || \ |
115 | defined(__MINGW32__)|| defined(__MWERKS__) || defined(__FreeBSD__)) | |
c801d85f KB |
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 | { | |
717b9bf2 | 139 | #if defined(__MINGW32__) |
c801d85f KB |
140 | time_t t0; |
141 | struct tm *tp; | |
142 | time(&t0); | |
143 | tp = localtime(&t0); | |
45e726de HH |
144 | # if __GNUC__ == 2 && __GNUC_MINOR__ <= 8 |
145 | // gcc 2.8.x or earlier | |
c801d85f KB |
146 | timeb tz; |
147 | ftime(& tz); | |
148 | *timeZone = tz._timezone; | |
45e726de HH |
149 | # else |
150 | // egcs or gcc 2.95 | |
151 | *timeZone = _timezone; // tp->tm_gmtoff; // ??? | |
152 | # endif | |
c801d85f KB |
153 | *dstObserved = tp->tm_isdst; |
154 | #else | |
45e726de | 155 | // not mingw32... |
717b9bf2 | 156 | #if (((defined(__SYSV__) && !defined(__HPUX__)) || defined(__MSDOS__) || defined(__WXMSW__) || defined(__WXPM__)) \ |
ce3ed50d | 157 | && !defined(__GNUWIN32__) && !defined(__MWERKS__) ) |
717b9bf2 | 158 | # if defined(__BORLANDC__) |
c801d85f KB |
159 | /* Borland uses underscores */ |
160 | *timeZone = _timezone; | |
161 | *dstObserved = _daylight; | |
717b9bf2 | 162 | # elif defined(__SALFORDC__) |
ce3ed50d JS |
163 | *timeZone = _timezone; |
164 | *dstObserved = daylight; | |
717b9bf2 DW |
165 | # elif defined(__VISAGECPP__) |
166 | *timeZone = _timezone; | |
167 | *dstObserved = daylight; | |
168 | # else | |
c801d85f KB |
169 | *timeZone = timezone; |
170 | *dstObserved = daylight; | |
717b9bf2 | 171 | # endif |
469e1e5c | 172 | #elif defined(__xlC__) || defined(__AIX__) || defined(__SVR4__) || defined(__SYSV__) || defined(__MWERKS__) || (defined(__GNUWIN32__) && !defined(__MINGW32__)) // || defined(__AIXV3__) |
717b9bf2 | 173 | # ifndef __MWERKS__ // shouldn't this be one scope below ? |
c801d85f | 174 | struct timeval tp; |
717b9bf2 DW |
175 | # endif |
176 | # if defined(__SYSV__) || (defined(__GNUWIN32__) && !defined(__MINGW32)) | |
c801d85f KB |
177 | struct timezone tz; |
178 | gettimeofday(&tp, &tz); | |
179 | *timeZone = 60*(tz.tz_minuteswest); | |
180 | *dstObserved = tz.tz_dsttime; | |
717b9bf2 | 181 | # else |
c801d85f KB |
182 | time_t t0; |
183 | struct tm *tp; | |
184 | time(&t0); | |
185 | tp = localtime(&t0); | |
717b9bf2 | 186 | # ifndef __MWERKS__ |
c801d85f | 187 | *timeZone = tp->tm_gmtoff; // ??? |
717b9bf2 | 188 | # else |
469e1e5c | 189 | *timeZone = 0 ; |
717b9bf2 | 190 | # endif |
c801d85f KB |
191 | *dstObserved = tp->tm_isdst; |
192 | #endif | |
34138703 JS |
193 | #elif defined(__WXSTUBS__) |
194 | return FALSE; | |
c801d85f KB |
195 | #else |
196 | // #error wxGetLocalTime not implemented. | |
197 | struct timeval tp; | |
198 | struct timezone tz; | |
199 | gettimeofday(&tp, &tz); | |
200 | *timeZone = 60*(tz.tz_minuteswest); | |
201 | *dstObserved = tz.tz_dsttime; | |
202 | #endif | |
203 | #endif | |
204 | // __MINGW32__ | |
205 | return TRUE; | |
206 | } | |
207 | #endif | |
208 | ||
209 | // Get number of seconds since 00:00:00 GMT, Jan 1st 1970. | |
210 | long wxGetCurrentTime(void) | |
211 | { | |
212 | #if defined(__xlC__) || defined(__AIX__) || defined(__SVR4__) || defined(__SYSV__) // || defined(__AIXV3__) | |
213 | struct timeval tp; | |
54f04bc0 | 214 | #if defined(__SYSV__) || (defined (__GNUWIN32__) && !defined (__MINGW32__) || defined(__FreeBSD__)) |
c801d85f KB |
215 | gettimeofday(&tp, (struct timezone *)NULL); |
216 | #else | |
217 | gettimeofday(&tp); | |
218 | #endif | |
219 | return tp.tv_sec; | |
220 | #else // (defined(__SC__) || defined(__SGI__) || defined(___BSDI__) || defined(__ALPHA__)) | |
221 | return time(0); | |
222 | #endif | |
223 | /* | |
224 | #else | |
225 | struct timeb tp; | |
226 | ftime(&tp); | |
227 | return tp.time; | |
228 | #endif | |
229 | */ | |
230 | } | |
231 |