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