mac support as stubs added
[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 #ifdef __SVR4__
32 #define __SYSV__
33 #endif
34
35 #include <time.h>
36 #ifndef __WXMAC__
37 #include <sys/types.h>
38 #endif
39
40 #if (!defined(__SC__) && !defined(__SGI__) && !defined(__GNUWIN32__) && !defined(__MWERKS__)) || defined(__MINGW32__)
41 #include <sys/timeb.h>
42 #endif
43
44 #if defined(__linux__) || defined(__SVR4__) || defined(__SYSV__) || defined(__SGI__) || \
45 defined(__ALPHA__) || defined(__GNUWIN32__) || defined(__FreeBSD__) || defined(__NetBSD__)
46 #include <sys/time.h>
47 #endif
48
49 #ifdef __MINGW32__
50 #include "windows.h"
51 #endif
52
53 #if defined(__SUN__) || defined(__OSF__)
54 // At least on Sun, ftime is undeclared.
55 // Need to be verified on other platforms.
56 extern "C" int ftime(struct timeb *tp);
57 // extern "C" time_t time(time_t);
58 // #include <sys/timeb.h>
59 #if defined(__SVR4__) && !defined(__ALPHA__)
60 // ditto for gettimeofday on Solaris 2.x.
61 extern "C" int gettimeofday(struct timeval *tp, void *);
62 #endif
63 #endif
64
65 /*
66 * Timer functions
67 *
68 */
69
70 long wxStartTime = 0;
71 void wxStartTimer(void)
72 {
73 #if defined(__xlC__) || defined(__AIX__) || defined(__SVR4__) || defined(__SYSV__) || (defined(__GNUWIN32__) && !defined(__MINGW32__)) // || defined(__AIXV3__)
74 struct timeval tp;
75 #if defined(__SYSV__) || (defined (__GNUWIN32__) && !defined (__MINGW32__))
76 gettimeofday(&tp, (struct timezone *)NULL);
77 #else
78 gettimeofday(&tp);
79 #endif
80 wxStartTime = 1000*tp.tv_sec + tp.tv_usec/1000;
81 #elif (defined(__SC__) || defined(__SGI__) || defined(___BSDI__) || defined(__ALPHA__) || defined(__MINGW32__) || defined(__MWERKS__))
82 time_t t0;
83 struct tm *tp;
84 time(&t0);
85 tp = localtime(&t0);
86 wxStartTime = 1000*(60*(60*tp->tm_hour+tp->tm_min)+tp->tm_sec);
87 #else
88 struct timeb tp;
89 ftime(&tp);
90 wxStartTime = 1000*tp.time + tp.millitm;
91 #endif
92 }
93
94 // Returns elapsed time in milliseconds
95 long wxGetElapsedTime(bool resetTimer)
96 {
97 #if defined(__xlC__) || defined(__AIX__) || defined(__SVR4__) || defined(__SYSV__) || (defined(__GNUWIN32__) && !defined(__MINGW32__)) // || defined(__AIXV3__)
98 struct timeval tp;
99 #if defined(__SYSV__) || (defined (__GNUWIN32__) && !defined (__MINGW32__))
100 gettimeofday(&tp, (struct timezone *)NULL);
101 #else
102 gettimeofday(&tp);
103 #endif
104 long oldTime = wxStartTime;
105 long newTime = 1000*tp.tv_sec + tp.tv_usec / 1000;
106 if (resetTimer)
107 wxStartTime = newTime;
108 #elif (defined(__SC__) || defined(__SGI__) || defined(___BSDI__) || defined(__ALPHA__) || defined(__MINGW32__)|| defined(__MWERKS__))
109 time_t t0;
110 struct tm *tp;
111 time(&t0);
112 tp = localtime(&t0);
113 long oldTime = wxStartTime;
114 long newTime = 1000*(60*(60*tp->tm_hour+tp->tm_min)+tp->tm_sec);
115 if (resetTimer)
116 wxStartTime = newTime;
117 #else
118 struct timeb tp;
119 ftime(&tp);
120 long oldTime = wxStartTime;
121 long newTime = 1000*tp.time + tp.millitm;
122 if (resetTimer)
123 wxStartTime = newTime;
124 #endif
125 return newTime - oldTime;
126 }
127
128 // EXPERIMENTAL: comment this out if it doesn't compile.
129 #ifndef __VMS__
130 bool wxGetLocalTime(long *timeZone, int *dstObserved)
131 {
132 #if defined(__MINGW32__) && defined(__EGCS__)
133 time_t t0;
134 struct tm *tp;
135 time(&t0);
136 tp = localtime(&t0);
137 *timeZone = timezone; // tp->tm_gmtoff; // ???
138 *dstObserved = tp->tm_isdst;
139 #elif defined(__MINGW32__)
140 time_t t0;
141 struct tm *tp;
142 time(&t0);
143 tp = localtime(&t0);
144 timeb tz;
145 ftime(& tz);
146 *timeZone = tz._timezone;
147 *dstObserved = tp->tm_isdst;
148 #else
149
150 #if (((defined(__SYSV__) && !defined(__HPUX__)) || defined(__MSDOS__) || defined(__WXMSW__)) && !defined(__GNUWIN32__) && !defined(__MWERKS__))
151 #ifdef __BORLANDC__
152 /* Borland uses underscores */
153 *timeZone = _timezone;
154 *dstObserved = _daylight;
155 #else
156 *timeZone = timezone;
157 *dstObserved = daylight;
158 #endif
159 #elif defined(__xlC__) || defined(__AIX__) || defined(__SVR4__) || defined(__SYSV__) || defined(__MWERKS__) || (defined(__GNUWIN32__) && !defined(__MINGW32__)) // || defined(__AIXV3__)
160 #ifndef __MWERKS__ // shouldn't this be one scope below ?
161 struct timeval tp;
162 #endif
163 #if defined(__SYSV__) || (defined(__GNUWIN32__) && !defined(__MINGW32))
164 struct timezone tz;
165 gettimeofday(&tp, &tz);
166 *timeZone = 60*(tz.tz_minuteswest);
167 *dstObserved = tz.tz_dsttime;
168 #else
169 time_t t0;
170 struct tm *tp;
171 time(&t0);
172 tp = localtime(&t0);
173 #ifndef __MWERKS__
174 *timeZone = tp->tm_gmtoff; // ???
175 #else
176 *timeZone = 0 ;
177 #endif
178 *dstObserved = tp->tm_isdst;
179 #endif
180 #elif defined(__WXSTUBS__)
181 return FALSE;
182 #else
183 // #error wxGetLocalTime not implemented.
184 struct timeval tp;
185 struct timezone tz;
186 gettimeofday(&tp, &tz);
187 *timeZone = 60*(tz.tz_minuteswest);
188 *dstObserved = tz.tz_dsttime;
189 #endif
190 #endif
191 // __MINGW32__
192 return TRUE;
193 }
194 #endif
195
196 // Get number of seconds since 00:00:00 GMT, Jan 1st 1970.
197 long wxGetCurrentTime(void)
198 {
199 #if defined(__xlC__) || defined(__AIX__) || defined(__SVR4__) || defined(__SYSV__) // || defined(__AIXV3__)
200 struct timeval tp;
201 #if defined(__SYSV__) || (defined (__GNUWIN32__) && !defined (__MINGW32__))
202 gettimeofday(&tp, (struct timezone *)NULL);
203 #else
204 gettimeofday(&tp);
205 #endif
206 return tp.tv_sec;
207 #else // (defined(__SC__) || defined(__SGI__) || defined(___BSDI__) || defined(__ALPHA__))
208 return time(0);
209 #endif
210 /*
211 #else
212 struct timeb tp;
213 ftime(&tp);
214 return tp.time;
215 #endif
216 */
217 }
218