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