]> git.saurik.com Git - wxWidgets.git/blame - src/common/timercmn.cpp
unused param warning fixed
[wxWidgets.git] / src / common / timercmn.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
0470b1e6 2// Name: common/timercmn.cpp
c801d85f
KB
3// Purpose: Common timer implementation
4// Author: Julian Smart
0470b1e6 5// Modified by: Vadim Zeitlin on 12.11.99 to get rid of all ifdefs
c801d85f
KB
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
0470b1e6
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
c801d85f 20#ifdef __GNUG__
0470b1e6 21 #pragma implementation "timerbase.h"
c801d85f
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
0470b1e6 28 #pragma hdrstop
c801d85f
KB
29#endif
30
31#ifndef WX_PRECOMP
0470b1e6
VZ
32 #include "wx/intl.h"
33 #include "wx/log.h"
c801d85f
KB
34#endif
35
36#include "wx/timer.h"
37
0470b1e6
VZ
38// I'm told VMS is POSIX, so should have localtime()
39#if defined(__WXMSW__) || defined(__VMS__)
40 // configure might have found it already for us
41 #ifndef HAVE_LOCALTIME
42 #define HAVE_LOCALTIME
43
44 // TODO add test for broken compilers here if needed
45 #define WX_GMTOFF_IN_TM
46 #endif
47#endif
48
49#if defined(HAVE_GETTIMEOFDAY)
50 #include <sys/time.h>
51 #include <unistd.h>
52#elif defined(HAVE_LOCALTIME)
53 #include <time.h>
54 #ifndef __WXMAC__
55 #include <sys/types.h> // for time_t
56 #endif
57#elif defined(HAVE_FTIME)
58 #include <sys/timeb.h>
59#else
60 #error "no function to find the current time on this system"
c801d85f
KB
61#endif
62
0470b1e6
VZ
63// ----------------------------------------------------------------------------
64// macros
65// ----------------------------------------------------------------------------
ce3ed50d 66
0470b1e6
VZ
67// on some really old systems gettimeofday() doesn't have the second argument,
68// define wxGetTimeOfDay() to hide this difference
69#ifdef HAVE_GETTIMEOFDAY
70 #ifdef WX_GETTIMEOFDAY_NO_TZ
71 struct timezone;
72 #define wxGetTimeOfDay(tv, tz) gettimeofday(tv)
73 #else
74 #define wxGetTimeOfDay(tv, tz) gettimeofday((tv), (tz))
75 #endif
76#endif // HAVE_GETTIMEOFDAY
c801d85f 77
0470b1e6
VZ
78// ============================================================================
79// implementation
80// ============================================================================
c801d85f 81
0470b1e6
VZ
82// ----------------------------------------------------------------------------
83// wxStopWatch
84// ----------------------------------------------------------------------------
c801d85f 85
0470b1e6
VZ
86void wxStopWatch::Start(long t)
87{
88 m_t0 = wxGetCurrentMTime() - t;
c801d85f 89
0470b1e6
VZ
90 m_pause = 0;
91}
92
93long wxStopWatch::Time() const
94{
95 return m_pause ? m_pause : GetElapsedTime();
96}
97
98// ----------------------------------------------------------------------------
99// old timer functions superceded by wxStopWatch
100// ----------------------------------------------------------------------------
c801d85f 101
0470b1e6 102static long wxStartTime = 0;
c801d85f 103
0470b1e6
VZ
104// starts the global timer
105void wxStartTimer()
c801d85f 106{
0470b1e6 107 wxStartTime = wxGetCurrentMTime();
c801d85f
KB
108}
109
110// Returns elapsed time in milliseconds
111long wxGetElapsedTime(bool resetTimer)
f0599ea9 112{
0470b1e6
VZ
113 long oldTime = wxStartTime;
114 long newTime = wxGetCurrentMTime();
f0599ea9 115
0470b1e6
VZ
116 if ( resetTimer )
117 wxStartTime = newTime;
118
119 return newTime - oldTime;
f0599ea9
SB
120}
121
122
123// Get number of seconds since 00:00:00 GMT, Jan 1st 1970.
0470b1e6 124long wxGetCurrentTime()
f0599ea9 125{
0470b1e6 126 return wxGetCurrentMTime() / 1000;
f0599ea9
SB
127}
128
0470b1e6
VZ
129// ----------------------------------------------------------------------------
130// the functions to get the current time and timezone info
131// ----------------------------------------------------------------------------
132
f0599ea9 133// return GMT time in millisecond
4f3ac409 134long wxGetCurrentMTime()
c801d85f 135{
0470b1e6
VZ
136#if defined(HAVE_LOCALTIME)
137 time_t t0 = time(&t0);
138 if ( t0 != (time_t)-1 )
139 {
140 struct tm *tp = localtime(&t0);
141
142 if ( tp )
143 {
144 return 1000*(60*(60*tp->tm_hour+tp->tm_min)+tp->tm_sec);
145 }
146 }
147#elif defined(HAVE_GETTIMEOFDAY)
148 struct timeval tp;
149 if ( wxGetTimeOfDay(&tp, (struct timezone *)NULL) != -1 )
150 {
151 return (1000*tp.tv_sec + tp.tv_usec / 1000);
152 }
153#elif defined(HAVE_FTIME)
154 struct timeb tp;
155 if ( ftime(&tp) == 0 )
156 {
157 return (1000*tp.time + tp.millitm);
158 }
c801d85f 159#else
0470b1e6 160 #error "no function to find the current time on this system"
c801d85f 161#endif
c801d85f 162
0470b1e6 163 wxLogSysError(_("Failed to get the system time"));
f0599ea9 164
0470b1e6 165 return -1;
f0599ea9
SB
166}
167
c801d85f
KB
168bool wxGetLocalTime(long *timeZone, int *dstObserved)
169{
0470b1e6
VZ
170#if defined(HAVE_LOCALTIME) && defined(WX_GMTOFF_IN_TM)
171 time_t t0 = time(&t0);
172 if ( t0 != (time_t)-1 )
173 {
174 struct tm *tm = localtime(&t0);
175
176 if ( tm )
177 {
178 *timeZone = tm->tm_gmtoff;
179 *dstObserved = tm->tm_isdst;
180
181 return TRUE;
182 }
183 }
184#elif defined(HAVE_GETTIMEOFDAY) && !defined(WX_GETTIMEOFDAY_NO_TZ)
185 struct timeval tp;
186 struct timezone tz;
187 if ( gettimeofday(&tp, &tz) != -1 )
188 {
189 *timeZone = 60*tz.tz_minuteswest;
190 *dstObserved = tz.tz_dsttime;
191
192 return TRUE;
193 }
194#elif defined(HAVE_FTIME)
195 struct timeb tb;
196 if ( ftime(&tb) == 0 )
197 {
198 *timeZone = 60*tb.timezone;
199 *dstObserved = tb.dstflag;
200 }
c801d85f 201#else
0470b1e6
VZ
202 // special hacks for known compilers - I wonder if this is still needed,
203 // i.e. if there are any of them which don't support localtime()? (VZ)
204
205 #if defined(__BORLANDC__)
206 *timeZone = _timezone;
207 *dstObserved = _daylight;
208 #elif defined(__SALFORDC__)
209 *timeZone = _timezone;
210 *dstObserved = daylight;
211 #elif defined(__VISAGECPP__)
212 *timeZone = _timezone;
213 *dstObserved = daylight;
214 #else
215 wxFAIL_MSG(_T("wxGetLocalTime() not implemented"));
216 #endif // compiler
217#endif
218
219 return FALSE;
c801d85f 220}