]>
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 #if !USE_SHARED_LIBRARY
49 IMPLEMENT_DYNAMIC_CLASS(wxTime
, wxObject
)
53 extern bool wxGetLocalTime(long *timeZone
, int *dstObserved
);
54 extern long wxGetCurrentTime(void);
56 static long TIME_ZONE
; /* seconds west of GMT */
57 static int DST_OBSERVED
; /* flags U.S. daylight saving time observed */
59 static bool wxTimeInitialized
= FALSE
;
61 wxTime::tFormat
wxTime::Format
= wxTime::wx12h
;
62 wxTime::tPrecision
wxTime::Precision
= wxTime::wxStdMinSec
;
64 static const unsigned long seconds_in_day
= 24*60*60L;
65 static const wxDate
refDate(1,1,1901);
66 // static const wxDate maxDate(49709L); /* ((2**32)-1)/seconds_in_day -1 */
68 wxTime
wxTime::GetLocalTime(const wxDate
& date
, hourTy h
, minuteTy m
, secondTy s
)
70 Return a local wxTime for the specified Standard Time date, hour, minute,
74 if (!wxTimeInitialized
)
76 wxGetLocalTime(&TIME_ZONE
, &DST_OBSERVED
);
77 wxTimeInitialized
= TRUE
;
80 if (!date.IsBetween(refDate,maxDate))
81 setError(NIHCL_DATERANGE,DEFAULT,
82 date.dayOfMonth(),date.nameOfMonth(),date.year());
84 // The following line causes an error in GCC 2.1
85 // long daysBetween = date-refDate;
86 // ... but this seems to get round it.
89 long daysBetween
= tmp1
- tmp2
;
91 return wxTime(seconds_in_day
*daysBetween
+ 60*60L*h
+ 60*m
+ s
);
96 Construct a wxTime for this instant.
99 if (!wxTimeInitialized
)
101 wxGetLocalTime(&TIME_ZONE
, &DST_OBSERVED
);
102 wxTimeInitialized
= TRUE
;
104 sec
= wxGetCurrentTime();
106 sec
+= (unsigned long) 2177452800; /* seconds from 1/1/01 to 1/1/70 */
108 sec
+= 2177452800UL; /* seconds from 1/1/01 to 1/1/70 */
112 wxTime::wxTime(hourTy h
, minuteTy m
, secondTy s
, bool dst
)
114 Construct a wxTime for today at the specified (local) hour, minute, and
118 if (!wxTimeInitialized
)
120 wxGetLocalTime(&TIME_ZONE
, &DST_OBSERVED
);
121 wxTimeInitialized
= TRUE
;
124 sec
= wxTime(wxDate(),h
,m
,s
,dst
).sec
;
128 wxTime::wxTime(const wxDate
& date
, hourTy h
, minuteTy m
, secondTy s
, bool dst
)
130 Construct a wxTime for the specified (local) Date, hour, minute, and
134 if (!wxTimeInitialized
)
136 wxGetLocalTime(&TIME_ZONE
, &DST_OBSERVED
);
137 wxTimeInitialized
= TRUE
;
139 sec
= GetLocalTime(date
,h
,m
,s
).sec
-3600;
143 if (IsDST() || dst
) sec
-= 3600;
149 if (IsDST()) setError(NIHCL_BADTIME,DEFAULT,
150 date.dayOfMonth(),date.nameOfMonth(),date.year(),
151 h,m,s,(dst?_("DST"):""));
154 sec
+= TIME_ZONE
; // adjust to GMT
158 wxTime::operator wxDate() const
160 Convert a wxTime to a local wxDate
163 // return wxDate((int)(GetLocalTime().sec/seconds_in_day)); 4.2 cc bug
164 long daycount
= (long)(GetLocalTime().sec
/seconds_in_day
);
166 wxDate
date(1,1,1901);
172 bool wxTime::IsBetween(const wxTime
& a
, const wxTime
& b
) const
174 return *this >= a
&& *this <= b
;
177 hourTy
wxTime::GetHour() const
179 Return the hour of this wxTime in local time; i.e., adjust for
180 time zone and Daylight Savings Time.
183 return GetLocalTime().GetHourGMT();
186 hourTy
wxTime::GetHourGMT() const
188 Return the hour of this Time in GMT.
191 return (hourTy
)((sec
% 86400) / 3600);
194 wxTime
wxTime::GetBeginDST(unsigned year
)
196 Return the local Standard Time at which Daylight Savings Time
197 begins in the specified year.
201 wxTime
DSTtime(GetLocalTime(wxDate(3,31,year
).Previous(1)+7,2));
204 DSTtime
= GetLocalTime(wxDate(4,30,year
).Previous(1),2);
205 if (year
==1974) DSTtime
= GetLocalTime(wxDate(1,6,1974),2);
206 if (year
==1975) DSTtime
= GetLocalTime(wxDate(2,23,1975),2);
211 wxTime
wxTime::GetEndDST(unsigned year
)
213 Return the local Standard Time at which Daylight Savings Time
214 ends in the specified year.
217 wxTime
STDtime(GetLocalTime(wxDate(10,31,year
).Previous(1),2-1));
221 bool wxTime::IsDST() const
223 Return TRUE if this local Standard Time should be adjusted
224 for Daylight Savings Time.
227 long daycount
= (long)(sec
/seconds_in_day
);
229 // At this point, daycount is the number of days from 1/1/1901.
230 // Need to convert to julian date (which starts at 1/1/4713 B.C.)
231 wxDate
date(1,1,1901);
234 unsigned year
= date
.GetYear();
237 if (*this >= GetBeginDST(year
))
238 if (*this < GetEndDST(year
)) return TRUE
;
243 wxTime
wxTime::GetLocalTime() const
245 Adjusts this GM Time for local time zone and Daylight Savings Time.
248 wxTime
local_time(sec
-TIME_ZONE
);
249 if (local_time
.IsDST()) local_time
.sec
+= 3600;
253 minuteTy
wxTime::GetMinute() const
255 Return the minute of this wxTime in local time; i.e., adjust
256 for time zone and Daylight Savings Time.
259 return GetLocalTime().GetMinuteGMT();
262 minuteTy
wxTime::GetMinuteGMT() const
264 Return the minute of this wxTime in GMT.
267 return (minuteTy
)(((sec
% 86400) % 3600) / 60);
270 secondTy
wxTime::GetSecond() const
272 Return the second of this wxTime.
275 return (secondTy
)(((sec
% 86400) % 3600) % 60);
278 secondTy
wxTime::GetSecondGMT() const
280 Return the minute of this wxTime in GMT.
283 return (secondTy
)(((sec
% 86400) % 3600) % 60);
286 int wxTime::GetDay() const
288 wxDate
da((wxDate
) *this);
292 int wxTime::GetDayOfWeek() const
294 wxDate
da((wxDate
) *this);
295 return da
.GetDayOfWeek();
298 int wxTime::GetMonth() const
300 wxDate
da((wxDate
) *this);
301 return da
.GetMonth();
304 int wxTime::GetYear() const
306 wxDate
da((wxDate
) *this);
310 wxTime
wxTime::Max(const wxTime
& t
) const
312 if (t
< *this) return *this;
316 wxTime
wxTime::Min(const wxTime
& t
) const
318 if (t
> *this) return *this;
323 wxTime::operator wxChar
*(void)
329 void wxTime::SetFormat(const wxTime::tFormat lFormat
,
330 const wxTime::tPrecision lPrecision
) {
332 wxTime::Format
= lFormat
;
333 wxTime::Precision
= lPrecision
;
336 wxChar
*wxTime::FormatTime() const {
337 static wxChar timeBuf
[30];
338 unsigned hh(GetHour());
350 wxSprintf(timeBuf
,wxT("%2d:%02d:%02d"),hh
,GetMinute(),GetSecond());
353 wxSprintf(timeBuf
,wxT("%2d:%02d"),hh
,GetMinute());
359 wxStrcat(timeBuf
,_("am"));
361 wxStrcat(timeBuf
,_("pm"));
367 int wxTime::compare(const Object& ob) const
369 assertArgSpecies(ob,classDesc,"compare");
370 register clockTy t = castdown(ob).sec;
371 if (sec < t) return -1;
372 if (sec > t) return 1;
376 void wxTime::deepenShallowCopy() {}
378 unsigned wxTime::hash() const { return sec; }
380 bool wxTime::isEqual(const Object& ob) const
382 return ob.isSpecies(classDesc) && *this==castdown(ob);
385 const Class* wxTime::species() const { return &classDesc; }
387 void wxTime::printOn(ostream& strm) const
389 register unsigned hh = GetHour();
390 wxDate(*this).printOn(strm);
391 strm << ' ' << ((hh <= 12) ? hh : hh-12) << ':'
392 << setfill('0') << setw(2) << GetMinute() << ':'
393 << setfill('0') << setw(2) << GetSecond() << ' ';
394 if (hh < 12) strm << _("am");
395 else strm << _("pm");
398 wxTime::wxTime(OIOin& strm)
405 void wxTime::storer(OIOout& strm) const
412 wxTime::wxTime(OIOifd& fd)
419 void wxTime::storer(OIOofd& fd) const