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