]> git.saurik.com Git - wxWidgets.git/blob - src/common/time.cpp
no message
[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 wxTime wxTime::Max(const wxTime& t) const
275 {
276 if (t < *this) return *this;
277 return t;
278 }
279
280 wxTime wxTime::Min(const wxTime& t) const
281 {
282 if (t > *this) return *this;
283 return t;
284 }
285
286 wxTime::operator char *(void)
287 {
288 return FormatTime();
289 }
290
291 void wxTime::SetFormat(const wxTime::tFormat lFormat,
292 const wxTime::tPrecision lPrecision) {
293
294 wxTime::Format = lFormat;
295 wxTime::Precision = lPrecision;
296 }
297
298 char *wxTime::FormatTime() const {
299 static char timeBuf[30];
300 unsigned hh(GetHour());
301
302 switch (Format) {
303 case wx12h:
304 hh -= 12;
305 break;
306 case wx24h:
307 break;
308 }
309
310 switch (Precision) {
311 case wxStdMinSec:
312 sprintf(timeBuf,"%2d:%02d:%02d",hh,GetMinute(),GetSecond());
313 break;
314 case wxStdMin:
315 sprintf(timeBuf,"%2d:%02d",hh,GetMinute());
316 break;
317 }
318
319 if (Format == wx12h)
320 if (GetHour() <= 12)
321 strcat(timeBuf,_("am"));
322 else
323 strcat(timeBuf,_("pm"));
324
325 return timeBuf;
326 }
327
328 /*
329 int wxTime::compare(const Object& ob) const
330 {
331 assertArgSpecies(ob,classDesc,"compare");
332 register clockTy t = castdown(ob).sec;
333 if (sec < t) return -1;
334 if (sec > t) return 1;
335 return 0;
336 }
337
338 void wxTime::deepenShallowCopy() {}
339
340 unsigned wxTime::hash() const { return sec; }
341
342 bool wxTime::isEqual(const Object& ob) const
343 {
344 return ob.isSpecies(classDesc) && *this==castdown(ob);
345 }
346
347 const Class* wxTime::species() const { return &classDesc; }
348
349 void wxTime::printOn(ostream& strm) const
350 {
351 register unsigned hh = GetHour();
352 wxDate(*this).printOn(strm);
353 strm << ' ' << ((hh <= 12) ? hh : hh-12) << ':'
354 << setfill('0') << setw(2) << GetMinute() << ':'
355 << setfill('0') << setw(2) << GetSecond() << ' ';
356 if (hh < 12) strm << _("am");
357 else strm << _("pm");
358 }
359
360 wxTime::wxTime(OIOin& strm)
361 : BASE(strm)
362 {
363 unsigned long usec;
364 strm >> sec >> usec;
365 }
366
367 void wxTime::storer(OIOout& strm) const
368 {
369 BASE::storer(strm);
370 strm << sec << 0l;
371 }
372
373
374 wxTime::wxTime(OIOifd& fd)
375 : BASE(fd)
376 {
377 unsigned long usec;
378 fd >> sec >> usec;
379 }
380
381 void wxTime::storer(OIOofd& fd) const
382 {
383 BASE::storer(fd);
384 fd << sec << 0l;
385 }
386 */
387
388 #endif