No changes, just move time functions from wx/stopwatch.h to wx/time.h.
[wxWidgets.git] / src / common / time.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/time.cpp
3 // Purpose: Implementation of time-related functions.
4 // Author: Vadim Zeitlin
5 // Created: 2011-11-26
6 // RCS-ID: $Id: wxhead.cpp,v 1.11 2010-04-22 12:44:51 zeitlin Exp $
7 // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #include "wx/time.h"
27
28 #ifndef WX_PRECOMP
29 #ifdef __WXMSW__
30 #include "wx/msw/wrapwin.h"
31 #endif
32 #include "wx/intl.h"
33 #include "wx/log.h"
34 #endif
35
36 #ifndef WX_GMTOFF_IN_TM
37 // Define it for some systems which don't (always) use configure but are
38 // known to have tm_gmtoff field.
39 #if defined(__WXPALMOS__) || defined(__DARWIN__)
40 #define WX_GMTOFF_IN_TM
41 #endif
42 #endif
43
44 #if defined(__VISAGECPP__) && !defined(HAVE_FTIME)
45 #define HAVE_FTIME
46 # if __IBMCPP__ >= 400
47 # define ftime(x) _ftime(x)
48 # endif
49 #endif
50
51 #if defined(__MWERKS__) && defined(__WXMSW__)
52 # undef HAVE_FTIME
53 # undef HAVE_GETTIMEOFDAY
54 #endif
55
56 #ifndef __WXPALMOS5__
57 #ifndef __WXWINCE__
58 #include <time.h>
59 #else
60 #include "wx/msw/private.h"
61 #include "wx/msw/wince/time.h"
62 #endif
63 #endif // __WXPALMOS5__
64
65
66 #if !defined(__WXMAC__) && !defined(__WXWINCE__)
67 #include <sys/types.h> // for time_t
68 #endif
69
70 #if defined(HAVE_GETTIMEOFDAY)
71 #include <sys/time.h>
72 #include <unistd.h>
73 #elif defined(HAVE_FTIME)
74 #include <sys/timeb.h>
75 #endif
76
77 #ifdef __WXPALMOS__
78 #include <DateTime.h>
79 #include <TimeMgr.h>
80 #include <SystemMgr.h>
81 #endif
82
83 namespace
84 {
85
86 const int MILLISECONDS_PER_SECOND = 1000;
87 const int MICROSECONDS_PER_MILLISECOND = 1000;
88 const int MICROSECONDS_PER_SECOND = 1000*1000;
89
90 } // anonymous namespace
91
92 // ============================================================================
93 // implementation
94 // ============================================================================
95
96 // returns the time zone in the C sense, i.e. the difference UTC - local
97 // (in seconds)
98 int wxGetTimeZone()
99 {
100 #ifdef WX_GMTOFF_IN_TM
101 // set to true when the timezone is set
102 static bool s_timezoneSet = false;
103 static long gmtoffset = LONG_MAX; // invalid timezone
104
105 // ensure that the timezone variable is set by calling wxLocaltime_r
106 if ( !s_timezoneSet )
107 {
108 // just call wxLocaltime_r() instead of figuring out whether this
109 // system supports tzset(), _tzset() or something else
110 time_t t = time(NULL);
111 struct tm tm;
112
113 wxLocaltime_r(&t, &tm);
114 s_timezoneSet = true;
115
116 // note that GMT offset is the opposite of time zone and so to return
117 // consistent results in both WX_GMTOFF_IN_TM and !WX_GMTOFF_IN_TM
118 // cases we have to negate it
119 gmtoffset = -tm.tm_gmtoff;
120
121 // this function is supposed to return the same value whether DST is
122 // enabled or not, so we need to use an additional offset if DST is on
123 // as tm_gmtoff already does include it
124 if ( tm.tm_isdst )
125 gmtoffset += 3600;
126 }
127 return (int)gmtoffset;
128 #elif defined(__DJGPP__) || defined(__WINE__)
129 struct timeb tb;
130 ftime(&tb);
131 return tb.timezone*60;
132 #elif defined(__VISUALC__)
133 // We must initialize the time zone information before using it (this will
134 // be done only once internally).
135 _tzset();
136
137 // Starting with VC++ 8 timezone variable is deprecated and is not even
138 // available in some standard library version so use the new function for
139 // accessing it instead.
140 #if wxCHECK_VISUALC_VERSION(8)
141 long t;
142 _get_timezone(&t);
143 return t;
144 #else // VC++ < 8
145 return timezone;
146 #endif
147 #elif defined(WX_TIMEZONE) // If WX_TIMEZONE was defined by configure, use it.
148 return WX_TIMEZONE;
149 #elif defined(__BORLANDC__) || defined(__MINGW32__) || defined(__VISAGECPP__)
150 return _timezone;
151 #elif defined(__MWERKS__)
152 return 28800;
153 #else // unknown platform -- assume it has timezone
154 return timezone;
155 #endif // WX_GMTOFF_IN_TM/!WX_GMTOFF_IN_TM
156 }
157
158 // Get local time as seconds since 00:00:00, Jan 1st 1970
159 long wxGetLocalTime()
160 {
161 struct tm tm;
162 time_t t0, t1;
163
164 // This cannot be made static because mktime can overwrite it.
165 //
166 memset(&tm, 0, sizeof(tm));
167 tm.tm_year = 70;
168 tm.tm_mon = 0;
169 tm.tm_mday = 5; // not Jan 1st 1970 due to mktime 'feature'
170 tm.tm_hour = 0;
171 tm.tm_min = 0;
172 tm.tm_sec = 0;
173 tm.tm_isdst = -1; // let mktime guess
174
175 // Note that mktime assumes that the struct tm contains local time.
176 //
177 t1 = time(&t1); // now
178 t0 = mktime(&tm); // origin
179
180 // Return the difference in seconds.
181 //
182 if (( t0 != (time_t)-1 ) && ( t1 != (time_t)-1 ))
183 return (long)difftime(t1, t0) + (60 * 60 * 24 * 4);
184
185 wxLogSysError(_("Failed to get the local system time"));
186 return -1;
187 }
188
189 // Get UTC time as seconds since 00:00:00, Jan 1st 1970
190 long wxGetUTCTime()
191 {
192 return (long)time(NULL);
193 }
194
195 #if wxUSE_LONGLONG
196
197 wxLongLong wxGetUTCTimeUSec()
198 {
199 #if defined(__WXMSW__)
200 FILETIME ft;
201 ::GetSystemTimeAsFileTime(&ft);
202
203 // FILETIME is in 100ns or 0.1us since 1601-01-01, transform to us since
204 // 1970-01-01.
205 wxLongLong t(ft.dwHighDateTime, ft.dwLowDateTime);
206 t /= 10;
207 t -= wxLL(11644473600000000); // Unix - Windows epochs difference in us.
208 return t;
209 #else // non-MSW
210
211 #ifdef HAVE_GETTIMEOFDAY
212 timeval tv;
213 if ( wxGetTimeOfDay(&tv) != -1 )
214 {
215 wxLongLong val(tv.tv_sec);
216 val *= MICROSECONDS_PER_SECOND;
217 val += tv.tv_usec;
218 return val;
219 }
220 #endif // HAVE_GETTIMEOFDAY
221
222 // Fall back to lesser precision function.
223 return wxGetUTCTimeMillis()*MICROSECONDS_PER_MILLISECOND;
224 #endif // MSW/!MSW
225 }
226
227 // Get local time as milliseconds since 00:00:00, Jan 1st 1970
228 wxLongLong wxGetUTCTimeMillis()
229 {
230 wxLongLong val = MILLISECONDS_PER_SECOND;
231
232 // If possible, use a function which avoids conversions from
233 // broken-up time structures to milliseconds
234 #if defined(__WXPALMOS__)
235 DateTimeType thenst;
236 thenst.second = 0;
237 thenst.minute = 0;
238 thenst.hour = 0;
239 thenst.day = 1;
240 thenst.month = 1;
241 thenst.year = 1970;
242 thenst.weekDay = 5;
243 uint32_t now = TimGetSeconds();
244 uint32_t then = TimDateTimeToSeconds (&thenst);
245 return SysTimeToMilliSecs(SysTimeInSecs(now - then));
246 #elif defined(__WXMSW__)
247 FILETIME ft;
248 ::GetSystemTimeAsFileTime(&ft);
249
250 // FILETIME is expressed in 100ns (or 0.1us) units since 1601-01-01,
251 // transform them to ms since 1970-01-01.
252 wxLongLong t(ft.dwHighDateTime, ft.dwLowDateTime);
253 t /= 10000;
254 t -= wxLL(11644473600000); // Unix - Windows epochs difference in ms.
255 return t;
256 #elif defined(HAVE_GETTIMEOFDAY)
257 struct timeval tp;
258 if ( wxGetTimeOfDay(&tp) != -1 )
259 {
260 val *= tp.tv_sec;
261 return (val + (tp.tv_usec / MICROSECONDS_PER_MILLISECOND));
262 }
263 else
264 {
265 wxLogError(_("wxGetTimeOfDay failed."));
266 return 0;
267 }
268 #elif defined(HAVE_FTIME)
269 struct timeb tp;
270
271 // ftime() is void and not int in some mingw32 headers, so don't
272 // test the return code (well, it shouldn't fail anyhow...)
273 (void)::ftime(&tp);
274 val *= tp.time;
275 return (val + tp.millitm);
276 #else // no gettimeofday() nor ftime()
277 // If your platform/compiler does not support ms resolution please
278 // do NOT just shut off these warnings, drop me a line instead at
279 // <guille@iies.es>
280
281 #if defined(__VISUALC__) || defined (__WATCOMC__)
282 #pragma message("wxStopWatch will be up to second resolution!")
283 #elif defined(__BORLANDC__)
284 #pragma message "wxStopWatch will be up to second resolution!"
285 #else
286 #warning "wxStopWatch will be up to second resolution!"
287 #endif // compiler
288
289 val *= wxGetUTCTime();
290 return val;
291 #endif // time functions
292 }
293
294 wxLongLong wxGetLocalTimeMillis()
295 {
296 return wxGetUTCTimeMillis() - wxGetTimeZone()*MILLISECONDS_PER_SECOND;
297 }
298
299 #else // !wxUSE_LONGLONG
300
301 double wxGetLocalTimeMillis(void)
302 {
303 return (double(clock()) / double(CLOCKS_PER_SEC)) * 1000.0;
304 }
305
306 #endif // wxUSE_LONGLONG/!wxUSE_LONGLONG