]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: time.cpp | |
3 | // Purpose: wxTime class, from NIHCL | |
4 | // Author: Julian Smart, after K. E. Gorlen | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
3f4a0c5b | 9 | // Licence: wxWindows license |
c801d85f KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "time.h" | |
14 | #endif | |
15 | ||
16 | /* | |
17 | Provides an object that represents a Time, stored as the number of | |
18 | seconds since January 1, 1901, GMT. | |
19 | */ | |
20 | ||
21 | // For compilers that support precompilation, includes "wx.h". | |
22 | #include "wx/wxprec.h" | |
23 | ||
24 | #ifdef __BORLANDC__ | |
25 | #pragma hdrstop | |
26 | #endif | |
27 | ||
28 | #include "wx/setup.h" | |
29 | ||
9838df2c | 30 | #if wxUSE_TIMEDATE |
c801d85f KB |
31 | |
32 | #include "wx/time.h" | |
33 | #include "wx/date.h" | |
34 | #include "wx/utils.h" | |
3f4a0c5b VZ |
35 | #include "wx/intl.h" |
36 | ||
be087207 RR |
37 | #if wxUSE_STD_IOSTREAM |
38 | #include "wx/ioswrap.h" | |
39 | #if wxUSE_IOSTREAMH | |
40 | #include <iomanip.h> | |
41 | #else | |
42 | #include <iomanip> | |
43 | #endif | |
c801d85f KB |
44 | #endif |
45 | ||
c801d85f KB |
46 | #include <string.h> |
47 | ||
c801d85f | 48 | IMPLEMENT_DYNAMIC_CLASS(wxTime, wxObject) |
c801d85f KB |
49 | |
50 | ||
51 | extern bool wxGetLocalTime(long *timeZone, int *dstObserved); | |
52 | extern long wxGetCurrentTime(void); | |
53 | ||
54 | static long TIME_ZONE; /* seconds west of GMT */ | |
55 | static int DST_OBSERVED; /* flags U.S. daylight saving time observed */ | |
56 | ||
57 | static bool wxTimeInitialized = FALSE; | |
58 | ||
3f4a0c5b VZ |
59 | wxTime::tFormat wxTime::Format = wxTime::wx12h; |
60 | wxTime::tPrecision wxTime::Precision = wxTime::wxStdMinSec; | |
c801d85f KB |
61 | |
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 */ | |
65 | ||
66 | wxTime wxTime::GetLocalTime(const wxDate& date, hourTy h, minuteTy m, secondTy s) | |
67 | /* | |
3f4a0c5b VZ |
68 | Return a local wxTime for the specified Standard Time date, hour, minute, |
69 | and second. | |
c801d85f KB |
70 | */ |
71 | { | |
72 | if (!wxTimeInitialized) | |
73 | { | |
3f4a0c5b VZ |
74 | wxGetLocalTime(&TIME_ZONE, &DST_OBSERVED); |
75 | wxTimeInitialized = TRUE; | |
c801d85f KB |
76 | } |
77 | /* | |
3f4a0c5b VZ |
78 | if (!date.IsBetween(refDate,maxDate)) |
79 | setError(NIHCL_DATERANGE,DEFAULT, | |
80 | date.dayOfMonth(),date.nameOfMonth(),date.year()); | |
c801d85f KB |
81 | */ |
82 | // The following line causes an error in GCC 2.1 | |
83 | // long daysBetween = date-refDate; | |
84 | // ... but this seems to get round it. | |
85 | wxDate tmp1(date); | |
86 | wxDate tmp2(refDate); | |
87 | long daysBetween = tmp1 - tmp2; | |
88 | ||
89 | return wxTime(seconds_in_day*daysBetween + 60*60L*h + 60*m + s); | |
90 | } | |
91 | ||
92 | wxTime::wxTime() | |
93 | /* | |
3f4a0c5b | 94 | Construct a wxTime for this instant. |
c801d85f KB |
95 | */ |
96 | { | |
97 | if (!wxTimeInitialized) | |
98 | { | |
3f4a0c5b VZ |
99 | wxGetLocalTime(&TIME_ZONE, &DST_OBSERVED); |
100 | wxTimeInitialized = TRUE; | |
c801d85f KB |
101 | } |
102 | sec = wxGetCurrentTime(); | |
ce3ed50d JS |
103 | #ifdef __SALFORDC__ |
104 | sec += (unsigned long) 2177452800; /* seconds from 1/1/01 to 1/1/70 */ | |
105 | #else | |
1f0299c1 | 106 | sec += 2177452800UL; /* seconds from 1/1/01 to 1/1/70 */ |
ce3ed50d | 107 | #endif |
c801d85f KB |
108 | } |
109 | ||
110 | wxTime::wxTime(hourTy h, minuteTy m, secondTy s, bool dst) | |
111 | /* | |
3f4a0c5b VZ |
112 | Construct a wxTime for today at the specified (local) hour, minute, and |
113 | second. | |
c801d85f KB |
114 | */ |
115 | { | |
116 | if (!wxTimeInitialized) | |
117 | { | |
3f4a0c5b VZ |
118 | wxGetLocalTime(&TIME_ZONE, &DST_OBSERVED); |
119 | wxTimeInitialized = TRUE; | |
c801d85f KB |
120 | } |
121 | ||
122 | sec = wxTime(wxDate(),h,m,s,dst).sec; | |
123 | } | |
124 | ||
125 | ||
126 | wxTime::wxTime(const wxDate& date, hourTy h, minuteTy m, secondTy s, bool dst) | |
127 | /* | |
128 | Construct a wxTime for the specified (local) Date, hour, minute, and | |
129 | second. | |
130 | */ | |
131 | { | |
132 | if (!wxTimeInitialized) | |
133 | { | |
134 | wxGetLocalTime(&TIME_ZONE, &DST_OBSERVED); | |
3f4a0c5b | 135 | wxTimeInitialized = TRUE; |
c801d85f KB |
136 | } |
137 | sec = GetLocalTime(date,h,m,s).sec-3600; | |
138 | if (IsDST()) | |
139 | { | |
140 | sec += 3600; | |
141 | if (IsDST() || dst) sec -= 3600; | |
142 | } | |
143 | else | |
144 | { | |
145 | sec += 3600; | |
146 | /* | |
147 | if (IsDST()) setError(NIHCL_BADTIME,DEFAULT, | |
3f4a0c5b | 148 | date.dayOfMonth(),date.nameOfMonth(),date.year(), |
9b64e798 | 149 | h,m,s,(dst?_("DST"):"")); |
c801d85f KB |
150 | */ |
151 | } | |
152 | sec += TIME_ZONE; // adjust to GMT | |
153 | } | |
154 | ||
ce3ed50d | 155 | #ifndef __SALFORDC__ |
c801d85f KB |
156 | wxTime::operator wxDate() const |
157 | /* | |
3f4a0c5b | 158 | Convert a wxTime to a local wxDate |
c801d85f KB |
159 | */ |
160 | { | |
161 | // return wxDate((int)(GetLocalTime().sec/seconds_in_day)); 4.2 cc bug | |
3f4a0c5b | 162 | long daycount = (long)(GetLocalTime().sec/seconds_in_day); |
c801d85f KB |
163 | |
164 | wxDate date(1,1,1901); | |
165 | date += daycount; | |
166 | return date; | |
167 | } | |
ce3ed50d | 168 | #endif |
c801d85f KB |
169 | |
170 | bool wxTime::IsBetween(const wxTime& a, const wxTime& b) const | |
171 | { | |
172 | return *this >= a && *this <= b; | |
173 | } | |
174 | ||
175 | hourTy wxTime::GetHour() const | |
176 | /* | |
3f4a0c5b | 177 | Return the hour of this wxTime in local time; i.e., adjust for |
c801d85f KB |
178 | time zone and Daylight Savings Time. |
179 | */ | |
180 | { | |
3f4a0c5b | 181 | return GetLocalTime().GetHourGMT(); |
c801d85f KB |
182 | } |
183 | ||
184 | hourTy wxTime::GetHourGMT() const | |
185 | /* | |
186 | Return the hour of this Time in GMT. | |
187 | */ | |
188 | { | |
3f4a0c5b | 189 | return (hourTy)((sec % 86400) / 3600); |
c801d85f KB |
190 | } |
191 | ||
192 | wxTime wxTime::GetBeginDST(unsigned year) | |
193 | /* | |
3f4a0c5b | 194 | Return the local Standard Time at which Daylight Savings Time |
c801d85f KB |
195 | begins in the specified year. |
196 | */ | |
197 | { | |
198 | // Previous Sunday | |
199 | wxTime DSTtime(GetLocalTime(wxDate(3,31,year).Previous(1)+7,2)); | |
200 | if (year<=1986) { | |
201 | // Previous Sunday | |
3f4a0c5b VZ |
202 | DSTtime = GetLocalTime(wxDate(4,30,year).Previous(1),2); |
203 | if (year==1974) DSTtime = GetLocalTime(wxDate(1,6,1974),2); | |
c801d85f KB |
204 | if (year==1975) DSTtime = GetLocalTime(wxDate(2,23,1975),2); |
205 | } | |
206 | return DSTtime; | |
207 | } | |
208 | ||
209 | wxTime wxTime::GetEndDST(unsigned year) | |
210 | /* | |
211 | Return the local Standard Time at which Daylight Savings Time | |
212 | ends in the specified year. | |
213 | */ | |
214 | { | |
3f4a0c5b VZ |
215 | wxTime STDtime(GetLocalTime(wxDate(10,31,year).Previous(1),2-1)); |
216 | return STDtime; | |
c801d85f KB |
217 | } |
218 | ||
219 | bool wxTime::IsDST() const | |
220 | /* | |
221 | Return TRUE if this local Standard Time should be adjusted | |
222 | for Daylight Savings Time. | |
223 | */ | |
224 | { | |
225 | long daycount = (long)(sec/seconds_in_day); | |
226 | ||
227 | // At this point, daycount is the number of days from 1/1/1901. | |
3f4a0c5b VZ |
228 | // Need to convert to julian date (which starts at 1/1/4713 B.C.) |
229 | wxDate date(1,1,1901); | |
c801d85f KB |
230 | date += daycount; |
231 | ||
232 | unsigned year = date.GetYear(); | |
3f4a0c5b | 233 | if (DST_OBSERVED) |
c801d85f KB |
234 | { |
235 | if (*this >= GetBeginDST(year)) | |
236 | if (*this < GetEndDST(year)) return TRUE; | |
237 | } | |
238 | return FALSE; | |
239 | } | |
240 | ||
241 | wxTime wxTime::GetLocalTime() const | |
242 | /* | |
243 | Adjusts this GM Time for local time zone and Daylight Savings Time. | |
244 | */ | |
245 | { | |
3f4a0c5b | 246 | wxTime local_time(sec-TIME_ZONE); |
c801d85f KB |
247 | if (local_time.IsDST()) local_time.sec += 3600; |
248 | return local_time; | |
249 | } | |
250 | ||
251 | minuteTy wxTime::GetMinute() const | |
252 | /* | |
253 | Return the minute of this wxTime in local time; i.e., adjust | |
3f4a0c5b | 254 | for time zone and Daylight Savings Time. |
c801d85f KB |
255 | */ |
256 | { | |
257 | return GetLocalTime().GetMinuteGMT(); | |
258 | } | |
259 | ||
260 | minuteTy wxTime::GetMinuteGMT() const | |
261 | /* | |
262 | Return the minute of this wxTime in GMT. | |
263 | */ | |
264 | { | |
3f4a0c5b | 265 | return (minuteTy)(((sec % 86400) % 3600) / 60); |
c801d85f KB |
266 | } |
267 | ||
268 | secondTy wxTime::GetSecond() const | |
269 | /* | |
270 | Return the second of this wxTime. | |
271 | */ | |
272 | { | |
3f4a0c5b | 273 | return (secondTy)(((sec % 86400) % 3600) % 60); |
c801d85f KB |
274 | } |
275 | ||
aa0b7e1e JS |
276 | secondTy wxTime::GetSecondGMT() const |
277 | /* | |
278 | Return the minute of this wxTime in GMT. | |
279 | */ | |
280 | { | |
281 | return (secondTy)(((sec % 86400) % 3600) % 60); | |
282 | } | |
283 | ||
284 | int wxTime::GetDay() const | |
285 | { | |
286 | wxDate da((wxDate) *this); | |
287 | return da.GetDay(); | |
288 | } | |
289 | ||
290 | int wxTime::GetDayOfWeek() const | |
291 | { | |
292 | wxDate da((wxDate) *this); | |
293 | return da.GetDayOfWeek(); | |
294 | } | |
295 | ||
296 | int wxTime::GetMonth() const | |
297 | { | |
298 | wxDate da((wxDate) *this); | |
299 | return da.GetMonth(); | |
300 | } | |
301 | ||
302 | int wxTime::GetYear() const | |
303 | { | |
304 | wxDate da((wxDate) *this); | |
305 | return da.GetYear(); | |
306 | } | |
307 | ||
c801d85f KB |
308 | wxTime wxTime::Max(const wxTime& t) const |
309 | { | |
310 | if (t < *this) return *this; | |
311 | return t; | |
312 | } | |
313 | ||
314 | wxTime wxTime::Min(const wxTime& t) const | |
315 | { | |
316 | if (t > *this) return *this; | |
3f4a0c5b | 317 | return t; |
c801d85f KB |
318 | } |
319 | ||
ce3ed50d | 320 | #ifndef __SALFORDC__ |
84fff0b3 | 321 | wxTime::operator wxChar *(void) |
c801d85f KB |
322 | { |
323 | return FormatTime(); | |
324 | } | |
ce3ed50d | 325 | #endif |
c801d85f KB |
326 | |
327 | void wxTime::SetFormat(const wxTime::tFormat lFormat, | |
3f4a0c5b | 328 | const wxTime::tPrecision lPrecision) { |
c801d85f | 329 | |
3f4a0c5b VZ |
330 | wxTime::Format = lFormat; |
331 | wxTime::Precision = lPrecision; | |
c801d85f KB |
332 | } |
333 | ||
84fff0b3 OK |
334 | wxChar *wxTime::FormatTime() const { |
335 | static wxChar timeBuf[30]; | |
3f4a0c5b VZ |
336 | unsigned hh(GetHour()); |
337 | ||
338 | switch (Format) { | |
339 | case wx12h: | |
340 | hh -= 12; | |
341 | break; | |
342 | case wx24h: | |
343 | break; | |
344 | } | |
345 | ||
346 | switch (Precision) { | |
347 | case wxStdMinSec: | |
223d09f6 | 348 | wxSprintf(timeBuf,wxT("%2d:%02d:%02d"),hh,GetMinute(),GetSecond()); |
3f4a0c5b VZ |
349 | break; |
350 | case wxStdMin: | |
223d09f6 | 351 | wxSprintf(timeBuf,wxT("%2d:%02d"),hh,GetMinute()); |
3f4a0c5b VZ |
352 | break; |
353 | } | |
354 | ||
355 | if (Format == wx12h) | |
356 | if (GetHour() <= 12) | |
84fff0b3 | 357 | wxStrcat(timeBuf,_("am")); |
3f4a0c5b | 358 | else |
84fff0b3 | 359 | wxStrcat(timeBuf,_("pm")); |
3f4a0c5b VZ |
360 | |
361 | return timeBuf; | |
c801d85f KB |
362 | } |
363 | ||
364 | /* | |
365 | int wxTime::compare(const Object& ob) const | |
366 | { | |
3f4a0c5b | 367 | assertArgSpecies(ob,classDesc,"compare"); |
c801d85f KB |
368 | register clockTy t = castdown(ob).sec; |
369 | if (sec < t) return -1; | |
370 | if (sec > t) return 1; | |
371 | return 0; | |
372 | } | |
373 | ||
374 | void wxTime::deepenShallowCopy() {} | |
375 | ||
376 | unsigned wxTime::hash() const { return sec; } | |
377 | ||
378 | bool wxTime::isEqual(const Object& ob) const | |
379 | { | |
380 | return ob.isSpecies(classDesc) && *this==castdown(ob); | |
381 | } | |
382 | ||
383 | const Class* wxTime::species() const { return &classDesc; } | |
384 | ||
385 | void wxTime::printOn(ostream& strm) const | |
386 | { | |
387 | register unsigned hh = GetHour(); | |
388 | wxDate(*this).printOn(strm); | |
389 | strm << ' ' << ((hh <= 12) ? hh : hh-12) << ':' | |
390 | << setfill('0') << setw(2) << GetMinute() << ':' | |
3f4a0c5b | 391 | << setfill('0') << setw(2) << GetSecond() << ' '; |
1a5a8367 DP |
392 | if (hh < 12) strm << _("am"); |
393 | else strm << _("pm"); | |
c801d85f KB |
394 | } |
395 | ||
396 | wxTime::wxTime(OIOin& strm) | |
3f4a0c5b | 397 | : BASE(strm) |
c801d85f | 398 | { |
3f4a0c5b VZ |
399 | unsigned long usec; |
400 | strm >> sec >> usec; | |
c801d85f KB |
401 | } |
402 | ||
403 | void wxTime::storer(OIOout& strm) const | |
404 | { | |
405 | BASE::storer(strm); | |
406 | strm << sec << 0l; | |
407 | } | |
408 | ||
409 | ||
410 | wxTime::wxTime(OIOifd& fd) | |
3f4a0c5b | 411 | : BASE(fd) |
c801d85f | 412 | { |
3f4a0c5b VZ |
413 | unsigned long usec; |
414 | fd >> sec >> usec; | |
c801d85f KB |
415 | } |
416 | ||
417 | void wxTime::storer(OIOofd& fd) const | |
418 | { | |
3f4a0c5b VZ |
419 | BASE::storer(fd); |
420 | fd << sec << 0l; | |
c801d85f KB |
421 | } |
422 | */ | |
423 | ||
424 | #endif |