]>
git.saurik.com Git - wxWidgets.git/blob - src/common/time.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTime class, from NIHCL
4 // Author: Julian Smart, after K. E. Gorlen
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "time.h"
17 Provides an object that represents a Time, stored as the number of
18 seconds since January 1, 1901, GMT.
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
37 #if wxUSE_STD_IOSTREAM
38 #include "wx/ioswrap.h"
48 IMPLEMENT_DYNAMIC_CLASS(wxTime
, wxObject
)
51 extern bool wxGetLocalTime(long *timeZone
, int *dstObserved
);
52 extern long wxGetCurrentTime(void);
54 static long TIME_ZONE
; /* seconds west of GMT */
55 static int DST_OBSERVED
; /* flags U.S. daylight saving time observed */
57 static bool wxTimeInitialized
= FALSE
;
59 wxTime::tFormat
wxTime::Format
= wxTime::wx12h
;
60 wxTime::tPrecision
wxTime::Precision
= wxTime::wxStdMinSec
;
62 static const unsigned long seconds_in_day
= 24*60*60L;
63 static const wxDate
refDate(1,1,1901);
64 // static const wxDate maxDate(49709L); /* ((2**32)-1)/seconds_in_day -1 */
66 wxTime
wxTime::GetLocalTime(const wxDate
& date
, hourTy h
, minuteTy m
, secondTy s
)
68 Return a local wxTime for the specified Standard Time date, hour, minute,
72 if (!wxTimeInitialized
)
74 wxGetLocalTime(&TIME_ZONE
, &DST_OBSERVED
);
75 wxTimeInitialized
= TRUE
;
78 if (!date.IsBetween(refDate,maxDate))
79 setError(NIHCL_DATERANGE,DEFAULT,
80 date.dayOfMonth(),date.nameOfMonth(),date.year());
82 // The following line causes an error in GCC 2.1
83 // long daysBetween = date-refDate;
84 // ... but this seems to get round it.
87 long daysBetween
= tmp1
- tmp2
;
89 return wxTime(seconds_in_day
*daysBetween
+ 60*60L*h
+ 60*m
+ s
);
94 Construct a wxTime for this instant.
97 if (!wxTimeInitialized
)
99 wxGetLocalTime(&TIME_ZONE
, &DST_OBSERVED
);
100 wxTimeInitialized
= TRUE
;
102 sec
= wxGetCurrentTime();
104 sec
+= (unsigned long) 2177452800; /* seconds from 1/1/01 to 1/1/70 */
106 sec
+= 2177452800UL; /* seconds from 1/1/01 to 1/1/70 */
110 wxTime::wxTime(hourTy h
, minuteTy m
, secondTy s
, bool dst
)
112 Construct a wxTime for today at the specified (local) hour, minute, and
116 if (!wxTimeInitialized
)
118 wxGetLocalTime(&TIME_ZONE
, &DST_OBSERVED
);
119 wxTimeInitialized
= TRUE
;
122 sec
= wxTime(wxDate(),h
,m
,s
,dst
).sec
;
126 wxTime::wxTime(const wxDate
& date
, hourTy h
, minuteTy m
, secondTy s
, bool dst
)
128 Construct a wxTime for the specified (local) Date, hour, minute, and
132 if (!wxTimeInitialized
)
134 wxGetLocalTime(&TIME_ZONE
, &DST_OBSERVED
);
135 wxTimeInitialized
= TRUE
;
137 sec
= GetLocalTime(date
,h
,m
,s
).sec
-3600;
141 if (IsDST() || dst
) sec
-= 3600;
147 if (IsDST()) setError(NIHCL_BADTIME,DEFAULT,
148 date.dayOfMonth(),date.nameOfMonth(),date.year(),
149 h,m,s,(dst?_("DST"):""));
152 sec
+= TIME_ZONE
; // adjust to GMT
156 wxTime::operator wxDate() const
158 Convert a wxTime to a local wxDate
161 // return wxDate((int)(GetLocalTime().sec/seconds_in_day)); 4.2 cc bug
162 long daycount
= (long)(GetLocalTime().sec
/seconds_in_day
);
164 wxDate
date(1,1,1901);
170 bool wxTime::IsBetween(const wxTime
& a
, const wxTime
& b
) const
172 return *this >= a
&& *this <= b
;
175 hourTy
wxTime::GetHour() const
177 Return the hour of this wxTime in local time; i.e., adjust for
178 time zone and Daylight Savings Time.
181 return GetLocalTime().GetHourGMT();
184 hourTy
wxTime::GetHourGMT() const
186 Return the hour of this Time in GMT.
189 return (hourTy
)((sec
% 86400) / 3600);
192 wxTime
wxTime::GetBeginDST(unsigned year
)
194 Return the local Standard Time at which Daylight Savings Time
195 begins in the specified year.
199 wxTime
DSTtime(GetLocalTime(wxDate(3,31,year
).Previous(1)+7,2));
202 DSTtime
= GetLocalTime(wxDate(4,30,year
).Previous(1),2);
203 if (year
==1974) DSTtime
= GetLocalTime(wxDate(1,6,1974),2);
204 if (year
==1975) DSTtime
= GetLocalTime(wxDate(2,23,1975),2);
209 wxTime
wxTime::GetEndDST(unsigned year
)
211 Return the local Standard Time at which Daylight Savings Time
212 ends in the specified year.
215 wxTime
STDtime(GetLocalTime(wxDate(10,31,year
).Previous(1),2-1));
219 bool wxTime::IsDST() const
221 Return TRUE if this local Standard Time should be adjusted
222 for Daylight Savings Time.
225 long daycount
= (long)(sec
/seconds_in_day
);
227 // At this point, daycount is the number of days from 1/1/1901.
228 // Need to convert to julian date (which starts at 1/1/4713 B.C.)
229 wxDate
date(1,1,1901);
232 unsigned year
= date
.GetYear();
235 if (*this >= GetBeginDST(year
))
236 if (*this < GetEndDST(year
)) return TRUE
;
241 wxTime
wxTime::GetLocalTime() const
243 Adjusts this GM Time for local time zone and Daylight Savings Time.
246 wxTime
local_time(sec
-TIME_ZONE
);
247 if (local_time
.IsDST()) local_time
.sec
+= 3600;
251 minuteTy
wxTime::GetMinute() const
253 Return the minute of this wxTime in local time; i.e., adjust
254 for time zone and Daylight Savings Time.
257 return GetLocalTime().GetMinuteGMT();
260 minuteTy
wxTime::GetMinuteGMT() const
262 Return the minute of this wxTime in GMT.
265 return (minuteTy
)(((sec
% 86400) % 3600) / 60);
268 secondTy
wxTime::GetSecond() const
270 Return the second of this wxTime.
273 return (secondTy
)(((sec
% 86400) % 3600) % 60);
276 secondTy
wxTime::GetSecondGMT() const
278 Return the minute of this wxTime in GMT.
281 return (secondTy
)(((sec
% 86400) % 3600) % 60);
284 int wxTime::GetDay() const
286 wxDate
da((wxDate
) *this);
290 int wxTime::GetDayOfWeek() const
292 wxDate
da((wxDate
) *this);
293 return da
.GetDayOfWeek();
296 int wxTime::GetMonth() const
298 wxDate
da((wxDate
) *this);
299 return da
.GetMonth();
302 int wxTime::GetYear() const
304 wxDate
da((wxDate
) *this);
308 wxTime
wxTime::Max(const wxTime
& t
) const
310 if (t
< *this) return *this;
314 wxTime
wxTime::Min(const wxTime
& t
) const
316 if (t
> *this) return *this;
321 wxTime::operator wxChar
*(void)
327 void wxTime::SetFormat(const wxTime::tFormat lFormat
,
328 const wxTime::tPrecision lPrecision
) {
330 wxTime::Format
= lFormat
;
331 wxTime::Precision
= lPrecision
;
334 wxChar
*wxTime::FormatTime() const {
335 static wxChar timeBuf
[30];
336 unsigned hh(GetHour());
348 wxSprintf(timeBuf
,wxT("%2d:%02d:%02d"),hh
,GetMinute(),GetSecond());
351 wxSprintf(timeBuf
,wxT("%2d:%02d"),hh
,GetMinute());
357 wxStrcat(timeBuf
,_("am"));
359 wxStrcat(timeBuf
,_("pm"));
365 int wxTime::compare(const Object& ob) const
367 assertArgSpecies(ob,classDesc,"compare");
368 register clockTy t = castdown(ob).sec;
369 if (sec < t) return -1;
370 if (sec > t) return 1;
374 void wxTime::deepenShallowCopy() {}
376 unsigned wxTime::hash() const { return sec; }
378 bool wxTime::isEqual(const Object& ob) const
380 return ob.isSpecies(classDesc) && *this==castdown(ob);
383 const Class* wxTime::species() const { return &classDesc; }
385 void wxTime::printOn(ostream& strm) const
387 register unsigned hh = GetHour();
388 wxDate(*this).printOn(strm);
389 strm << ' ' << ((hh <= 12) ? hh : hh-12) << ':'
390 << setfill('0') << setw(2) << GetMinute() << ':'
391 << setfill('0') << setw(2) << GetSecond() << ' ';
392 if (hh < 12) strm << _("am");
393 else strm << _("pm");
396 wxTime::wxTime(OIOin& strm)
403 void wxTime::storer(OIOout& strm) const
410 wxTime::wxTime(OIOifd& fd)
417 void wxTime::storer(OIOofd& fd) const