]>
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"
45 #if !USE_SHARED_LIBRARY
46 IMPLEMENT_DYNAMIC_CLASS(wxTime
, wxObject
)
50 extern bool wxGetLocalTime(long *timeZone
, int *dstObserved
);
51 extern long wxGetCurrentTime(void);
53 static long TIME_ZONE
; /* seconds west of GMT */
54 static int DST_OBSERVED
; /* flags U.S. daylight saving time observed */
56 static bool wxTimeInitialized
= FALSE
;
58 wxTime::tFormat
wxTime::Format
= wxTime::wx12h
;
59 wxTime::tPrecision
wxTime::Precision
= wxTime::wxStdMinSec
;
61 static const unsigned long seconds_in_day
= 24*60*60L;
62 static const wxDate
refDate(1,1,1901);
63 // static const wxDate maxDate(49709L); /* ((2**32)-1)/seconds_in_day -1 */
65 wxTime
wxTime::GetLocalTime(const wxDate
& date
, hourTy h
, minuteTy m
, secondTy s
)
67 Return a local wxTime for the specified Standard Time date, hour, minute,
71 if (!wxTimeInitialized
)
73 wxGetLocalTime(&TIME_ZONE
, &DST_OBSERVED
);
74 wxTimeInitialized
= TRUE
;
77 if (!date.IsBetween(refDate,maxDate))
78 setError(NIHCL_DATERANGE,DEFAULT,
79 date.dayOfMonth(),date.nameOfMonth(),date.year());
81 // The following line causes an error in GCC 2.1
82 // long daysBetween = date-refDate;
83 // ... but this seems to get round it.
86 long daysBetween
= tmp1
- tmp2
;
88 return wxTime(seconds_in_day
*daysBetween
+ 60*60L*h
+ 60*m
+ s
);
93 Construct a wxTime for this instant.
96 if (!wxTimeInitialized
)
98 wxGetLocalTime(&TIME_ZONE
, &DST_OBSERVED
);
99 wxTimeInitialized
= TRUE
;
101 sec
= wxGetCurrentTime();
102 sec
+= 2177452800UL; /* seconds from 1/1/01 to 1/1/70 */
105 wxTime::wxTime(hourTy h
, minuteTy m
, secondTy s
, bool dst
)
107 Construct a wxTime for today at the specified (local) hour, minute, and
111 if (!wxTimeInitialized
)
113 wxGetLocalTime(&TIME_ZONE
, &DST_OBSERVED
);
114 wxTimeInitialized
= TRUE
;
117 sec
= wxTime(wxDate(),h
,m
,s
,dst
).sec
;
121 wxTime::wxTime(const wxDate
& date
, hourTy h
, minuteTy m
, secondTy s
, bool dst
)
123 Construct a wxTime for the specified (local) Date, hour, minute, and
127 if (!wxTimeInitialized
)
129 wxGetLocalTime(&TIME_ZONE
, &DST_OBSERVED
);
130 wxTimeInitialized
= TRUE
;
132 sec
= GetLocalTime(date
,h
,m
,s
).sec
-3600;
136 if (IsDST() || dst
) sec
-= 3600;
142 if (IsDST()) setError(NIHCL_BADTIME,DEFAULT,
143 date.dayOfMonth(),date.nameOfMonth(),date.year(),
144 h,m,s,(dst?"DST":""));
147 sec
+= TIME_ZONE
; // adjust to GMT
150 wxTime::operator wxDate() const
152 Convert a wxTime to a local wxDate
155 // return wxDate((int)(GetLocalTime().sec/seconds_in_day)); 4.2 cc bug
156 long daycount
= (long)(GetLocalTime().sec
/seconds_in_day
);
158 wxDate
date(1,1,1901);
163 bool wxTime::IsBetween(const wxTime
& a
, const wxTime
& b
) const
165 return *this >= a
&& *this <= b
;
168 hourTy
wxTime::GetHour() const
170 Return the hour of this wxTime in local time; i.e., adjust for
171 time zone and Daylight Savings Time.
174 return GetLocalTime().GetHourGMT();
177 hourTy
wxTime::GetHourGMT() const
179 Return the hour of this Time in GMT.
182 return (hourTy
)((sec
% 86400) / 3600);
185 wxTime
wxTime::GetBeginDST(unsigned year
)
187 Return the local Standard Time at which Daylight Savings Time
188 begins in the specified year.
192 wxTime
DSTtime(GetLocalTime(wxDate(3,31,year
).Previous(1)+7,2));
195 DSTtime
= GetLocalTime(wxDate(4,30,year
).Previous(1),2);
196 if (year
==1974) DSTtime
= GetLocalTime(wxDate(1,6,1974),2);
197 if (year
==1975) DSTtime
= GetLocalTime(wxDate(2,23,1975),2);
202 wxTime
wxTime::GetEndDST(unsigned year
)
204 Return the local Standard Time at which Daylight Savings Time
205 ends in the specified year.
208 wxTime
STDtime(GetLocalTime(wxDate(10,31,year
).Previous(1),2-1));
212 bool wxTime::IsDST() const
214 Return TRUE if this local Standard Time should be adjusted
215 for Daylight Savings Time.
218 long daycount
= (long)(sec
/seconds_in_day
);
220 // At this point, daycount is the number of days from 1/1/1901.
221 // Need to convert to julian date (which starts at 1/1/4713 B.C.)
222 wxDate
date(1,1,1901);
225 unsigned year
= date
.GetYear();
228 if (*this >= GetBeginDST(year
))
229 if (*this < GetEndDST(year
)) return TRUE
;
234 wxTime
wxTime::GetLocalTime() const
236 Adjusts this GM Time for local time zone and Daylight Savings Time.
239 wxTime
local_time(sec
-TIME_ZONE
);
240 if (local_time
.IsDST()) local_time
.sec
+= 3600;
244 minuteTy
wxTime::GetMinute() const
246 Return the minute of this wxTime in local time; i.e., adjust
247 for time zone and Daylight Savings Time.
250 return GetLocalTime().GetMinuteGMT();
253 minuteTy
wxTime::GetMinuteGMT() const
255 Return the minute of this wxTime in GMT.
258 return (minuteTy
)(((sec
% 86400) % 3600) / 60);
261 secondTy
wxTime::GetSecond() const
263 Return the second of this wxTime.
266 return (secondTy
)(((sec
% 86400) % 3600) % 60);
269 wxTime
wxTime::Max(const wxTime
& t
) const
271 if (t
< *this) return *this;
275 wxTime
wxTime::Min(const wxTime
& t
) const
277 if (t
> *this) return *this;
281 wxTime::operator char *(void)
286 void wxTime::SetFormat(const wxTime::tFormat lFormat
,
287 const wxTime::tPrecision lPrecision
) {
289 wxTime::Format
= lFormat
;
290 wxTime::Precision
= lPrecision
;
293 char *wxTime::FormatTime() const {
294 static char timeBuf
[30];
295 unsigned hh(GetHour());
307 sprintf(timeBuf
,"%2d:%02d:%02d",hh
,GetMinute(),GetSecond());
310 sprintf(timeBuf
,"%2d:%02d",hh
,GetMinute());
316 strcat(timeBuf
,"am");
318 strcat(timeBuf
,"pm");
324 int wxTime::compare(const Object& ob) const
326 assertArgSpecies(ob,classDesc,"compare");
327 register clockTy t = castdown(ob).sec;
328 if (sec < t) return -1;
329 if (sec > t) return 1;
333 void wxTime::deepenShallowCopy() {}
335 unsigned wxTime::hash() const { return sec; }
337 bool wxTime::isEqual(const Object& ob) const
339 return ob.isSpecies(classDesc) && *this==castdown(ob);
342 const Class* wxTime::species() const { return &classDesc; }
344 void wxTime::printOn(ostream& strm) const
346 register unsigned hh = GetHour();
347 wxDate(*this).printOn(strm);
348 strm << ' ' << ((hh <= 12) ? hh : hh-12) << ':'
349 << setfill('0') << setw(2) << GetMinute() << ':'
350 << setfill('0') << setw(2) << GetSecond() << ' ';
351 if (hh < 12) strm << "am";
355 wxTime::wxTime(OIOin& strm)
362 void wxTime::storer(OIOout& strm) const
369 wxTime::wxTime(OIOifd& fd)
376 void wxTime::storer(OIOofd& fd) const