]>
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 | { | |
4f3ac409 | 76 | wxStartTime=wxGetCurrentMTime(); |
c801d85f KB |
77 | } |
78 | ||
79 | // Returns elapsed time in milliseconds | |
80 | long wxGetElapsedTime(bool resetTimer) | |
f0599ea9 SB |
81 | { |
82 | long oldTime = wxStartTime; | |
4f3ac409 | 83 | long newTime=wxGetCurrentMTime(); |
f0599ea9 SB |
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 | { | |
4f3ac409 | 93 | return wxGetCurrentMTime()/1000; |
f0599ea9 SB |
94 | } |
95 | ||
96 | // return GMT time in millisecond | |
4f3ac409 | 97 | long wxGetCurrentMTime() |
c801d85f | 98 | { |
91b8de8d RR |
99 | #if defined(__xlC__) || defined(__AIX__) || defined(__SVR4__) || defined(__SYSV__) || \ |
100 | (defined(__GNUWIN32__) && !defined(__MINGW32__)) // || defined(__AIXV3__) | |
c801d85f | 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 | |
f0599ea9 | 107 | return (1000*tp.tv_sec + tp.tv_usec / 1000); |
91b8de8d RR |
108 | #elif (defined(__SC__) || defined(__SGI__) || defined(___BSDI__) || defined(__ALPHA__) || \ |
109 | defined(__MINGW32__)|| defined(__MWERKS__) || defined(__FreeBSD__)) | |
c801d85f KB |
110 | time_t t0; |
111 | struct tm *tp; | |
112 | time(&t0); | |
113 | tp = localtime(&t0); | |
f0599ea9 | 114 | return 1000*(60*(60*tp->tm_hour+tp->tm_min)+tp->tm_sec); |
c801d85f KB |
115 | #else |
116 | struct timeb tp; | |
117 | ftime(&tp); | |
f0599ea9 | 118 | return (1000*tp.time + tp.millitm); |
c801d85f | 119 | #endif |
c801d85f KB |
120 | } |
121 | ||
f0599ea9 SB |
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 | { | |
4f3ac409 | 135 | m_t0=wxGetCurrentMTime()-t; |
f0599ea9 SB |
136 | m_pause=0; |
137 | } | |
138 | ||
139 | void wxChrono::Pause() | |
140 | { | |
4f3ac409 | 141 | m_pause=wxGetCurrentMTime()-m_t0; |
f0599ea9 SB |
142 | } |
143 | ||
144 | void wxChrono::Resume() | |
145 | { | |
4f3ac409 | 146 | m_t0=wxGetCurrentMTime()-m_pause; |
f0599ea9 SB |
147 | m_pause=0; |
148 | } | |
149 | ||
150 | long wxChrono::Time() | |
151 | { | |
152 | if (m_pause) return m_pause; | |
4f3ac409 | 153 | return wxGetCurrentMTime()-m_t0; |
f0599ea9 SB |
154 | } |
155 | ||
156 | ||
157 | ||
c801d85f | 158 | // EXPERIMENTAL: comment this out if it doesn't compile. |
5230801e | 159 | #if !defined( __VMS__ ) || ( __VMS_VER >= 70000000 ) |
c801d85f KB |
160 | bool wxGetLocalTime(long *timeZone, int *dstObserved) |
161 | { | |
717b9bf2 | 162 | #if defined(__MINGW32__) |
c801d85f KB |
163 | time_t t0; |
164 | struct tm *tp; | |
165 | time(&t0); | |
166 | tp = localtime(&t0); | |
45e726de HH |
167 | # if __GNUC__ == 2 && __GNUC_MINOR__ <= 8 |
168 | // gcc 2.8.x or earlier | |
c801d85f KB |
169 | timeb tz; |
170 | ftime(& tz); | |
171 | *timeZone = tz._timezone; | |
45e726de HH |
172 | # else |
173 | // egcs or gcc 2.95 | |
174 | *timeZone = _timezone; // tp->tm_gmtoff; // ??? | |
175 | # endif | |
c801d85f KB |
176 | *dstObserved = tp->tm_isdst; |
177 | #else | |
45e726de | 178 | // not mingw32... |
717b9bf2 | 179 | #if (((defined(__SYSV__) && !defined(__HPUX__)) || defined(__MSDOS__) || defined(__WXMSW__) || defined(__WXPM__)) \ |
ce3ed50d | 180 | && !defined(__GNUWIN32__) && !defined(__MWERKS__) ) |
717b9bf2 | 181 | # if defined(__BORLANDC__) |
c801d85f KB |
182 | /* Borland uses underscores */ |
183 | *timeZone = _timezone; | |
184 | *dstObserved = _daylight; | |
717b9bf2 | 185 | # elif defined(__SALFORDC__) |
ce3ed50d JS |
186 | *timeZone = _timezone; |
187 | *dstObserved = daylight; | |
717b9bf2 DW |
188 | # elif defined(__VISAGECPP__) |
189 | *timeZone = _timezone; | |
190 | *dstObserved = daylight; | |
191 | # else | |
c801d85f KB |
192 | *timeZone = timezone; |
193 | *dstObserved = daylight; | |
717b9bf2 | 194 | # endif |
5230801e JJ |
195 | #elif defined(__xlC__) || defined(__AIX__) || defined(__SVR4__) || \ |
196 | defined(__SYSV__) || defined(__MWERKS__) || (defined(__GNUWIN32__) && \ | |
197 | !defined(__MINGW32__))\ | |
198 | || defined( __VMS__ ) // || defined(__AIXV3__) | |
199 | # if defined(__SYSV__) || (defined(__GNUWIN32__) && !defined(__MINGW32)) | |
200 | # ifndef __MWERKS__ | |
c801d85f | 201 | struct timeval tp; |
717b9bf2 | 202 | # endif |
c801d85f KB |
203 | struct timezone tz; |
204 | gettimeofday(&tp, &tz); | |
205 | *timeZone = 60*(tz.tz_minuteswest); | |
206 | *dstObserved = tz.tz_dsttime; | |
717b9bf2 | 207 | # else |
c801d85f KB |
208 | time_t t0; |
209 | struct tm *tp; | |
210 | time(&t0); | |
211 | tp = localtime(&t0); | |
717b9bf2 | 212 | # ifndef __MWERKS__ |
c801d85f | 213 | *timeZone = tp->tm_gmtoff; // ??? |
717b9bf2 | 214 | # else |
469e1e5c | 215 | *timeZone = 0 ; |
717b9bf2 | 216 | # endif |
c801d85f KB |
217 | *dstObserved = tp->tm_isdst; |
218 | #endif | |
34138703 JS |
219 | #elif defined(__WXSTUBS__) |
220 | return FALSE; | |
c801d85f KB |
221 | #else |
222 | // #error wxGetLocalTime not implemented. | |
223 | struct timeval tp; | |
224 | struct timezone tz; | |
225 | gettimeofday(&tp, &tz); | |
226 | *timeZone = 60*(tz.tz_minuteswest); | |
227 | *dstObserved = tz.tz_dsttime; | |
228 | #endif | |
229 | #endif | |
230 | // __MINGW32__ | |
231 | return TRUE; | |
232 | } | |
233 | #endif |