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