]> git.saurik.com Git - wxWidgets.git/blame - src/common/timercmn.cpp
* Fixed two memory leaks.
[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
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.
53extern "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.
58extern "C" int gettimeofday(struct timeval *tp, void *);
59#endif
60#endif
61
62/*
63 * Timer functions
64 *
65 */
66
67long wxStartTime = 0;
68void 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
92long 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__
127bool 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
2049ba38 147#if (((defined(__SYSV__) && !defined(__HPUX__)) || defined(__MSDOS__) || defined(__WXMSW__)) && !defined(__GNUWIN32__))
c801d85f
KB
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
34138703
JS
171#elif defined(__WXSTUBS__)
172 return FALSE;
c801d85f
KB
173#else
174// #error wxGetLocalTime not implemented.
175 struct timeval tp;
176 struct timezone tz;
177 gettimeofday(&tp, &tz);
178 *timeZone = 60*(tz.tz_minuteswest);
179 *dstObserved = tz.tz_dsttime;
180#endif
181#endif
182 // __MINGW32__
183 return TRUE;
184}
185#endif
186
187// Get number of seconds since 00:00:00 GMT, Jan 1st 1970.
188long wxGetCurrentTime(void)
189{
190#if defined(__xlC__) || defined(__AIX__) || defined(__SVR4__) || defined(__SYSV__) // || defined(__AIXV3__)
191 struct timeval tp;
192#ifdef __SYSV__
193 gettimeofday(&tp, (struct timezone *)NULL);
194#else
195 gettimeofday(&tp);
196#endif
197 return tp.tv_sec;
198#else // (defined(__SC__) || defined(__SGI__) || defined(___BSDI__) || defined(__ALPHA__))
199 return time(0);
200#endif
201/*
202#else
203 struct timeb tp;
204 ftime(&tp);
205 return tp.time;
206#endif
207*/
208}
209