]>
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 #include "wx/ioswrap.h"
47 #if !USE_SHARED_LIBRARY
48 IMPLEMENT_DYNAMIC_CLASS(wxTime
, wxObject
)
52 extern bool wxGetLocalTime(long *timeZone
, int *dstObserved
);
53 extern long wxGetCurrentTime(void);
55 static long TIME_ZONE
; /* seconds west of GMT */
56 static int DST_OBSERVED
; /* flags U.S. daylight saving time observed */
58 static bool wxTimeInitialized
= FALSE
;
60 wxTime::tFormat
wxTime::Format
= wxTime::wx12h
;
61 wxTime::tPrecision
wxTime::Precision
= wxTime::wxStdMinSec
;
63 static const unsigned long seconds_in_day
= 24*60*60L;
64 static const wxDate
refDate(1,1,1901);
65 // static const wxDate maxDate(49709L); /* ((2**32)-1)/seconds_in_day -1 */
67 wxTime
wxTime::GetLocalTime(const wxDate
& date
, hourTy h
, minuteTy m
, secondTy s
)
69 Return a local wxTime for the specified Standard Time date, hour, minute,
73 if (!wxTimeInitialized
)
75 wxGetLocalTime(&TIME_ZONE
, &DST_OBSERVED
);
76 wxTimeInitialized
= TRUE
;
79 if (!date.IsBetween(refDate,maxDate))
80 setError(NIHCL_DATERANGE,DEFAULT,
81 date.dayOfMonth(),date.nameOfMonth(),date.year());
83 // The following line causes an error in GCC 2.1
84 // long daysBetween = date-refDate;
85 // ... but this seems to get round it.
88 long daysBetween
= tmp1
- tmp2
;
90 return wxTime(seconds_in_day
*daysBetween
+ 60*60L*h
+ 60*m
+ s
);
95 Construct a wxTime for this instant.
98 if (!wxTimeInitialized
)
100 wxGetLocalTime(&TIME_ZONE
, &DST_OBSERVED
);
101 wxTimeInitialized
= TRUE
;
103 sec
= wxGetCurrentTime();
105 sec
+= (unsigned long) 2177452800; /* seconds from 1/1/01 to 1/1/70 */
107 sec
+= 2177452800UL; /* seconds from 1/1/01 to 1/1/70 */
111 wxTime::wxTime(hourTy h
, minuteTy m
, secondTy s
, bool dst
)
113 Construct a wxTime for today at the specified (local) hour, minute, and
117 if (!wxTimeInitialized
)
119 wxGetLocalTime(&TIME_ZONE
, &DST_OBSERVED
);
120 wxTimeInitialized
= TRUE
;
123 sec
= wxTime(wxDate(),h
,m
,s
,dst
).sec
;
127 wxTime::wxTime(const wxDate
& date
, hourTy h
, minuteTy m
, secondTy s
, bool dst
)
129 Construct a wxTime for the specified (local) Date, hour, minute, and
133 if (!wxTimeInitialized
)
135 wxGetLocalTime(&TIME_ZONE
, &DST_OBSERVED
);
136 wxTimeInitialized
= TRUE
;
138 sec
= GetLocalTime(date
,h
,m
,s
).sec
-3600;
142 if (IsDST() || dst
) sec
-= 3600;
148 if (IsDST()) setError(NIHCL_BADTIME,DEFAULT,
149 date.dayOfMonth(),date.nameOfMonth(),date.year(),
150 h,m,s,(dst?_("DST"):""));
153 sec
+= TIME_ZONE
; // adjust to GMT
157 wxTime::operator wxDate() const
159 Convert a wxTime to a local wxDate
162 // return wxDate((int)(GetLocalTime().sec/seconds_in_day)); 4.2 cc bug
163 long daycount
= (long)(GetLocalTime().sec
/seconds_in_day
);
165 wxDate
date(1,1,1901);
171 bool wxTime::IsBetween(const wxTime
& a
, const wxTime
& b
) const
173 return *this >= a
&& *this <= b
;
176 hourTy
wxTime::GetHour() const
178 Return the hour of this wxTime in local time; i.e., adjust for
179 time zone and Daylight Savings Time.
182 return GetLocalTime().GetHourGMT();
185 hourTy
wxTime::GetHourGMT() const
187 Return the hour of this Time in GMT.
190 return (hourTy
)((sec
% 86400) / 3600);
193 wxTime
wxTime::GetBeginDST(unsigned year
)
195 Return the local Standard Time at which Daylight Savings Time
196 begins in the specified year.
200 wxTime
DSTtime(GetLocalTime(wxDate(3,31,year
).Previous(1)+7,2));
203 DSTtime
= GetLocalTime(wxDate(4,30,year
).Previous(1),2);
204 if (year
==1974) DSTtime
= GetLocalTime(wxDate(1,6,1974),2);
205 if (year
==1975) DSTtime
= GetLocalTime(wxDate(2,23,1975),2);
210 wxTime
wxTime::GetEndDST(unsigned year
)
212 Return the local Standard Time at which Daylight Savings Time
213 ends in the specified year.
216 wxTime
STDtime(GetLocalTime(wxDate(10,31,year
).Previous(1),2-1));
220 bool wxTime::IsDST() const
222 Return TRUE if this local Standard Time should be adjusted
223 for Daylight Savings Time.
226 long daycount
= (long)(sec
/seconds_in_day
);
228 // At this point, daycount is the number of days from 1/1/1901.
229 // Need to convert to julian date (which starts at 1/1/4713 B.C.)
230 wxDate
date(1,1,1901);
233 unsigned year
= date
.GetYear();
236 if (*this >= GetBeginDST(year
))
237 if (*this < GetEndDST(year
)) return TRUE
;
242 wxTime
wxTime::GetLocalTime() const
244 Adjusts this GM Time for local time zone and Daylight Savings Time.
247 wxTime
local_time(sec
-TIME_ZONE
);
248 if (local_time
.IsDST()) local_time
.sec
+= 3600;
252 minuteTy
wxTime::GetMinute() const
254 Return the minute of this wxTime in local time; i.e., adjust
255 for time zone and Daylight Savings Time.
258 return GetLocalTime().GetMinuteGMT();
261 minuteTy
wxTime::GetMinuteGMT() const
263 Return the minute of this wxTime in GMT.
266 return (minuteTy
)(((sec
% 86400) % 3600) / 60);
269 secondTy
wxTime::GetSecond() const
271 Return the second of this wxTime.
274 return (secondTy
)(((sec
% 86400) % 3600) % 60);
277 secondTy
wxTime::GetSecondGMT() const
279 Return the minute of this wxTime in GMT.
282 return (secondTy
)(((sec
% 86400) % 3600) % 60);
285 int wxTime::GetDay() const
287 wxDate
da((wxDate
) *this);
291 int wxTime::GetDayOfWeek() const
293 wxDate
da((wxDate
) *this);
294 return da
.GetDayOfWeek();
297 int wxTime::GetMonth() const
299 wxDate
da((wxDate
) *this);
300 return da
.GetMonth();
303 int wxTime::GetYear() const
305 wxDate
da((wxDate
) *this);
309 wxTime
wxTime::Max(const wxTime
& t
) const
311 if (t
< *this) return *this;
315 wxTime
wxTime::Min(const wxTime
& t
) const
317 if (t
> *this) return *this;
322 wxTime::operator wxChar
*(void)
328 void wxTime::SetFormat(const wxTime::tFormat lFormat
,
329 const wxTime::tPrecision lPrecision
) {
331 wxTime::Format
= lFormat
;
332 wxTime::Precision
= lPrecision
;
335 wxChar
*wxTime::FormatTime() const {
336 static wxChar timeBuf
[30];
337 unsigned hh(GetHour());
349 wxSprintf(timeBuf
,_T("%2d:%02d:%02d"),hh
,GetMinute(),GetSecond());
352 wxSprintf(timeBuf
,_T("%2d:%02d"),hh
,GetMinute());
358 wxStrcat(timeBuf
,_("am"));
360 wxStrcat(timeBuf
,_("pm"));
366 int wxTime::compare(const Object& ob) const
368 assertArgSpecies(ob,classDesc,"compare");
369 register clockTy t = castdown(ob).sec;
370 if (sec < t) return -1;
371 if (sec > t) return 1;
375 void wxTime::deepenShallowCopy() {}
377 unsigned wxTime::hash() const { return sec; }
379 bool wxTime::isEqual(const Object& ob) const
381 return ob.isSpecies(classDesc) && *this==castdown(ob);
384 const Class* wxTime::species() const { return &classDesc; }
386 void wxTime::printOn(ostream& strm) const
388 register unsigned hh = GetHour();
389 wxDate(*this).printOn(strm);
390 strm << ' ' << ((hh <= 12) ? hh : hh-12) << ':'
391 << setfill('0') << setw(2) << GetMinute() << ':'
392 << setfill('0') << setw(2) << GetSecond() << ' ';
393 if (hh < 12) strm << _("am");
394 else strm << _("pm");
397 wxTime::wxTime(OIOin& strm)
404 void wxTime::storer(OIOout& strm) const
411 wxTime::wxTime(OIOifd& fd)
418 void wxTime::storer(OIOofd& fd) const