]>
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"
50 #if !USE_SHARED_LIBRARY
51 IMPLEMENT_DYNAMIC_CLASS(wxTime
, wxObject
)
55 extern bool wxGetLocalTime(long *timeZone
, int *dstObserved
);
56 extern long wxGetCurrentTime(void);
58 static long TIME_ZONE
; /* seconds west of GMT */
59 static int DST_OBSERVED
; /* flags U.S. daylight saving time observed */
61 static bool wxTimeInitialized
= FALSE
;
63 wxTime::tFormat
wxTime::Format
= wxTime::wx12h
;
64 wxTime::tPrecision
wxTime::Precision
= wxTime::wxStdMinSec
;
66 static const unsigned long seconds_in_day
= 24*60*60L;
67 static const wxDate
refDate(1,1,1901);
68 // static const wxDate maxDate(49709L); /* ((2**32)-1)/seconds_in_day -1 */
70 wxTime
wxTime::GetLocalTime(const wxDate
& date
, hourTy h
, minuteTy m
, secondTy s
)
72 Return a local wxTime for the specified Standard Time date, hour, minute,
76 if (!wxTimeInitialized
)
78 wxGetLocalTime(&TIME_ZONE
, &DST_OBSERVED
);
79 wxTimeInitialized
= TRUE
;
82 if (!date.IsBetween(refDate,maxDate))
83 setError(NIHCL_DATERANGE,DEFAULT,
84 date.dayOfMonth(),date.nameOfMonth(),date.year());
86 // The following line causes an error in GCC 2.1
87 // long daysBetween = date-refDate;
88 // ... but this seems to get round it.
91 long daysBetween
= tmp1
- tmp2
;
93 return wxTime(seconds_in_day
*daysBetween
+ 60*60L*h
+ 60*m
+ s
);
98 Construct a wxTime for this instant.
101 if (!wxTimeInitialized
)
103 wxGetLocalTime(&TIME_ZONE
, &DST_OBSERVED
);
104 wxTimeInitialized
= TRUE
;
106 sec
= wxGetCurrentTime();
107 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
155 wxTime::operator wxDate() const
157 Convert a wxTime to a local wxDate
160 // return wxDate((int)(GetLocalTime().sec/seconds_in_day)); 4.2 cc bug
161 long daycount
= (long)(GetLocalTime().sec
/seconds_in_day
);
163 wxDate
date(1,1,1901);
168 bool wxTime::IsBetween(const wxTime
& a
, const wxTime
& b
) const
170 return *this >= a
&& *this <= b
;
173 hourTy
wxTime::GetHour() const
175 Return the hour of this wxTime in local time; i.e., adjust for
176 time zone and Daylight Savings Time.
179 return GetLocalTime().GetHourGMT();
182 hourTy
wxTime::GetHourGMT() const
184 Return the hour of this Time in GMT.
187 return (hourTy
)((sec
% 86400) / 3600);
190 wxTime
wxTime::GetBeginDST(unsigned year
)
192 Return the local Standard Time at which Daylight Savings Time
193 begins in the specified year.
197 wxTime
DSTtime(GetLocalTime(wxDate(3,31,year
).Previous(1)+7,2));
200 DSTtime
= GetLocalTime(wxDate(4,30,year
).Previous(1),2);
201 if (year
==1974) DSTtime
= GetLocalTime(wxDate(1,6,1974),2);
202 if (year
==1975) DSTtime
= GetLocalTime(wxDate(2,23,1975),2);
207 wxTime
wxTime::GetEndDST(unsigned year
)
209 Return the local Standard Time at which Daylight Savings Time
210 ends in the specified year.
213 wxTime
STDtime(GetLocalTime(wxDate(10,31,year
).Previous(1),2-1));
217 bool wxTime::IsDST() const
219 Return TRUE if this local Standard Time should be adjusted
220 for Daylight Savings Time.
223 long daycount
= (long)(sec
/seconds_in_day
);
225 // At this point, daycount is the number of days from 1/1/1901.
226 // Need to convert to julian date (which starts at 1/1/4713 B.C.)
227 wxDate
date(1,1,1901);
230 unsigned year
= date
.GetYear();
233 if (*this >= GetBeginDST(year
))
234 if (*this < GetEndDST(year
)) return TRUE
;
239 wxTime
wxTime::GetLocalTime() const
241 Adjusts this GM Time for local time zone and Daylight Savings Time.
244 wxTime
local_time(sec
-TIME_ZONE
);
245 if (local_time
.IsDST()) local_time
.sec
+= 3600;
249 minuteTy
wxTime::GetMinute() const
251 Return the minute of this wxTime in local time; i.e., adjust
252 for time zone and Daylight Savings Time.
255 return GetLocalTime().GetMinuteGMT();
258 minuteTy
wxTime::GetMinuteGMT() const
260 Return the minute of this wxTime in GMT.
263 return (minuteTy
)(((sec
% 86400) % 3600) / 60);
266 secondTy
wxTime::GetSecond() const
268 Return the second of this wxTime.
271 return (secondTy
)(((sec
% 86400) % 3600) % 60);
274 wxTime
wxTime::Max(const wxTime
& t
) const
276 if (t
< *this) return *this;
280 wxTime
wxTime::Min(const wxTime
& t
) const
282 if (t
> *this) return *this;
286 wxTime::operator char *(void)
291 void wxTime::SetFormat(const wxTime::tFormat lFormat
,
292 const wxTime::tPrecision lPrecision
) {
294 wxTime::Format
= lFormat
;
295 wxTime::Precision
= lPrecision
;
298 char *wxTime::FormatTime() const {
299 static char timeBuf
[30];
300 unsigned hh(GetHour());
312 sprintf(timeBuf
,"%2d:%02d:%02d",hh
,GetMinute(),GetSecond());
315 sprintf(timeBuf
,"%2d:%02d",hh
,GetMinute());
321 strcat(timeBuf
,_("am"));
323 strcat(timeBuf
,_("pm"));
329 int wxTime::compare(const Object& ob) const
331 assertArgSpecies(ob,classDesc,"compare");
332 register clockTy t = castdown(ob).sec;
333 if (sec < t) return -1;
334 if (sec > t) return 1;
338 void wxTime::deepenShallowCopy() {}
340 unsigned wxTime::hash() const { return sec; }
342 bool wxTime::isEqual(const Object& ob) const
344 return ob.isSpecies(classDesc) && *this==castdown(ob);
347 const Class* wxTime::species() const { return &classDesc; }
349 void wxTime::printOn(ostream& strm) const
351 register unsigned hh = GetHour();
352 wxDate(*this).printOn(strm);
353 strm << ' ' << ((hh <= 12) ? hh : hh-12) << ':'
354 << setfill('0') << setw(2) << GetMinute() << ':'
355 << setfill('0') << setw(2) << GetSecond() << ' ';
356 if (hh < 12) strm << _("am");
357 else strm << _("pm");
360 wxTime::wxTime(OIOin& strm)
367 void wxTime::storer(OIOout& strm) const
374 wxTime::wxTime(OIOifd& fd)
381 void wxTime::storer(OIOofd& fd) const