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