]>
git.saurik.com Git - wxWidgets.git/blob - src/common/time.cpp
d709f8b21286364cc140db034b1b302174b3165b
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();
108 sec
+= (unsigned long) 2177452800; /* seconds from 1/1/01 to 1/1/70 */
110 sec
+= 2177452800UL; /* seconds from 1/1/01 to 1/1/70 */
114 wxTime::wxTime(hourTy h
, minuteTy m
, secondTy s
, bool dst
)
116 Construct a wxTime for today at the specified (local) hour, minute, and
120 if (!wxTimeInitialized
)
122 wxGetLocalTime(&TIME_ZONE
, &DST_OBSERVED
);
123 wxTimeInitialized
= TRUE
;
126 sec
= wxTime(wxDate(),h
,m
,s
,dst
).sec
;
130 wxTime::wxTime(const wxDate
& date
, hourTy h
, minuteTy m
, secondTy s
, bool dst
)
132 Construct a wxTime for the specified (local) Date, hour, minute, and
136 if (!wxTimeInitialized
)
138 wxGetLocalTime(&TIME_ZONE
, &DST_OBSERVED
);
139 wxTimeInitialized
= TRUE
;
141 sec
= GetLocalTime(date
,h
,m
,s
).sec
-3600;
145 if (IsDST() || dst
) sec
-= 3600;
151 if (IsDST()) setError(NIHCL_BADTIME,DEFAULT,
152 date.dayOfMonth(),date.nameOfMonth(),date.year(),
153 h,m,s,(dst?_("DST"):""));
156 sec
+= TIME_ZONE
; // adjust to GMT
160 wxTime::operator wxDate() const
162 Convert a wxTime to a local wxDate
165 // return wxDate((int)(GetLocalTime().sec/seconds_in_day)); 4.2 cc bug
166 long daycount
= (long)(GetLocalTime().sec
/seconds_in_day
);
168 wxDate
date(1,1,1901);
174 bool wxTime::IsBetween(const wxTime
& a
, const wxTime
& b
) const
176 return *this >= a
&& *this <= b
;
179 hourTy
wxTime::GetHour() const
181 Return the hour of this wxTime in local time; i.e., adjust for
182 time zone and Daylight Savings Time.
185 return GetLocalTime().GetHourGMT();
188 hourTy
wxTime::GetHourGMT() const
190 Return the hour of this Time in GMT.
193 return (hourTy
)((sec
% 86400) / 3600);
196 wxTime
wxTime::GetBeginDST(unsigned year
)
198 Return the local Standard Time at which Daylight Savings Time
199 begins in the specified year.
203 wxTime
DSTtime(GetLocalTime(wxDate(3,31,year
).Previous(1)+7,2));
206 DSTtime
= GetLocalTime(wxDate(4,30,year
).Previous(1),2);
207 if (year
==1974) DSTtime
= GetLocalTime(wxDate(1,6,1974),2);
208 if (year
==1975) DSTtime
= GetLocalTime(wxDate(2,23,1975),2);
213 wxTime
wxTime::GetEndDST(unsigned year
)
215 Return the local Standard Time at which Daylight Savings Time
216 ends in the specified year.
219 wxTime
STDtime(GetLocalTime(wxDate(10,31,year
).Previous(1),2-1));
223 bool wxTime::IsDST() const
225 Return TRUE if this local Standard Time should be adjusted
226 for Daylight Savings Time.
229 long daycount
= (long)(sec
/seconds_in_day
);
231 // At this point, daycount is the number of days from 1/1/1901.
232 // Need to convert to julian date (which starts at 1/1/4713 B.C.)
233 wxDate
date(1,1,1901);
236 unsigned year
= date
.GetYear();
239 if (*this >= GetBeginDST(year
))
240 if (*this < GetEndDST(year
)) return TRUE
;
245 wxTime
wxTime::GetLocalTime() const
247 Adjusts this GM Time for local time zone and Daylight Savings Time.
250 wxTime
local_time(sec
-TIME_ZONE
);
251 if (local_time
.IsDST()) local_time
.sec
+= 3600;
255 minuteTy
wxTime::GetMinute() const
257 Return the minute of this wxTime in local time; i.e., adjust
258 for time zone and Daylight Savings Time.
261 return GetLocalTime().GetMinuteGMT();
264 minuteTy
wxTime::GetMinuteGMT() const
266 Return the minute of this wxTime in GMT.
269 return (minuteTy
)(((sec
% 86400) % 3600) / 60);
272 secondTy
wxTime::GetSecond() const
274 Return the second of this wxTime.
277 return (secondTy
)(((sec
% 86400) % 3600) % 60);
280 secondTy
wxTime::GetSecondGMT() const
282 Return the minute of this wxTime in GMT.
285 return (secondTy
)(((sec
% 86400) % 3600) % 60);
288 int wxTime::GetDay() const
290 wxDate
da((wxDate
) *this);
294 int wxTime::GetDayOfWeek() const
296 wxDate
da((wxDate
) *this);
297 return da
.GetDayOfWeek();
300 int wxTime::GetMonth() const
302 wxDate
da((wxDate
) *this);
303 return da
.GetMonth();
306 int wxTime::GetYear() const
308 wxDate
da((wxDate
) *this);
312 wxTime
wxTime::Max(const wxTime
& t
) const
314 if (t
< *this) return *this;
318 wxTime
wxTime::Min(const wxTime
& t
) const
320 if (t
> *this) return *this;
325 wxTime::operator char *(void)
331 void wxTime::SetFormat(const wxTime::tFormat lFormat
,
332 const wxTime::tPrecision lPrecision
) {
334 wxTime::Format
= lFormat
;
335 wxTime::Precision
= lPrecision
;
338 char *wxTime::FormatTime() const {
339 static char timeBuf
[30];
340 unsigned hh(GetHour());
352 sprintf(timeBuf
,"%2d:%02d:%02d",hh
,GetMinute(),GetSecond());
355 sprintf(timeBuf
,"%2d:%02d",hh
,GetMinute());
361 strcat(timeBuf
,_("am"));
363 strcat(timeBuf
,_("pm"));
369 int wxTime::compare(const Object& ob) const
371 assertArgSpecies(ob,classDesc,"compare");
372 register clockTy t = castdown(ob).sec;
373 if (sec < t) return -1;
374 if (sec > t) return 1;
378 void wxTime::deepenShallowCopy() {}
380 unsigned wxTime::hash() const { return sec; }
382 bool wxTime::isEqual(const Object& ob) const
384 return ob.isSpecies(classDesc) && *this==castdown(ob);
387 const Class* wxTime::species() const { return &classDesc; }
389 void wxTime::printOn(ostream& strm) const
391 register unsigned hh = GetHour();
392 wxDate(*this).printOn(strm);
393 strm << ' ' << ((hh <= 12) ? hh : hh-12) << ':'
394 << setfill('0') << setw(2) << GetMinute() << ':'
395 << setfill('0') << setw(2) << GetSecond() << ' ';
396 if (hh < 12) strm << _("am");
397 else strm << _("pm");
400 wxTime::wxTime(OIOin& strm)
407 void wxTime::storer(OIOout& strm) const
414 wxTime::wxTime(OIOifd& fd)
421 void wxTime::storer(OIOofd& fd) const