]>
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"
46 #if !USE_SHARED_LIBRARY
47 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();
103 sec
+= 2177452800UL; /* seconds from 1/1/01 to 1/1/70 */
106 wxTime::wxTime(hourTy h
, minuteTy m
, secondTy s
, bool dst
)
108 Construct a wxTime for today at the specified (local) hour, minute, and
112 if (!wxTimeInitialized
)
114 wxGetLocalTime(&TIME_ZONE
, &DST_OBSERVED
);
115 wxTimeInitialized
= TRUE
;
118 sec
= wxTime(wxDate(),h
,m
,s
,dst
).sec
;
122 wxTime::wxTime(const wxDate
& date
, hourTy h
, minuteTy m
, secondTy s
, bool dst
)
124 Construct a wxTime for the specified (local) Date, hour, minute, and
128 if (!wxTimeInitialized
)
130 wxGetLocalTime(&TIME_ZONE
, &DST_OBSERVED
);
131 wxTimeInitialized
= TRUE
;
133 sec
= GetLocalTime(date
,h
,m
,s
).sec
-3600;
137 if (IsDST() || dst
) sec
-= 3600;
143 if (IsDST()) setError(NIHCL_BADTIME,DEFAULT,
144 date.dayOfMonth(),date.nameOfMonth(),date.year(),
145 h,m,s,(dst?_("DST"):""));
148 sec
+= TIME_ZONE
; // adjust to GMT
151 wxTime::operator wxDate() const
153 Convert a wxTime to a local wxDate
156 // return wxDate((int)(GetLocalTime().sec/seconds_in_day)); 4.2 cc bug
157 long daycount
= (long)(GetLocalTime().sec
/seconds_in_day
);
159 wxDate
date(1,1,1901);
164 bool wxTime::IsBetween(const wxTime
& a
, const wxTime
& b
) const
166 return *this >= a
&& *this <= b
;
169 hourTy
wxTime::GetHour() const
171 Return the hour of this wxTime in local time; i.e., adjust for
172 time zone and Daylight Savings Time.
175 return GetLocalTime().GetHourGMT();
178 hourTy
wxTime::GetHourGMT() const
180 Return the hour of this Time in GMT.
183 return (hourTy
)((sec
% 86400) / 3600);
186 wxTime
wxTime::GetBeginDST(unsigned year
)
188 Return the local Standard Time at which Daylight Savings Time
189 begins in the specified year.
193 wxTime
DSTtime(GetLocalTime(wxDate(3,31,year
).Previous(1)+7,2));
196 DSTtime
= GetLocalTime(wxDate(4,30,year
).Previous(1),2);
197 if (year
==1974) DSTtime
= GetLocalTime(wxDate(1,6,1974),2);
198 if (year
==1975) DSTtime
= GetLocalTime(wxDate(2,23,1975),2);
203 wxTime
wxTime::GetEndDST(unsigned year
)
205 Return the local Standard Time at which Daylight Savings Time
206 ends in the specified year.
209 wxTime
STDtime(GetLocalTime(wxDate(10,31,year
).Previous(1),2-1));
213 bool wxTime::IsDST() const
215 Return TRUE if this local Standard Time should be adjusted
216 for Daylight Savings Time.
219 long daycount
= (long)(sec
/seconds_in_day
);
221 // At this point, daycount is the number of days from 1/1/1901.
222 // Need to convert to julian date (which starts at 1/1/4713 B.C.)
223 wxDate
date(1,1,1901);
226 unsigned year
= date
.GetYear();
229 if (*this >= GetBeginDST(year
))
230 if (*this < GetEndDST(year
)) return TRUE
;
235 wxTime
wxTime::GetLocalTime() const
237 Adjusts this GM Time for local time zone and Daylight Savings Time.
240 wxTime
local_time(sec
-TIME_ZONE
);
241 if (local_time
.IsDST()) local_time
.sec
+= 3600;
245 minuteTy
wxTime::GetMinute() const
247 Return the minute of this wxTime in local time; i.e., adjust
248 for time zone and Daylight Savings Time.
251 return GetLocalTime().GetMinuteGMT();
254 minuteTy
wxTime::GetMinuteGMT() const
256 Return the minute of this wxTime in GMT.
259 return (minuteTy
)(((sec
% 86400) % 3600) / 60);
262 secondTy
wxTime::GetSecond() const
264 Return the second of this wxTime.
267 return (secondTy
)(((sec
% 86400) % 3600) % 60);
270 wxTime
wxTime::Max(const wxTime
& t
) const
272 if (t
< *this) return *this;
276 wxTime
wxTime::Min(const wxTime
& t
) const
278 if (t
> *this) return *this;
282 wxTime::operator char *(void)
287 void wxTime::SetFormat(const wxTime::tFormat lFormat
,
288 const wxTime::tPrecision lPrecision
) {
290 wxTime::Format
= lFormat
;
291 wxTime::Precision
= lPrecision
;
294 char *wxTime::FormatTime() const {
295 static char timeBuf
[30];
296 unsigned hh(GetHour());
308 sprintf(timeBuf
,"%2d:%02d:%02d",hh
,GetMinute(),GetSecond());
311 sprintf(timeBuf
,"%2d:%02d",hh
,GetMinute());
317 strcat(timeBuf
,_("am"));
319 strcat(timeBuf
,_("pm"));
325 int wxTime::compare(const Object& ob) const
327 assertArgSpecies(ob,classDesc,"compare");
328 register clockTy t = castdown(ob).sec;
329 if (sec < t) return -1;
330 if (sec > t) return 1;
334 void wxTime::deepenShallowCopy() {}
336 unsigned wxTime::hash() const { return sec; }
338 bool wxTime::isEqual(const Object& ob) const
340 return ob.isSpecies(classDesc) && *this==castdown(ob);
343 const Class* wxTime::species() const { return &classDesc; }
345 void wxTime::printOn(ostream& strm) const
347 register unsigned hh = GetHour();
348 wxDate(*this).printOn(strm);
349 strm << ' ' << ((hh <= 12) ? hh : hh-12) << ':'
350 << setfill('0') << setw(2) << GetMinute() << ':'
351 << setfill('0') << setw(2) << GetSecond() << ' ';
352 if (hh < 12) strm << _("am");
353 else strm << _("pm");
356 wxTime::wxTime(OIOin& strm)
363 void wxTime::storer(OIOout& strm) const
370 wxTime::wxTime(OIOifd& fd)
377 void wxTime::storer(OIOofd& fd) const