]> git.saurik.com Git - wxWidgets.git/blame - src/common/time.cpp
* Fixed two memory leaks.
[wxWidgets.git] / src / common / time.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: time.cpp
3// Purpose: wxTime class, from NIHCL
4// Author: Julian Smart, after K. E. Gorlen
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 "time.h"
14#endif
15
16/*
17Provides an object that represents a Time, stored as the number of
18seconds since January 1, 1901, GMT.
19*/
20
21// For compilers that support precompilation, includes "wx.h".
22#include "wx/wxprec.h"
23
24#ifdef __BORLANDC__
25#pragma hdrstop
26#endif
27
28#include "wx/setup.h"
29
30#if USE_TIMEDATE
31
32#include "wx/time.h"
33#include "wx/date.h"
34#include "wx/utils.h"
1a5a8367 35#include <wx/intl.h>
c801d85f
KB
36
37#if USE_IOSTREAMH
38#include <iostream.h>
39#else
40#include <iostream>
41#endif
42
43#include <iomanip.h>
44#include <string.h>
45
46#if !USE_SHARED_LIBRARY
47IMPLEMENT_DYNAMIC_CLASS(wxTime, wxObject)
48#endif
49
50
51extern bool wxGetLocalTime(long *timeZone, int *dstObserved);
52extern long wxGetCurrentTime(void);
53
54static long TIME_ZONE; /* seconds west of GMT */
55static int DST_OBSERVED; /* flags U.S. daylight saving time observed */
56
57static bool wxTimeInitialized = FALSE;
58
59wxTime::tFormat wxTime::Format = wxTime::wx12h;
60wxTime::tPrecision wxTime::Precision = wxTime::wxStdMinSec;
61
62static const unsigned long seconds_in_day = 24*60*60L;
63static const wxDate refDate(1,1,1901);
64// static const wxDate maxDate(49709L); /* ((2**32)-1)/seconds_in_day -1 */
65
66wxTime wxTime::GetLocalTime(const wxDate& date, hourTy h, minuteTy m, secondTy s)
67/*
68 Return a local wxTime for the specified Standard Time date, hour, minute,
69 and second.
70*/
71{
72 if (!wxTimeInitialized)
73 {
74 wxGetLocalTime(&TIME_ZONE, &DST_OBSERVED);
75 wxTimeInitialized = TRUE;
76 }
77/*
78 if (!date.IsBetween(refDate,maxDate))
79 setError(NIHCL_DATERANGE,DEFAULT,
80 date.dayOfMonth(),date.nameOfMonth(),date.year());
81*/
82 // The following line causes an error in GCC 2.1
83// long daysBetween = date-refDate;
84 // ... but this seems to get round it.
85 wxDate tmp1(date);
86 wxDate tmp2(refDate);
87 long daysBetween = tmp1 - tmp2;
88
89 return wxTime(seconds_in_day*daysBetween + 60*60L*h + 60*m + s);
90}
91
92wxTime::wxTime()
93/*
94 Construct a wxTime for this instant.
95*/
96{
97 if (!wxTimeInitialized)
98 {
99 wxGetLocalTime(&TIME_ZONE, &DST_OBSERVED);
100 wxTimeInitialized = TRUE;
101 }
102 sec = wxGetCurrentTime();
1f0299c1 103 sec += 2177452800UL; /* seconds from 1/1/01 to 1/1/70 */
c801d85f
KB
104}
105
106wxTime::wxTime(hourTy h, minuteTy m, secondTy s, bool dst)
107/*
108 Construct a wxTime for today at the specified (local) hour, minute, and
109 second.
110*/
111{
112 if (!wxTimeInitialized)
113 {
114 wxGetLocalTime(&TIME_ZONE, &DST_OBSERVED);
115 wxTimeInitialized = TRUE;
116 }
117
118 sec = wxTime(wxDate(),h,m,s,dst).sec;
119}
120
121
122wxTime::wxTime(const wxDate& date, hourTy h, minuteTy m, secondTy s, bool dst)
123/*
124 Construct a wxTime for the specified (local) Date, hour, minute, and
125 second.
126*/
127{
128 if (!wxTimeInitialized)
129 {
130 wxGetLocalTime(&TIME_ZONE, &DST_OBSERVED);
131 wxTimeInitialized = TRUE;
132 }
133 sec = GetLocalTime(date,h,m,s).sec-3600;
134 if (IsDST())
135 {
136 sec += 3600;
137 if (IsDST() || dst) sec -= 3600;
138 }
139 else
140 {
141 sec += 3600;
142/*
143 if (IsDST()) setError(NIHCL_BADTIME,DEFAULT,
144 date.dayOfMonth(),date.nameOfMonth(),date.year(),
1a5a8367 145 h,m,s,(dst?_("DST"):""));
c801d85f
KB
146*/
147 }
148 sec += TIME_ZONE; // adjust to GMT
149}
150
151wxTime::operator wxDate() const
152/*
153 Convert a wxTime to a local wxDate
154*/
155{
156// return wxDate((int)(GetLocalTime().sec/seconds_in_day)); 4.2 cc bug
157 long daycount = (long)(GetLocalTime().sec/seconds_in_day);
158
159 wxDate date(1,1,1901);
160 date += daycount;
161 return date;
162}
163
164bool wxTime::IsBetween(const wxTime& a, const wxTime& b) const
165{
166 return *this >= a && *this <= b;
167}
168
169hourTy wxTime::GetHour() const
170/*
171 Return the hour of this wxTime in local time; i.e., adjust for
172 time zone and Daylight Savings Time.
173*/
174{
175 return GetLocalTime().GetHourGMT();
176}
177
178hourTy wxTime::GetHourGMT() const
179/*
180 Return the hour of this Time in GMT.
181*/
182{
183 return (hourTy)((sec % 86400) / 3600);
184}
185
186wxTime wxTime::GetBeginDST(unsigned year)
187/*
188 Return the local Standard Time at which Daylight Savings Time
189 begins in the specified year.
190*/
191{
192 // Previous Sunday
193 wxTime DSTtime(GetLocalTime(wxDate(3,31,year).Previous(1)+7,2));
194 if (year<=1986) {
195 // Previous Sunday
196 DSTtime = GetLocalTime(wxDate(4,30,year).Previous(1),2);
197 if (year==1974) DSTtime = GetLocalTime(wxDate(1,6,1974),2);
198 if (year==1975) DSTtime = GetLocalTime(wxDate(2,23,1975),2);
199 }
200 return DSTtime;
201}
202
203wxTime wxTime::GetEndDST(unsigned year)
204/*
205 Return the local Standard Time at which Daylight Savings Time
206 ends in the specified year.
207*/
208{
209 wxTime STDtime(GetLocalTime(wxDate(10,31,year).Previous(1),2-1));
210 return STDtime;
211}
212
213bool wxTime::IsDST() const
214/*
215 Return TRUE if this local Standard Time should be adjusted
216 for Daylight Savings Time.
217*/
218{
219 long daycount = (long)(sec/seconds_in_day);
220
221 // At this point, daycount is the number of days from 1/1/1901.
222 // Need to convert to julian date (which starts at 1/1/4713 B.C.)
223 wxDate date(1,1,1901);
224 date += daycount;
225
226 unsigned year = date.GetYear();
227 if (DST_OBSERVED)
228 {
229 if (*this >= GetBeginDST(year))
230 if (*this < GetEndDST(year)) return TRUE;
231 }
232 return FALSE;
233}
234
235wxTime wxTime::GetLocalTime() const
236/*
237 Adjusts this GM Time for local time zone and Daylight Savings Time.
238*/
239{
240 wxTime local_time(sec-TIME_ZONE);
241 if (local_time.IsDST()) local_time.sec += 3600;
242 return local_time;
243}
244
245minuteTy wxTime::GetMinute() const
246/*
247 Return the minute of this wxTime in local time; i.e., adjust
248 for time zone and Daylight Savings Time.
249*/
250{
251 return GetLocalTime().GetMinuteGMT();
252}
253
254minuteTy wxTime::GetMinuteGMT() const
255/*
256 Return the minute of this wxTime in GMT.
257*/
258{
259 return (minuteTy)(((sec % 86400) % 3600) / 60);
260}
261
262secondTy wxTime::GetSecond() const
263/*
264 Return the second of this wxTime.
265*/
266{
267 return (secondTy)(((sec % 86400) % 3600) % 60);
268}
269
270wxTime wxTime::Max(const wxTime& t) const
271{
272 if (t < *this) return *this;
273 return t;
274}
275
276wxTime wxTime::Min(const wxTime& t) const
277{
278 if (t > *this) return *this;
279 return t;
280}
281
282wxTime::operator char *(void)
283{
284 return FormatTime();
285}
286
287void wxTime::SetFormat(const wxTime::tFormat lFormat,
288 const wxTime::tPrecision lPrecision) {
289
290 wxTime::Format = lFormat;
291 wxTime::Precision = lPrecision;
292}
293
294char *wxTime::FormatTime() const {
295 static char timeBuf[30];
296 unsigned hh(GetHour());
297
298 switch (Format) {
299 case wx12h:
300 hh -= 12;
301 break;
302 case wx24h:
303 break;
304 }
305
306 switch (Precision) {
307 case wxStdMinSec:
308 sprintf(timeBuf,"%2d:%02d:%02d",hh,GetMinute(),GetSecond());
309 break;
310 case wxStdMin:
311 sprintf(timeBuf,"%2d:%02d",hh,GetMinute());
312 break;
313 }
314
315 if (Format == wx12h)
316 if (GetHour() <= 12)
1a5a8367 317 strcat(timeBuf,_("am"));
c801d85f 318 else
1a5a8367 319 strcat(timeBuf,_("pm"));
c801d85f
KB
320
321 return timeBuf;
322}
323
324/*
325int wxTime::compare(const Object& ob) const
326{
327 assertArgSpecies(ob,classDesc,"compare");
328 register clockTy t = castdown(ob).sec;
329 if (sec < t) return -1;
330 if (sec > t) return 1;
331 return 0;
332}
333
334void wxTime::deepenShallowCopy() {}
335
336unsigned wxTime::hash() const { return sec; }
337
338bool wxTime::isEqual(const Object& ob) const
339{
340 return ob.isSpecies(classDesc) && *this==castdown(ob);
341}
342
343const Class* wxTime::species() const { return &classDesc; }
344
345void wxTime::printOn(ostream& strm) const
346{
347 register unsigned hh = GetHour();
348 wxDate(*this).printOn(strm);
349 strm << ' ' << ((hh <= 12) ? hh : hh-12) << ':'
350 << setfill('0') << setw(2) << GetMinute() << ':'
351 << setfill('0') << setw(2) << GetSecond() << ' ';
1a5a8367
DP
352 if (hh < 12) strm << _("am");
353 else strm << _("pm");
c801d85f
KB
354}
355
356wxTime::wxTime(OIOin& strm)
357 : BASE(strm)
358{
359 unsigned long usec;
360 strm >> sec >> usec;
361}
362
363void wxTime::storer(OIOout& strm) const
364{
365 BASE::storer(strm);
366 strm << sec << 0l;
367}
368
369
370wxTime::wxTime(OIOifd& fd)
371 : BASE(fd)
372{
373 unsigned long usec;
374 fd >> sec >> usec;
375}
376
377void wxTime::storer(OIOofd& fd) const
378{
379 BASE::storer(fd);
380 fd << sec << 0l;
381}
382*/
383
384#endif