removed USE_SHARED_LIBRARY(IES)
[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_STD_IOSTREAM
38 #include "wx/ioswrap.h"
39 #if wxUSE_IOSTREAMH
40 #include <iomanip.h>
41 #else
42 #include <iomanip>
43 #endif
44 #endif
45
46 #include <string.h>
47
48 IMPLEMENT_DYNAMIC_CLASS(wxTime, wxObject)
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
59 wxTime::tFormat wxTime::Format = wxTime::wx12h;
60 wxTime::tPrecision wxTime::Precision = wxTime::wxStdMinSec;
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 /*
68 Return a local wxTime for the specified Standard Time date, hour, minute,
69 and second.
70 */
71 {
72 if (!wxTimeInitialized)
73 {
74 wxGetLocalTime(&TIME_ZONE, &DST_OBSERVED);
75 wxTimeInitialized = TRUE;
76 }
77 /*
78 if (!date.IsBetween(refDate,maxDate))
79 setError(NIHCL_DATERANGE,DEFAULT,
80 date.dayOfMonth(),date.nameOfMonth(),date.year());
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 /*
94 Construct a wxTime for this instant.
95 */
96 {
97 if (!wxTimeInitialized)
98 {
99 wxGetLocalTime(&TIME_ZONE, &DST_OBSERVED);
100 wxTimeInitialized = TRUE;
101 }
102 sec = wxGetCurrentTime();
103 #ifdef __SALFORDC__
104 sec += (unsigned long) 2177452800; /* seconds from 1/1/01 to 1/1/70 */
105 #else
106 sec += 2177452800UL; /* seconds from 1/1/01 to 1/1/70 */
107 #endif
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 #ifndef __SALFORDC__
156 wxTime::operator wxDate() const
157 /*
158 Convert a wxTime to a local wxDate
159 */
160 {
161 // return wxDate((int)(GetLocalTime().sec/seconds_in_day)); 4.2 cc bug
162 long daycount = (long)(GetLocalTime().sec/seconds_in_day);
163
164 wxDate date(1,1,1901);
165 date += daycount;
166 return date;
167 }
168 #endif
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 /*
177 Return the hour of this wxTime in local time; i.e., adjust for
178 time zone and Daylight Savings Time.
179 */
180 {
181 return GetLocalTime().GetHourGMT();
182 }
183
184 hourTy wxTime::GetHourGMT() const
185 /*
186 Return the hour of this Time in GMT.
187 */
188 {
189 return (hourTy)((sec % 86400) / 3600);
190 }
191
192 wxTime wxTime::GetBeginDST(unsigned year)
193 /*
194 Return the local Standard Time at which Daylight Savings Time
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
202 DSTtime = GetLocalTime(wxDate(4,30,year).Previous(1),2);
203 if (year==1974) DSTtime = GetLocalTime(wxDate(1,6,1974),2);
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 {
215 wxTime STDtime(GetLocalTime(wxDate(10,31,year).Previous(1),2-1));
216 return STDtime;
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.
228 // Need to convert to julian date (which starts at 1/1/4713 B.C.)
229 wxDate date(1,1,1901);
230 date += daycount;
231
232 unsigned year = date.GetYear();
233 if (DST_OBSERVED)
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 {
246 wxTime local_time(sec-TIME_ZONE);
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
254 for time zone and Daylight Savings Time.
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 {
265 return (minuteTy)(((sec % 86400) % 3600) / 60);
266 }
267
268 secondTy wxTime::GetSecond() const
269 /*
270 Return the second of this wxTime.
271 */
272 {
273 return (secondTy)(((sec % 86400) % 3600) % 60);
274 }
275
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
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;
317 return t;
318 }
319
320 #ifndef __SALFORDC__
321 wxTime::operator wxChar *(void)
322 {
323 return FormatTime();
324 }
325 #endif
326
327 void wxTime::SetFormat(const wxTime::tFormat lFormat,
328 const wxTime::tPrecision lPrecision) {
329
330 wxTime::Format = lFormat;
331 wxTime::Precision = lPrecision;
332 }
333
334 wxChar *wxTime::FormatTime() const {
335 static wxChar timeBuf[30];
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:
348 wxSprintf(timeBuf,wxT("%2d:%02d:%02d"),hh,GetMinute(),GetSecond());
349 break;
350 case wxStdMin:
351 wxSprintf(timeBuf,wxT("%2d:%02d"),hh,GetMinute());
352 break;
353 }
354
355 if (Format == wx12h)
356 if (GetHour() <= 12)
357 wxStrcat(timeBuf,_("am"));
358 else
359 wxStrcat(timeBuf,_("pm"));
360
361 return timeBuf;
362 }
363
364 /*
365 int wxTime::compare(const Object& ob) const
366 {
367 assertArgSpecies(ob,classDesc,"compare");
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() << ':'
391 << setfill('0') << setw(2) << GetSecond() << ' ';
392 if (hh < 12) strm << _("am");
393 else strm << _("pm");
394 }
395
396 wxTime::wxTime(OIOin& strm)
397 : BASE(strm)
398 {
399 unsigned long usec;
400 strm >> sec >> usec;
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)
411 : BASE(fd)
412 {
413 unsigned long usec;
414 fd >> sec >> usec;
415 }
416
417 void wxTime::storer(OIOofd& fd) const
418 {
419 BASE::storer(fd);
420 fd << sec << 0l;
421 }
422 */
423
424 #endif