]> git.saurik.com Git - wxWidgets.git/blame - src/common/timercmn.cpp
1. DLL compilation fixes (wrong WXDLLEXPORT placement) in db.h/cpp
[wxWidgets.git] / src / common / timercmn.cpp
CommitLineData
c801d85f
KB
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
227b5cd7 31#if defined(__SVR4__) && !defined(__SYSV__)
c801d85f
KB
32#define __SYSV__
33#endif
34
35#include <time.h>
ce3ed50d 36
17dff81c 37#ifndef __WXMAC__
c801d85f 38#include <sys/types.h>
17dff81c 39#endif
c801d85f 40
469e1e5c 41#if (!defined(__SC__) && !defined(__SGI__) && !defined(__GNUWIN32__) && !defined(__MWERKS__)) || defined(__MINGW32__)
c801d85f
KB
42#include <sys/timeb.h>
43#endif
44
edaa81ae 45#if defined(__linux__) || defined(__SVR4__) || defined(__SYSV__) || defined(__SGI__) || \
ce3ed50d 46 defined(__ALPHA__) || defined(__GNUWIN32__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
91b8de8d 47 defined(__SALFORDC__) || defined(__EMX__)
c801d85f
KB
48#include <sys/time.h>
49#endif
50
51#ifdef __MINGW32__
52#include "windows.h"
53#endif
54
54f04bc0 55#if defined(__SUN__) || defined(__OSF__) || defined(__FreeBSD__)
c801d85f
KB
56// At least on Sun, ftime is undeclared.
57// Need to be verified on other platforms.
58extern "C" int ftime(struct timeb *tp);
54f04bc0 59//extern "C" int gettimeofday(struct timeval *tp, void *);
c801d85f
KB
60// extern "C" time_t time(time_t);
61// #include <sys/timeb.h>
62#if defined(__SVR4__) && !defined(__ALPHA__)
63// ditto for gettimeofday on Solaris 2.x.
64extern "C" int gettimeofday(struct timeval *tp, void *);
65#endif
66#endif
67
68/*
69 * Timer functions
70 *
71 */
72
73long wxStartTime = 0;
74void wxStartTimer(void)
75{
4f3ac409 76 wxStartTime=wxGetCurrentMTime();
c801d85f
KB
77}
78
79// Returns elapsed time in milliseconds
80long wxGetElapsedTime(bool resetTimer)
f0599ea9
SB
81{
82 long oldTime = wxStartTime;
4f3ac409 83 long newTime=wxGetCurrentMTime();
f0599ea9
SB
84
85 if (resetTimer) wxStartTime = newTime;
86 return newTime - oldTime;
87}
88
89
90// Get number of seconds since 00:00:00 GMT, Jan 1st 1970.
91long wxGetCurrentTime(void)
92{
4f3ac409 93 return wxGetCurrentMTime()/1000;
f0599ea9
SB
94}
95
96// return GMT time in millisecond
4f3ac409 97long wxGetCurrentMTime()
c801d85f 98{
91b8de8d
RR
99#if defined(__xlC__) || defined(__AIX__) || defined(__SVR4__) || defined(__SYSV__) || \
100 (defined(__GNUWIN32__) && !defined(__MINGW32__)) // || defined(__AIXV3__)
c801d85f 101 struct timeval tp;
f57fe24c 102#if defined(__SYSV__) || (defined (__GNUWIN32__) && !defined (__MINGW32__))
c801d85f
KB
103 gettimeofday(&tp, (struct timezone *)NULL);
104#else
105 gettimeofday(&tp);
106#endif
f0599ea9 107 return (1000*tp.tv_sec + tp.tv_usec / 1000);
91b8de8d
RR
108#elif (defined(__SC__) || defined(__SGI__) || defined(___BSDI__) || defined(__ALPHA__) || \
109 defined(__MINGW32__)|| defined(__MWERKS__) || defined(__FreeBSD__))
c801d85f
KB
110 time_t t0;
111 struct tm *tp;
112 time(&t0);
113 tp = localtime(&t0);
f0599ea9 114 return 1000*(60*(60*tp->tm_hour+tp->tm_min)+tp->tm_sec);
c801d85f
KB
115#else
116 struct timeb tp;
117 ftime(&tp);
f0599ea9 118 return (1000*tp.time + tp.millitm);
c801d85f 119#endif
c801d85f
KB
120}
121
f0599ea9
SB
122//---------------
123// wxChrono class
124// This class encapsulates the above fonctions,
125// such that several wxChrono can be created
126// simultaneously
127
128wxChrono::wxChrono()
129{
130 Start();
131}
132
133void wxChrono::Start(long t)
134{
4f3ac409 135 m_t0=wxGetCurrentMTime()-t;
f0599ea9
SB
136 m_pause=0;
137}
138
139void wxChrono::Pause()
140{
4f3ac409 141 m_pause=wxGetCurrentMTime()-m_t0;
f0599ea9
SB
142}
143
144void wxChrono::Resume()
145{
4f3ac409 146 m_t0=wxGetCurrentMTime()-m_pause;
f0599ea9
SB
147 m_pause=0;
148}
149
150long wxChrono::Time()
151{
152 if (m_pause) return m_pause;
4f3ac409 153 return wxGetCurrentMTime()-m_t0;
f0599ea9
SB
154}
155
156
157
c801d85f
KB
158// EXPERIMENTAL: comment this out if it doesn't compile.
159#ifndef __VMS__
160bool wxGetLocalTime(long *timeZone, int *dstObserved)
161{
717b9bf2 162#if defined(__MINGW32__)
c801d85f
KB
163 time_t t0;
164 struct tm *tp;
165 time(&t0);
166 tp = localtime(&t0);
45e726de
HH
167# if __GNUC__ == 2 && __GNUC_MINOR__ <= 8
168 // gcc 2.8.x or earlier
c801d85f
KB
169 timeb tz;
170 ftime(& tz);
171 *timeZone = tz._timezone;
45e726de
HH
172# else
173 // egcs or gcc 2.95
174 *timeZone = _timezone; // tp->tm_gmtoff; // ???
175# endif
c801d85f
KB
176 *dstObserved = tp->tm_isdst;
177#else
45e726de 178// not mingw32...
717b9bf2 179#if (((defined(__SYSV__) && !defined(__HPUX__)) || defined(__MSDOS__) || defined(__WXMSW__) || defined(__WXPM__)) \
ce3ed50d 180 && !defined(__GNUWIN32__) && !defined(__MWERKS__) )
717b9bf2 181# if defined(__BORLANDC__)
c801d85f
KB
182 /* Borland uses underscores */
183 *timeZone = _timezone;
184 *dstObserved = _daylight;
717b9bf2 185# elif defined(__SALFORDC__)
ce3ed50d
JS
186 *timeZone = _timezone;
187 *dstObserved = daylight;
717b9bf2
DW
188# elif defined(__VISAGECPP__)
189 *timeZone = _timezone;
190 *dstObserved = daylight;
191# else
c801d85f
KB
192 *timeZone = timezone;
193 *dstObserved = daylight;
717b9bf2 194# endif
469e1e5c 195#elif defined(__xlC__) || defined(__AIX__) || defined(__SVR4__) || defined(__SYSV__) || defined(__MWERKS__) || (defined(__GNUWIN32__) && !defined(__MINGW32__)) // || defined(__AIXV3__)
717b9bf2 196# ifndef __MWERKS__ // shouldn't this be one scope below ?
c801d85f 197 struct timeval tp;
717b9bf2
DW
198# endif
199# if defined(__SYSV__) || (defined(__GNUWIN32__) && !defined(__MINGW32))
c801d85f
KB
200 struct timezone tz;
201 gettimeofday(&tp, &tz);
202 *timeZone = 60*(tz.tz_minuteswest);
203 *dstObserved = tz.tz_dsttime;
717b9bf2 204# else
c801d85f
KB
205 time_t t0;
206 struct tm *tp;
207 time(&t0);
208 tp = localtime(&t0);
717b9bf2 209# ifndef __MWERKS__
c801d85f 210 *timeZone = tp->tm_gmtoff; // ???
717b9bf2 211# else
469e1e5c 212 *timeZone = 0 ;
717b9bf2 213# endif
c801d85f
KB
214 *dstObserved = tp->tm_isdst;
215#endif
34138703
JS
216#elif defined(__WXSTUBS__)
217 return FALSE;
c801d85f
KB
218#else
219// #error wxGetLocalTime not implemented.
220 struct timeval tp;
221 struct timezone tz;
222 gettimeofday(&tp, &tz);
223 *timeZone = 60*(tz.tz_minuteswest);
224 *dstObserved = tz.tz_dsttime;
225#endif
226#endif
227 // __MINGW32__
228 return TRUE;
229}
230#endif