]>
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"
49 IMPLEMENT_DYNAMIC_CLASS(wxTime
, wxObject
)
53 #define WX_TIMEZONE _timezone
56 extern long wxGetUTCTime(void);
57 bool wxGetTZandDST(long *timeZone
, int *dstObserved
)
62 now
= time((time_t *) NULL
);
64 if (now
!= (time_t)-1)
68 if ((tm
) && (tm
->tm_isdst
> 0))
71 *timeZone
= WX_TIMEZONE
;
77 static long TIME_ZONE
; /* seconds west of GMT */
78 static int DST_OBSERVED
; /* flags U.S. daylight saving time observed */
80 static bool wxTimeInitialized
= FALSE
;
82 wxTime::tFormat
wxTime::Format
= wxTime::wx12h
;
83 wxTime::tPrecision
wxTime::Precision
= wxTime::wxStdMinSec
;
85 static const unsigned long seconds_in_day
= 24*60*60L;
86 static const wxDate
refDate(1,1,1901);
87 // static const wxDate maxDate(49709L); /* ((2**32)-1)/seconds_in_day -1 */
89 wxTime
wxTime::GetLocalTime(const wxDate
& date
, hourTy h
, minuteTy m
, secondTy s
)
91 Return a local wxTime for the specified Standard Time date, hour, minute,
95 if (!wxTimeInitialized
)
97 wxGetTZandDST(&TIME_ZONE
, &DST_OBSERVED
);
98 wxTimeInitialized
= TRUE
;
101 if (!date.IsBetween(refDate,maxDate))
102 setError(NIHCL_DATERANGE,DEFAULT,
103 date.dayOfMonth(),date.nameOfMonth(),date.year());
105 // The following line causes an error in GCC 2.1
106 // long daysBetween = date-refDate;
107 // ... but this seems to get round it.
109 wxDate
tmp2(refDate
);
110 long daysBetween
= tmp1
- tmp2
;
112 return wxTime(seconds_in_day
*daysBetween
+ 60*60L*h
+ 60*m
+ s
);
117 Construct a wxTime for this instant.
120 if (!wxTimeInitialized
)
122 wxGetTZandDST(&TIME_ZONE
, &DST_OBSERVED
);
123 wxTimeInitialized
= TRUE
;
125 sec
= wxGetUTCTime();
127 sec
+= (unsigned long) 2177452800; /* seconds from 1/1/01 to 1/1/70 */
129 sec
+= 2177452800UL; /* seconds from 1/1/01 to 1/1/70 */
133 wxTime::wxTime(hourTy h
, minuteTy m
, secondTy s
, bool dst
)
135 Construct a wxTime for today at the specified (local) hour, minute, and
139 if (!wxTimeInitialized
)
141 wxGetTZandDST(&TIME_ZONE
, &DST_OBSERVED
);
142 wxTimeInitialized
= TRUE
;
145 sec
= wxTime(wxDate(),h
,m
,s
,dst
).sec
;
149 wxTime::wxTime(const wxDate
& date
, hourTy h
, minuteTy m
, secondTy s
, bool dst
)
151 Construct a wxTime for the specified (local) Date, hour, minute, and
155 if (!wxTimeInitialized
)
157 wxGetTZandDST(&TIME_ZONE
, &DST_OBSERVED
);
158 wxTimeInitialized
= TRUE
;
160 sec
= GetLocalTime(date
,h
,m
,s
).sec
-3600;
164 if (IsDST() || dst
) sec
-= 3600;
170 if (IsDST()) setError(NIHCL_BADTIME,DEFAULT,
171 date.dayOfMonth(),date.nameOfMonth(),date.year(),
172 h,m,s,(dst?_("DST"):""));
175 sec
+= TIME_ZONE
; // adjust to GMT
179 wxTime::operator wxDate() const
181 Convert a wxTime to a local wxDate
184 // return wxDate((int)(GetLocalTime().sec/seconds_in_day)); 4.2 cc bug
185 long daycount
= (long)(GetLocalTime().sec
/seconds_in_day
);
187 wxDate
date(1,1,1901);
193 bool wxTime::IsBetween(const wxTime
& a
, const wxTime
& b
) const
195 return *this >= a
&& *this <= b
;
198 hourTy
wxTime::GetHour() const
200 Return the hour of this wxTime in local time; i.e., adjust for
201 time zone and Daylight Savings Time.
204 return GetLocalTime().GetHourGMT();
207 hourTy
wxTime::GetHourGMT() const
209 Return the hour of this Time in GMT.
212 return (hourTy
)((sec
% 86400) / 3600);
215 wxTime
wxTime::GetBeginDST(unsigned year
)
217 Return the local Standard Time at which Daylight Savings Time
218 begins in the specified year.
222 wxTime
DSTtime(GetLocalTime(wxDate(3,31,year
).Previous(1)+7,2));
225 DSTtime
= GetLocalTime(wxDate(4,30,year
).Previous(1),2);
226 if (year
==1974) DSTtime
= GetLocalTime(wxDate(1,6,1974),2);
227 if (year
==1975) DSTtime
= GetLocalTime(wxDate(2,23,1975),2);
232 wxTime
wxTime::GetEndDST(unsigned year
)
234 Return the local Standard Time at which Daylight Savings Time
235 ends in the specified year.
238 wxTime
STDtime(GetLocalTime(wxDate(10,31,year
).Previous(1),2-1));
242 bool wxTime::IsDST() const
244 Return TRUE if this local Standard Time should be adjusted
245 for Daylight Savings Time.
248 long daycount
= (long)(sec
/seconds_in_day
);
250 // At this point, daycount is the number of days from 1/1/1901.
251 // Need to convert to julian date (which starts at 1/1/4713 B.C.)
252 wxDate
date(1,1,1901);
255 unsigned year
= date
.GetYear();
258 if (*this >= GetBeginDST(year
))
259 if (*this < GetEndDST(year
)) return TRUE
;
264 wxTime
wxTime::GetLocalTime() const
266 Adjusts this GM Time for local time zone and Daylight Savings Time.
269 wxTime
local_time(sec
-TIME_ZONE
);
270 if (local_time
.IsDST()) local_time
.sec
+= 3600;
274 minuteTy
wxTime::GetMinute() const
276 Return the minute of this wxTime in local time; i.e., adjust
277 for time zone and Daylight Savings Time.
280 return GetLocalTime().GetMinuteGMT();
283 minuteTy
wxTime::GetMinuteGMT() const
285 Return the minute of this wxTime in GMT.
288 return (minuteTy
)(((sec
% 86400) % 3600) / 60);
291 secondTy
wxTime::GetSecond() const
293 Return the second of this wxTime.
296 return (secondTy
)(((sec
% 86400) % 3600) % 60);
299 secondTy
wxTime::GetSecondGMT() const
301 Return the minute of this wxTime in GMT.
304 return (secondTy
)(((sec
% 86400) % 3600) % 60);
307 int wxTime::GetDay() const
309 wxDate
da((wxDate
) *this);
313 int wxTime::GetDayOfWeek() const
315 wxDate
da((wxDate
) *this);
316 return da
.GetDayOfWeek();
319 int wxTime::GetMonth() const
321 wxDate
da((wxDate
) *this);
322 return da
.GetMonth();
325 int wxTime::GetYear() const
327 wxDate
da((wxDate
) *this);
331 wxTime
wxTime::Max(const wxTime
& t
) const
333 if (t
< *this) return *this;
337 wxTime
wxTime::Min(const wxTime
& t
) const
339 if (t
> *this) return *this;
344 wxTime::operator wxChar
*(void)
350 void wxTime::SetFormat(const wxTime::tFormat lFormat
,
351 const wxTime::tPrecision lPrecision
) {
353 wxTime::Format
= lFormat
;
354 wxTime::Precision
= lPrecision
;
357 wxChar
*wxTime::FormatTime() const {
358 static wxChar timeBuf
[30];
359 unsigned hh(GetHour());
371 wxSprintf(timeBuf
,wxT("%2d:%02d:%02d"),hh
,GetMinute(),GetSecond());
374 wxSprintf(timeBuf
,wxT("%2d:%02d"),hh
,GetMinute());
380 wxStrcat(timeBuf
,_("am"));
382 wxStrcat(timeBuf
,_("pm"));
388 int wxTime::compare(const Object& ob) const
390 assertArgSpecies(ob,classDesc,"compare");
391 register clockTy t = castdown(ob).sec;
392 if (sec < t) return -1;
393 if (sec > t) return 1;
397 void wxTime::deepenShallowCopy() {}
399 unsigned wxTime::hash() const { return sec; }
401 bool wxTime::isEqual(const Object& ob) const
403 return ob.isSpecies(classDesc) && *this==castdown(ob);
406 const Class* wxTime::species() const { return &classDesc; }
408 void wxTime::printOn(ostream& strm) const
410 register unsigned hh = GetHour();
411 wxDate(*this).printOn(strm);
412 strm << ' ' << ((hh <= 12) ? hh : hh-12) << ':'
413 << setfill('0') << setw(2) << GetMinute() << ':'
414 << setfill('0') << setw(2) << GetSecond() << ' ';
415 if (hh < 12) strm << _("am");
416 else strm << _("pm");
419 wxTime::wxTime(OIOin& strm)
426 void wxTime::storer(OIOout& strm) const
433 wxTime::wxTime(OIOifd& fd)
440 void wxTime::storer(OIOofd& fd) const