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