]>
Commit | Line | Data |
---|---|---|
1ef54dcf | 1 | /////////////////////////////////////////////////////////////////////////////// |
0979c962 VZ |
2 | // Name: wx/datetime.h |
3 | // Purpose: implementation of time/date related classes | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 11.05.99 | |
7 | // RCS-ID: $Id$ | |
1ef54dcf VZ |
8 | // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> |
9 | // parts of code taken from sndcal library by Scott E. Lee: | |
10 | // | |
11 | // Copyright 1993-1995, Scott E. Lee, all rights reserved. | |
12 | // Permission granted to use, copy, modify, distribute and sell | |
13 | // so long as the above copyright and this permission statement | |
14 | // are retained in all copies. | |
15 | // | |
65571936 | 16 | // Licence: wxWindows licence |
1ef54dcf VZ |
17 | /////////////////////////////////////////////////////////////////////////////// |
18 | ||
e44501a0 VZ |
19 | // TODO: for $DEITY sake, someone please fix the #ifdef __WXWINCE__ everywhere, |
20 | // the proper way to do it is to implement (subset of) wxStrftime() for | |
21 | // CE instead of this horror!! | |
22 | ||
1ef54dcf VZ |
23 | /* |
24 | * Implementation notes: | |
25 | * | |
26 | * 1. the time is stored as a 64bit integer containing the signed number of | |
299fcbfe VZ |
27 | * milliseconds since Jan 1. 1970 (the Unix Epoch) - so it is always |
28 | * expressed in GMT. | |
1ef54dcf VZ |
29 | * |
30 | * 2. the range is thus something about 580 million years, but due to current | |
31 | * algorithms limitations, only dates from Nov 24, 4714BC are handled | |
32 | * | |
33 | * 3. standard ANSI C functions are used to do time calculations whenever | |
34 | * possible, i.e. when the date is in the range Jan 1, 1970 to 2038 | |
35 | * | |
36 | * 4. otherwise, the calculations are done by converting the date to/from JDN | |
37 | * first (the range limitation mentioned above comes from here: the | |
38 | * algorithm used by Scott E. Lee's code only works for positive JDNs, more | |
39 | * or less) | |
40 | * | |
299fcbfe VZ |
41 | * 5. the object constructed for the given DD-MM-YYYY HH:MM:SS corresponds to |
42 | * this moment in local time and may be converted to the object | |
43 | * corresponding to the same date/time in another time zone by using | |
44 | * ToTimezone() | |
45 | * | |
46 | * 6. the conversions to the current (or any other) timezone are done when the | |
47 | * internal time representation is converted to the broken-down one in | |
48 | * wxDateTime::Tm. | |
1ef54dcf | 49 | */ |
0979c962 VZ |
50 | |
51 | // ============================================================================ | |
52 | // declarations | |
53 | // ============================================================================ | |
54 | ||
55 | // ---------------------------------------------------------------------------- | |
56 | // headers | |
57 | // ---------------------------------------------------------------------------- | |
58 | ||
14f355c2 | 59 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
0979c962 VZ |
60 | #pragma implementation "datetime.h" |
61 | #endif | |
62 | ||
63 | // For compilers that support precompilation, includes "wx.h". | |
64 | #include "wx/wxprec.h" | |
65 | ||
66 | #ifdef __BORLANDC__ | |
67 | #pragma hdrstop | |
68 | #endif | |
69 | ||
1e6feb95 VZ |
70 | #if !defined(wxUSE_DATETIME) || wxUSE_DATETIME |
71 | ||
0979c962 VZ |
72 | #ifndef WX_PRECOMP |
73 | #include "wx/string.h" | |
0979c962 VZ |
74 | #include "wx/log.h" |
75 | #endif // WX_PRECOMP | |
76 | ||
54800df8 | 77 | #include "wx/intl.h" |
fcc3d7cb | 78 | #include "wx/thread.h" |
cd0b1709 | 79 | #include "wx/tokenzr.h" |
7281ab04 | 80 | #include "wx/module.h" |
fcc3d7cb | 81 | |
ff0ea71c GT |
82 | #include <ctype.h> |
83 | ||
64f9c100 VS |
84 | #ifdef __WINDOWS__ |
85 | #include "wx/msw/wrapwin.h" | |
86 | #include <winnls.h> | |
74b99d0f WS |
87 | #ifndef __WXWINCE__ |
88 | #include <locale.h> | |
89 | #endif | |
64f9c100 VS |
90 | #endif |
91 | ||
0979c962 | 92 | #include "wx/datetime.h" |
3422227a | 93 | #include "wx/stopwatch.h" // for wxGetLocalTimeMillis() |
0979c962 | 94 | |
bd4a25bc JS |
95 | const long wxDateTime::TIME_T_FACTOR = 1000l; |
96 | ||
cb73e600 SC |
97 | #if wxUSE_EXTENDED_RTTI |
98 | ||
99 | template<> void wxStringReadValue(const wxString &s , wxDateTime &data ) | |
100 | { | |
101 | data.ParseFormat(s,wxT("%Y-%m-%d %H:%M:%S")) ; | |
102 | } | |
103 | ||
104 | template<> void wxStringWriteValue(wxString &s , const wxDateTime &data ) | |
105 | { | |
106 | s = data.Format(wxT("%Y-%m-%d %H:%M:%S")) ; | |
107 | } | |
108 | ||
8805dbab | 109 | wxCUSTOM_TYPE_INFO(wxDateTime, wxToStringConverter<wxDateTime> , wxFromStringConverter<wxDateTime>) |
cb73e600 SC |
110 | |
111 | #endif | |
112 | ||
113 | // | |
f0f951fa VZ |
114 | // ---------------------------------------------------------------------------- |
115 | // conditional compilation | |
116 | // ---------------------------------------------------------------------------- | |
6de20863 | 117 | |
2423ef22 VZ |
118 | #if defined(HAVE_STRPTIME) && defined(__GLIBC__) && \ |
119 | ((__GLIBC__ == 2) && (__GLIBC_MINOR__ == 0)) | |
f0f951fa VZ |
120 | // glibc 2.0.7 strptime() is broken - the following snippet causes it to |
121 | // crash (instead of just failing): | |
122 | // | |
123 | // strncpy(buf, "Tue Dec 21 20:25:40 1999", 128); | |
124 | // strptime(buf, "%x", &tm); | |
125 | // | |
126 | // so don't use it | |
127 | #undef HAVE_STRPTIME | |
128 | #endif // broken strptime() | |
129 | ||
31907d03 SC |
130 | #if defined(__MWERKS__) && wxUSE_UNICODE |
131 | #include <wtime.h> | |
132 | #endif | |
133 | ||
0779cc9a | 134 | #if !defined(WX_TIMEZONE) && !defined(WX_GMTOFF_IN_TM) |
6a27c749 WS |
135 | #if defined(__WXPALMOS__) |
136 | #define WX_GMTOFF_IN_TM | |
137 | #elif defined(__BORLANDC__) || defined(__MINGW32__) || defined(__VISAGECPP__) | |
f0f951fa | 138 | #define WX_TIMEZONE _timezone |
8208e181 | 139 | #elif defined(__MWERKS__) |
f6bcfd97 | 140 | long wxmw_timezone = 28800; |
c4e1b7f2 | 141 | #define WX_TIMEZONE wxmw_timezone |
5283098e | 142 | #elif defined(__DJGPP__) || defined(__WINE__) |
713a0efc | 143 | #include <sys/timeb.h> |
c4e1b7f2 | 144 | #include <values.h> |
713a0efc VS |
145 | static long wxGetTimeZone() |
146 | { | |
c4e1b7f2 VS |
147 | static long timezone = MAXLONG; // invalid timezone |
148 | if (timezone == MAXLONG) | |
149 | { | |
150 | struct timeb tb; | |
151 | ftime(&tb); | |
152 | timezone = tb.timezone; | |
153 | } | |
154 | return timezone; | |
713a0efc VS |
155 | } |
156 | #define WX_TIMEZONE wxGetTimeZone() | |
0779cc9a GD |
157 | #elif defined(__DARWIN__) |
158 | #define WX_GMTOFF_IN_TM | |
f0f951fa VZ |
159 | #else // unknown platform - try timezone |
160 | #define WX_TIMEZONE timezone | |
161 | #endif | |
0779cc9a | 162 | #endif // !WX_TIMEZONE && !WX_GMTOFF_IN_TM |
c25a510b | 163 | |
384223b3 VZ |
164 | // ---------------------------------------------------------------------------- |
165 | // macros | |
166 | // ---------------------------------------------------------------------------- | |
167 | ||
168 | // debugging helper: just a convenient replacement of wxCHECK() | |
169 | #define wxDATETIME_CHECK(expr, msg) \ | |
170 | if ( !(expr) ) \ | |
171 | { \ | |
172 | wxFAIL_MSG(msg); \ | |
173 | *this = wxInvalidDateTime; \ | |
174 | return *this; \ | |
175 | } | |
176 | ||
4f6aed9c VZ |
177 | // ---------------------------------------------------------------------------- |
178 | // private classes | |
179 | // ---------------------------------------------------------------------------- | |
180 | ||
181 | class wxDateTimeHolidaysModule : public wxModule | |
182 | { | |
183 | public: | |
184 | virtual bool OnInit() | |
185 | { | |
186 | wxDateTimeHolidayAuthority::AddAuthority(new wxDateTimeWorkDays); | |
187 | ||
68379eaf | 188 | return true; |
4f6aed9c VZ |
189 | } |
190 | ||
191 | virtual void OnExit() | |
192 | { | |
193 | wxDateTimeHolidayAuthority::ClearAllAuthorities(); | |
df5168c4 | 194 | wxDateTimeHolidayAuthority::ms_authorities.clear(); |
4f6aed9c VZ |
195 | } |
196 | ||
197 | private: | |
198 | DECLARE_DYNAMIC_CLASS(wxDateTimeHolidaysModule) | |
199 | }; | |
200 | ||
201 | IMPLEMENT_DYNAMIC_CLASS(wxDateTimeHolidaysModule, wxModule) | |
202 | ||
b76b015e VZ |
203 | // ---------------------------------------------------------------------------- |
204 | // constants | |
205 | // ---------------------------------------------------------------------------- | |
206 | ||
e6ec579c | 207 | // some trivial ones |
fcc3d7cb VZ |
208 | static const int MONTHS_IN_YEAR = 12; |
209 | ||
df05cdc5 VZ |
210 | static const int SEC_PER_MIN = 60; |
211 | ||
212 | static const int MIN_PER_HOUR = 60; | |
213 | ||
214 | static const int HOURS_PER_DAY = 24; | |
fcc3d7cb | 215 | |
e6ec579c VZ |
216 | static const long SECONDS_PER_DAY = 86400l; |
217 | ||
df05cdc5 VZ |
218 | static const int DAYS_PER_WEEK = 7; |
219 | ||
e6ec579c VZ |
220 | static const long MILLISECONDS_PER_DAY = 86400000l; |
221 | ||
222 | // this is the integral part of JDN of the midnight of Jan 1, 1970 | |
223 | // (i.e. JDN(Jan 1, 1970) = 2440587.5) | |
cd0b1709 | 224 | static const long EPOCH_JDN = 2440587l; |
b76b015e | 225 | |
1ef54dcf VZ |
226 | // the date of JDN -0.5 (as we don't work with fractional parts, this is the |
227 | // reference date for us) is Nov 24, 4714BC | |
228 | static const int JDN_0_YEAR = -4713; | |
229 | static const int JDN_0_MONTH = wxDateTime::Nov; | |
230 | static const int JDN_0_DAY = 24; | |
231 | ||
232 | // the constants used for JDN calculations | |
cd0b1709 VZ |
233 | static const long JDN_OFFSET = 32046l; |
234 | static const long DAYS_PER_5_MONTHS = 153l; | |
235 | static const long DAYS_PER_4_YEARS = 1461l; | |
236 | static const long DAYS_PER_400_YEARS = 146097l; | |
1ef54dcf | 237 | |
f0f951fa VZ |
238 | // this array contains the cumulated number of days in all previous months for |
239 | // normal and leap years | |
240 | static const wxDateTime::wxDateTime_t gs_cumulatedDays[2][MONTHS_IN_YEAR] = | |
241 | { | |
242 | { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }, | |
243 | { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 } | |
244 | }; | |
245 | ||
fcc3d7cb | 246 | // ---------------------------------------------------------------------------- |
2ef31e80 VZ |
247 | // global data |
248 | // ---------------------------------------------------------------------------- | |
249 | ||
1aaf88d2 WS |
250 | const wxChar * wxDefaultDateTimeFormat = wxT("%c"); |
251 | const wxChar * wxDefaultTimeSpanFormat = wxT("%H:%M:%S"); | |
252 | ||
384223b3 VZ |
253 | // in the fine tradition of ANSI C we use our equivalent of (time_t)-1 to |
254 | // indicate an invalid wxDateTime object | |
56d0c5a6 | 255 | const wxDateTime wxDefaultDateTime; |
2ef31e80 VZ |
256 | |
257 | wxDateTime::Country wxDateTime::ms_country = wxDateTime::Country_Unknown; | |
258 | ||
b76b015e VZ |
259 | // ---------------------------------------------------------------------------- |
260 | // private functions | |
261 | // ---------------------------------------------------------------------------- | |
262 | ||
4f6aed9c VZ |
263 | // debugger helper: shows what the date really is |
264 | #ifdef __WXDEBUG__ | |
265 | extern const wxChar *wxDumpDate(const wxDateTime* dt) | |
266 | { | |
0de868d9 | 267 | static wxChar buf[128]; |
4f6aed9c VZ |
268 | |
269 | wxStrcpy(buf, dt->Format(_T("%Y-%m-%d (%a) %H:%M:%S"))); | |
270 | ||
271 | return buf; | |
272 | } | |
273 | #endif // Debug | |
274 | ||
fcc3d7cb VZ |
275 | // get the number of days in the given month of the given year |
276 | static inline | |
277 | wxDateTime::wxDateTime_t GetNumOfDaysInMonth(int year, wxDateTime::Month month) | |
278 | { | |
e6ec579c VZ |
279 | // the number of days in month in Julian/Gregorian calendar: the first line |
280 | // is for normal years, the second one is for the leap ones | |
281 | static wxDateTime::wxDateTime_t daysInMonth[2][MONTHS_IN_YEAR] = | |
282 | { | |
283 | { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, | |
284 | { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } | |
285 | }; | |
286 | ||
287 | return daysInMonth[wxDateTime::IsLeapYear(year)][month]; | |
fcc3d7cb VZ |
288 | } |
289 | ||
c5432233 VZ |
290 | // returns the time zone in the C sense, i.e. the difference UTC - local |
291 | // (in seconds) | |
fcc3d7cb VZ |
292 | static int GetTimeZone() |
293 | { | |
51485b76 | 294 | #ifdef WX_GMTOFF_IN_TM |
68379eaf WS |
295 | // set to true when the timezone is set |
296 | static bool s_timezoneSet = false; | |
0779cc9a | 297 | static long gmtoffset = LONG_MAX; // invalid timezone |
c5432233 | 298 | |
c5432233 | 299 | // ensure that the timezone variable is set by calling localtime |
fcc3d7cb VZ |
300 | if ( !s_timezoneSet ) |
301 | { | |
1ef54dcf | 302 | // just call localtime() instead of figuring out whether this system |
e6ec579c | 303 | // supports tzset(), _tzset() or something else |
c5432233 | 304 | time_t t = 0; |
0779cc9a | 305 | struct tm *tm; |
fcc3d7cb | 306 | |
0779cc9a | 307 | tm = localtime(&t); |
68379eaf | 308 | s_timezoneSet = true; |
c5432233 | 309 | |
c5432233 VZ |
310 | // note that GMT offset is the opposite of time zone and so to return |
311 | // consistent results in both WX_GMTOFF_IN_TM and !WX_GMTOFF_IN_TM | |
312 | // cases we have to negate it | |
313 | gmtoffset = -tm->tm_gmtoff; | |
fcc3d7cb VZ |
314 | } |
315 | ||
0779cc9a | 316 | return (int)gmtoffset; |
51485b76 | 317 | #else // !WX_GMTOFF_IN_TM |
6de20863 | 318 | return (int)WX_TIMEZONE; |
51485b76 | 319 | #endif // WX_GMTOFF_IN_TM/!WX_GMTOFF_IN_TM |
fcc3d7cb VZ |
320 | } |
321 | ||
e6ec579c | 322 | // return the integral part of the JDN for the midnight of the given date (to |
1ef54dcf VZ |
323 | // get the real JDN you need to add 0.5, this is, in fact, JDN of the |
324 | // noon of the previous day) | |
e6ec579c VZ |
325 | static long GetTruncatedJDN(wxDateTime::wxDateTime_t day, |
326 | wxDateTime::Month mon, | |
327 | int year) | |
328 | { | |
1ef54dcf VZ |
329 | // CREDIT: code below is by Scott E. Lee (but bugs are mine) |
330 | ||
331 | // check the date validity | |
332 | wxASSERT_MSG( | |
333 | (year > JDN_0_YEAR) || | |
334 | ((year == JDN_0_YEAR) && (mon > JDN_0_MONTH)) || | |
335 | ((year == JDN_0_YEAR) && (mon == JDN_0_MONTH) && (day >= JDN_0_DAY)), | |
336 | _T("date out of range - can't convert to JDN") | |
337 | ); | |
338 | ||
339 | // make the year positive to avoid problems with negative numbers division | |
340 | year += 4800; | |
341 | ||
342 | // months are counted from March here | |
343 | int month; | |
344 | if ( mon >= wxDateTime::Mar ) | |
e6ec579c | 345 | { |
1ef54dcf | 346 | month = mon - 2; |
e6ec579c | 347 | } |
1ef54dcf | 348 | else |
e6ec579c | 349 | { |
1ef54dcf VZ |
350 | month = mon + 10; |
351 | year--; | |
352 | } | |
e6ec579c | 353 | |
1ef54dcf VZ |
354 | // now we can simply add all the contributions together |
355 | return ((year / 100) * DAYS_PER_400_YEARS) / 4 | |
356 | + ((year % 100) * DAYS_PER_4_YEARS) / 4 | |
357 | + (month * DAYS_PER_5_MONTHS + 2) / 5 | |
358 | + day | |
359 | - JDN_OFFSET; | |
e6ec579c VZ |
360 | } |
361 | ||
74b99d0f | 362 | #ifndef __WXWINCE__ |
2423ef22 | 363 | // this function is a wrapper around strftime(3) adding error checking |
b76b015e VZ |
364 | static wxString CallStrftime(const wxChar *format, const tm* tm) |
365 | { | |
68ee7c47 | 366 | wxChar buf[4096]; |
ba9bbf13 | 367 | if ( !wxStrftime(buf, WXSIZEOF(buf), format, tm) ) |
b76b015e | 368 | { |
68ee7c47 | 369 | // buffer is too small? |
b76b015e VZ |
370 | wxFAIL_MSG(_T("strftime() failed")); |
371 | } | |
372 | ||
373 | return wxString(buf); | |
374 | } | |
74b99d0f | 375 | #endif |
b76b015e | 376 | |
2423ef22 VZ |
377 | #ifdef HAVE_STRPTIME |
378 | ||
e2620fda VZ |
379 | // glibc2 doesn't define this in the headers unless _XOPEN_SOURCE is defined |
380 | // which, unfortunately, wreaks havoc elsewhere | |
381 | #if defined(__GLIBC__) && (__GLIBC__ == 2) | |
382 | extern "C" char *strptime(const char *, const char *, struct tm *); | |
383 | #endif | |
384 | ||
2423ef22 VZ |
385 | // Unicode-friendly strptime() wrapper |
386 | static const wxChar * | |
387 | CallStrptime(const wxChar *input, const char *fmt, tm *tm) | |
388 | { | |
389 | // the problem here is that strptime() returns pointer into the string we | |
390 | // passed to it while we're really interested in the pointer into the | |
391 | // original, Unicode, string so we try to transform the pointer back | |
392 | #if wxUSE_UNICODE | |
393 | wxCharBuffer inputMB(wxConvertWX2MB(input)); | |
394 | #else // ASCII | |
395 | const char * const inputMB = input; | |
396 | #endif // Unicode/Ascii | |
397 | ||
398 | const char *result = strptime(inputMB, fmt, tm); | |
399 | if ( !result ) | |
400 | return NULL; | |
401 | ||
402 | #if wxUSE_UNICODE | |
403 | // FIXME: this is wrong in presence of surrogates &c | |
404 | return input + (result - inputMB.data()); | |
405 | #else // ASCII | |
406 | return result; | |
407 | #endif // Unicode/Ascii | |
408 | } | |
409 | ||
410 | #endif // HAVE_STRPTIME | |
411 | ||
2f02cb89 VZ |
412 | // if year and/or month have invalid values, replace them with the current ones |
413 | static void ReplaceDefaultYearMonthWithCurrent(int *year, | |
414 | wxDateTime::Month *month) | |
415 | { | |
416 | struct tm *tmNow = NULL; | |
417 | ||
418 | if ( *year == wxDateTime::Inv_Year ) | |
419 | { | |
420 | tmNow = wxDateTime::GetTmNow(); | |
421 | ||
422 | *year = 1900 + tmNow->tm_year; | |
423 | } | |
424 | ||
425 | if ( *month == wxDateTime::Inv_Month ) | |
426 | { | |
427 | if ( !tmNow ) | |
428 | tmNow = wxDateTime::GetTmNow(); | |
429 | ||
430 | *month = (wxDateTime::Month)tmNow->tm_mon; | |
431 | } | |
432 | } | |
433 | ||
f0f951fa VZ |
434 | // fll the struct tm with default values |
435 | static void InitTm(struct tm& tm) | |
436 | { | |
437 | // struct tm may have etxra fields (undocumented and with unportable | |
438 | // names) which, nevertheless, must be set to 0 | |
439 | memset(&tm, 0, sizeof(struct tm)); | |
440 | ||
441 | tm.tm_mday = 1; // mday 0 is invalid | |
442 | tm.tm_year = 76; // any valid year | |
443 | tm.tm_isdst = -1; // auto determine | |
444 | } | |
445 | ||
cd0b1709 VZ |
446 | // parsing helpers |
447 | // --------------- | |
448 | ||
449 | // return the month if the string is a month name or Inv_Month otherwise | |
f0f951fa | 450 | static wxDateTime::Month GetMonthFromName(const wxString& name, int flags) |
cd0b1709 VZ |
451 | { |
452 | wxDateTime::Month mon; | |
453 | for ( mon = wxDateTime::Jan; mon < wxDateTime::Inv_Month; wxNextMonth(mon) ) | |
454 | { | |
f0f951fa VZ |
455 | // case-insensitive comparison either one of or with both abbreviated |
456 | // and not versions | |
457 | if ( flags & wxDateTime::Name_Full ) | |
cd0b1709 | 458 | { |
f0f951fa VZ |
459 | if ( name.CmpNoCase(wxDateTime:: |
460 | GetMonthName(mon, wxDateTime::Name_Full)) == 0 ) | |
461 | { | |
462 | break; | |
463 | } | |
464 | } | |
465 | ||
466 | if ( flags & wxDateTime::Name_Abbr ) | |
467 | { | |
468 | if ( name.CmpNoCase(wxDateTime:: | |
469 | GetMonthName(mon, wxDateTime::Name_Abbr)) == 0 ) | |
470 | { | |
471 | break; | |
472 | } | |
cd0b1709 VZ |
473 | } |
474 | } | |
475 | ||
476 | return mon; | |
477 | } | |
478 | ||
479 | // return the weekday if the string is a weekday name or Inv_WeekDay otherwise | |
f0f951fa | 480 | static wxDateTime::WeekDay GetWeekDayFromName(const wxString& name, int flags) |
cd0b1709 VZ |
481 | { |
482 | wxDateTime::WeekDay wd; | |
483 | for ( wd = wxDateTime::Sun; wd < wxDateTime::Inv_WeekDay; wxNextWDay(wd) ) | |
484 | { | |
f0f951fa VZ |
485 | // case-insensitive comparison either one of or with both abbreviated |
486 | // and not versions | |
487 | if ( flags & wxDateTime::Name_Full ) | |
cd0b1709 | 488 | { |
f0f951fa VZ |
489 | if ( name.CmpNoCase(wxDateTime:: |
490 | GetWeekDayName(wd, wxDateTime::Name_Full)) == 0 ) | |
491 | { | |
492 | break; | |
493 | } | |
494 | } | |
495 | ||
496 | if ( flags & wxDateTime::Name_Abbr ) | |
497 | { | |
498 | if ( name.CmpNoCase(wxDateTime:: | |
499 | GetWeekDayName(wd, wxDateTime::Name_Abbr)) == 0 ) | |
500 | { | |
501 | break; | |
502 | } | |
cd0b1709 VZ |
503 | } |
504 | } | |
505 | ||
506 | return wd; | |
507 | } | |
508 | ||
f6bcfd97 BP |
509 | // scans all digits (but no more than len) and returns the resulting number |
510 | static bool GetNumericToken(size_t len, const wxChar*& p, unsigned long *number) | |
f0f951fa | 511 | { |
f6bcfd97 | 512 | size_t n = 1; |
f0f951fa VZ |
513 | wxString s; |
514 | while ( wxIsdigit(*p) ) | |
515 | { | |
516 | s += *p++; | |
f6bcfd97 BP |
517 | |
518 | if ( len && ++n > len ) | |
519 | break; | |
f0f951fa VZ |
520 | } |
521 | ||
bb8a8478 | 522 | return !s.empty() && s.ToULong(number); |
f0f951fa VZ |
523 | } |
524 | ||
525 | // scans all alphabetic characters and returns the resulting string | |
526 | static wxString GetAlphaToken(const wxChar*& p) | |
527 | { | |
528 | wxString s; | |
529 | while ( wxIsalpha(*p) ) | |
530 | { | |
531 | s += *p++; | |
532 | } | |
533 | ||
534 | return s; | |
535 | } | |
536 | ||
0979c962 VZ |
537 | // ============================================================================ |
538 | // implementation of wxDateTime | |
539 | // ============================================================================ | |
540 | ||
b76b015e VZ |
541 | // ---------------------------------------------------------------------------- |
542 | // struct Tm | |
543 | // ---------------------------------------------------------------------------- | |
544 | ||
545 | wxDateTime::Tm::Tm() | |
546 | { | |
547 | year = (wxDateTime_t)wxDateTime::Inv_Year; | |
548 | mon = wxDateTime::Inv_Month; | |
549 | mday = 0; | |
e6ec579c | 550 | hour = min = sec = msec = 0; |
b76b015e VZ |
551 | wday = wxDateTime::Inv_WeekDay; |
552 | } | |
553 | ||
299fcbfe VZ |
554 | wxDateTime::Tm::Tm(const struct tm& tm, const TimeZone& tz) |
555 | : m_tz(tz) | |
b76b015e | 556 | { |
e6ec579c | 557 | msec = 0; |
907173e5 WS |
558 | sec = (wxDateTime::wxDateTime_t)tm.tm_sec; |
559 | min = (wxDateTime::wxDateTime_t)tm.tm_min; | |
560 | hour = (wxDateTime::wxDateTime_t)tm.tm_hour; | |
561 | mday = (wxDateTime::wxDateTime_t)tm.tm_mday; | |
fcc3d7cb | 562 | mon = (wxDateTime::Month)tm.tm_mon; |
b76b015e | 563 | year = 1900 + tm.tm_year; |
bb8a8478 WS |
564 | wday = (wxDateTime::wxDateTime_t)tm.tm_wday; |
565 | yday = (wxDateTime::wxDateTime_t)tm.tm_yday; | |
b76b015e VZ |
566 | } |
567 | ||
568 | bool wxDateTime::Tm::IsValid() const | |
569 | { | |
570 | // we allow for the leap seconds, although we don't use them (yet) | |
fcc3d7cb | 571 | return (year != wxDateTime::Inv_Year) && (mon != wxDateTime::Inv_Month) && |
c5a1681b | 572 | (mday <= GetNumOfDaysInMonth(year, mon)) && |
e6ec579c | 573 | (hour < 24) && (min < 60) && (sec < 62) && (msec < 1000); |
b76b015e VZ |
574 | } |
575 | ||
576 | void wxDateTime::Tm::ComputeWeekDay() | |
577 | { | |
c5a1681b VZ |
578 | // compute the week day from day/month/year: we use the dumbest algorithm |
579 | // possible: just compute our JDN and then use the (simple to derive) | |
580 | // formula: weekday = (JDN + 1.5) % 7 | |
bb8a8478 | 581 | wday = (wxDateTime::wxDateTime_t)((wxDateTime::WeekDay)(GetTruncatedJDN(mday, mon, year) + 2) % 7); |
b76b015e VZ |
582 | } |
583 | ||
e6ec579c | 584 | void wxDateTime::Tm::AddMonths(int monDiff) |
fcc3d7cb VZ |
585 | { |
586 | // normalize the months field | |
587 | while ( monDiff < -mon ) | |
588 | { | |
589 | year--; | |
590 | ||
591 | monDiff += MONTHS_IN_YEAR; | |
592 | } | |
593 | ||
2ef31e80 | 594 | while ( monDiff + mon >= MONTHS_IN_YEAR ) |
fcc3d7cb VZ |
595 | { |
596 | year++; | |
239446b4 VZ |
597 | |
598 | monDiff -= MONTHS_IN_YEAR; | |
fcc3d7cb VZ |
599 | } |
600 | ||
601 | mon = (wxDateTime::Month)(mon + monDiff); | |
602 | ||
e6ec579c | 603 | wxASSERT_MSG( mon >= 0 && mon < MONTHS_IN_YEAR, _T("logic error") ); |
4f6aed9c VZ |
604 | |
605 | // NB: we don't check here that the resulting date is valid, this function | |
606 | // is private and the caller must check it if needed | |
fcc3d7cb VZ |
607 | } |
608 | ||
e6ec579c | 609 | void wxDateTime::Tm::AddDays(int dayDiff) |
fcc3d7cb VZ |
610 | { |
611 | // normalize the days field | |
9d9b7755 | 612 | while ( dayDiff + mday < 1 ) |
fcc3d7cb VZ |
613 | { |
614 | AddMonths(-1); | |
615 | ||
9d9b7755 | 616 | dayDiff += GetNumOfDaysInMonth(year, mon); |
fcc3d7cb VZ |
617 | } |
618 | ||
907173e5 | 619 | mday = (wxDateTime::wxDateTime_t)( mday + dayDiff ); |
fcc3d7cb VZ |
620 | while ( mday > GetNumOfDaysInMonth(year, mon) ) |
621 | { | |
622 | mday -= GetNumOfDaysInMonth(year, mon); | |
623 | ||
624 | AddMonths(1); | |
625 | } | |
626 | ||
627 | wxASSERT_MSG( mday > 0 && mday <= GetNumOfDaysInMonth(year, mon), | |
628 | _T("logic error") ); | |
629 | } | |
630 | ||
631 | // ---------------------------------------------------------------------------- | |
632 | // class TimeZone | |
633 | // ---------------------------------------------------------------------------- | |
634 | ||
635 | wxDateTime::TimeZone::TimeZone(wxDateTime::TZ tz) | |
636 | { | |
637 | switch ( tz ) | |
638 | { | |
639 | case wxDateTime::Local: | |
299fcbfe VZ |
640 | // get the offset from C RTL: it returns the difference GMT-local |
641 | // while we want to have the offset _from_ GMT, hence the '-' | |
642 | m_offset = -GetTimeZone(); | |
fcc3d7cb VZ |
643 | break; |
644 | ||
645 | case wxDateTime::GMT_12: | |
646 | case wxDateTime::GMT_11: | |
647 | case wxDateTime::GMT_10: | |
648 | case wxDateTime::GMT_9: | |
649 | case wxDateTime::GMT_8: | |
650 | case wxDateTime::GMT_7: | |
651 | case wxDateTime::GMT_6: | |
652 | case wxDateTime::GMT_5: | |
653 | case wxDateTime::GMT_4: | |
654 | case wxDateTime::GMT_3: | |
655 | case wxDateTime::GMT_2: | |
656 | case wxDateTime::GMT_1: | |
299fcbfe | 657 | m_offset = -3600*(wxDateTime::GMT0 - tz); |
fcc3d7cb VZ |
658 | break; |
659 | ||
660 | case wxDateTime::GMT0: | |
661 | case wxDateTime::GMT1: | |
662 | case wxDateTime::GMT2: | |
663 | case wxDateTime::GMT3: | |
664 | case wxDateTime::GMT4: | |
665 | case wxDateTime::GMT5: | |
666 | case wxDateTime::GMT6: | |
667 | case wxDateTime::GMT7: | |
668 | case wxDateTime::GMT8: | |
669 | case wxDateTime::GMT9: | |
670 | case wxDateTime::GMT10: | |
671 | case wxDateTime::GMT11: | |
672 | case wxDateTime::GMT12: | |
299fcbfe | 673 | m_offset = 3600*(tz - wxDateTime::GMT0); |
fcc3d7cb VZ |
674 | break; |
675 | ||
676 | case wxDateTime::A_CST: | |
677 | // Central Standard Time in use in Australia = UTC + 9.5 | |
cd0b1709 | 678 | m_offset = 60l*(9*60 + 30); |
fcc3d7cb VZ |
679 | break; |
680 | ||
681 | default: | |
682 | wxFAIL_MSG( _T("unknown time zone") ); | |
683 | } | |
684 | } | |
685 | ||
b76b015e VZ |
686 | // ---------------------------------------------------------------------------- |
687 | // static functions | |
688 | // ---------------------------------------------------------------------------- | |
689 | ||
690 | /* static */ | |
691 | bool wxDateTime::IsLeapYear(int year, wxDateTime::Calendar cal) | |
692 | { | |
2f02cb89 VZ |
693 | if ( year == Inv_Year ) |
694 | year = GetCurrentYear(); | |
695 | ||
b76b015e VZ |
696 | if ( cal == Gregorian ) |
697 | { | |
698 | // in Gregorian calendar leap years are those divisible by 4 except | |
699 | // those divisible by 100 unless they're also divisible by 400 | |
700 | // (in some countries, like Russia and Greece, additional corrections | |
701 | // exist, but they won't manifest themselves until 2700) | |
702 | return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)); | |
703 | } | |
704 | else if ( cal == Julian ) | |
705 | { | |
706 | // in Julian calendar the rule is simpler | |
707 | return year % 4 == 0; | |
708 | } | |
709 | else | |
710 | { | |
711 | wxFAIL_MSG(_T("unknown calendar")); | |
712 | ||
68379eaf | 713 | return false; |
b76b015e VZ |
714 | } |
715 | } | |
716 | ||
fcc3d7cb VZ |
717 | /* static */ |
718 | int wxDateTime::GetCentury(int year) | |
719 | { | |
720 | return year > 0 ? year / 100 : year / 100 - 1; | |
721 | } | |
722 | ||
b76b015e VZ |
723 | /* static */ |
724 | int wxDateTime::ConvertYearToBC(int year) | |
725 | { | |
726 | // year 0 is BC 1 | |
727 | return year > 0 ? year : year - 1; | |
728 | } | |
729 | ||
730 | /* static */ | |
731 | int wxDateTime::GetCurrentYear(wxDateTime::Calendar cal) | |
732 | { | |
733 | switch ( cal ) | |
734 | { | |
735 | case Gregorian: | |
736 | return Now().GetYear(); | |
737 | ||
738 | case Julian: | |
739 | wxFAIL_MSG(_T("TODO")); | |
740 | break; | |
741 | ||
742 | default: | |
743 | wxFAIL_MSG(_T("unsupported calendar")); | |
744 | break; | |
745 | } | |
746 | ||
747 | return Inv_Year; | |
748 | } | |
749 | ||
750 | /* static */ | |
751 | wxDateTime::Month wxDateTime::GetCurrentMonth(wxDateTime::Calendar cal) | |
752 | { | |
753 | switch ( cal ) | |
754 | { | |
755 | case Gregorian: | |
756 | return Now().GetMonth(); | |
b76b015e VZ |
757 | |
758 | case Julian: | |
759 | wxFAIL_MSG(_T("TODO")); | |
760 | break; | |
761 | ||
762 | default: | |
763 | wxFAIL_MSG(_T("unsupported calendar")); | |
764 | break; | |
765 | } | |
766 | ||
767 | return Inv_Month; | |
768 | } | |
769 | ||
2f02cb89 VZ |
770 | /* static */ |
771 | wxDateTime::wxDateTime_t wxDateTime::GetNumberOfDays(int year, Calendar cal) | |
772 | { | |
773 | if ( year == Inv_Year ) | |
774 | { | |
775 | // take the current year if none given | |
776 | year = GetCurrentYear(); | |
777 | } | |
778 | ||
779 | switch ( cal ) | |
780 | { | |
781 | case Gregorian: | |
782 | case Julian: | |
783 | return IsLeapYear(year) ? 366 : 365; | |
2f02cb89 VZ |
784 | |
785 | default: | |
786 | wxFAIL_MSG(_T("unsupported calendar")); | |
787 | break; | |
788 | } | |
789 | ||
790 | return 0; | |
791 | } | |
792 | ||
b76b015e VZ |
793 | /* static */ |
794 | wxDateTime::wxDateTime_t wxDateTime::GetNumberOfDays(wxDateTime::Month month, | |
795 | int year, | |
796 | wxDateTime::Calendar cal) | |
797 | { | |
fcc3d7cb | 798 | wxCHECK_MSG( month < MONTHS_IN_YEAR, 0, _T("invalid month") ); |
b76b015e VZ |
799 | |
800 | if ( cal == Gregorian || cal == Julian ) | |
801 | { | |
802 | if ( year == Inv_Year ) | |
803 | { | |
804 | // take the current year if none given | |
805 | year = GetCurrentYear(); | |
806 | } | |
807 | ||
fcc3d7cb | 808 | return GetNumOfDaysInMonth(year, month); |
b76b015e VZ |
809 | } |
810 | else | |
811 | { | |
812 | wxFAIL_MSG(_T("unsupported calendar")); | |
813 | ||
814 | return 0; | |
815 | } | |
816 | } | |
817 | ||
818 | /* static */ | |
f0f951fa VZ |
819 | wxString wxDateTime::GetMonthName(wxDateTime::Month month, |
820 | wxDateTime::NameFlags flags) | |
b76b015e | 821 | { |
525d8583 | 822 | wxCHECK_MSG( month != Inv_Month, wxEmptyString, _T("invalid month") ); |
77b88df2 | 823 | #ifndef __WXWINCE__ |
68ee7c47 VZ |
824 | // notice that we must set all the fields to avoid confusing libc (GNU one |
825 | // gets confused to a crash if we don't do this) | |
cd0b1709 | 826 | tm tm; |
f0f951fa | 827 | InitTm(tm); |
cd0b1709 | 828 | tm.tm_mon = month; |
b76b015e | 829 | |
f0f951fa | 830 | return CallStrftime(flags == Name_Abbr ? _T("%b") : _T("%B"), &tm); |
77b88df2 | 831 | #else |
ba9bbf13 WS |
832 | wxString ret; |
833 | switch(month) | |
834 | { | |
835 | case Jan: | |
836 | ret = (flags == Name_Abbr ? wxT("Jan"): wxT("January")); | |
837 | break; | |
838 | case Feb: | |
839 | ret = (flags == Name_Abbr ? wxT("Feb"): wxT("Febuary")); | |
840 | break; | |
841 | case Mar: | |
842 | ret = (flags == Name_Abbr ? wxT("Mar"): wxT("March")); | |
843 | break; | |
844 | case Apr: | |
845 | ret = (flags == Name_Abbr ? wxT("Apr"): wxT("April")); | |
846 | break; | |
847 | case May: | |
848 | ret = (flags == Name_Abbr ? wxT("May"): wxT("May")); | |
849 | break; | |
850 | case Jun: | |
851 | ret = (flags == Name_Abbr ? wxT("Jun"): wxT("June")); | |
852 | break; | |
853 | case Jul: | |
854 | ret = (flags == Name_Abbr ? wxT("Jul"): wxT("July")); | |
855 | break; | |
856 | case Aug: | |
857 | ret = (flags == Name_Abbr ? wxT("Aug"): wxT("August")); | |
858 | break; | |
859 | case Sep: | |
860 | ret = (flags == Name_Abbr ? wxT("Sep"): wxT("September")); | |
861 | break; | |
862 | case Oct: | |
863 | ret = (flags == Name_Abbr ? wxT("Oct"): wxT("October")); | |
864 | break; | |
865 | case Nov: | |
866 | ret = (flags == Name_Abbr ? wxT("Nov"): wxT("November")); | |
867 | break; | |
868 | case Dec: | |
869 | ret = (flags == Name_Abbr ? wxT("Dec"): wxT("December")); | |
870 | break; | |
871 | } | |
872 | return ret; | |
77b88df2 | 873 | #endif |
b76b015e VZ |
874 | } |
875 | ||
876 | /* static */ | |
f0f951fa VZ |
877 | wxString wxDateTime::GetWeekDayName(wxDateTime::WeekDay wday, |
878 | wxDateTime::NameFlags flags) | |
b76b015e | 879 | { |
525d8583 | 880 | wxCHECK_MSG( wday != Inv_WeekDay, wxEmptyString, _T("invalid weekday") ); |
77b88df2 | 881 | #ifndef __WXWINCE__ |
2c622d71 VZ |
882 | // take some arbitrary Sunday (but notice that the day should be such that |
883 | // after adding wday to it below we still have a valid date, e.g. don't | |
884 | // take 28 here!) | |
f0f951fa VZ |
885 | tm tm; |
886 | InitTm(tm); | |
2c622d71 | 887 | tm.tm_mday = 21; |
f0f951fa VZ |
888 | tm.tm_mon = Nov; |
889 | tm.tm_year = 99; | |
b76b015e | 890 | |
1ef54dcf | 891 | // and offset it by the number of days needed to get the correct wday |
b76b015e VZ |
892 | tm.tm_mday += wday; |
893 | ||
c5a1681b VZ |
894 | // call mktime() to normalize it... |
895 | (void)mktime(&tm); | |
896 | ||
897 | // ... and call strftime() | |
f0f951fa | 898 | return CallStrftime(flags == Name_Abbr ? _T("%a") : _T("%A"), &tm); |
77b88df2 | 899 | #else |
ba9bbf13 WS |
900 | wxString ret; |
901 | switch(wday) | |
902 | { | |
903 | case Sun: | |
904 | ret = (flags == Name_Abbr ? wxT("Sun") : wxT("Sunday")); | |
905 | break; | |
906 | case Mon: | |
907 | ret = (flags == Name_Abbr ? wxT("Mon") : wxT("Monday")); | |
908 | break; | |
909 | case Tue: | |
910 | ret = (flags == Name_Abbr ? wxT("Tue") : wxT("Tuesday")); | |
911 | break; | |
912 | case Wed: | |
913 | ret = (flags == Name_Abbr ? wxT("Wed") : wxT("Wednesday")); | |
914 | break; | |
915 | case Thu: | |
916 | ret = (flags == Name_Abbr ? wxT("Thu") : wxT("Thursday")); | |
917 | break; | |
918 | case Fri: | |
919 | ret = (flags == Name_Abbr ? wxT("Fri") : wxT("Friday")); | |
920 | break; | |
921 | case Sat: | |
922 | ret = (flags == Name_Abbr ? wxT("Sat") : wxT("Saturday")); | |
923 | break; | |
924 | } | |
925 | return ret; | |
77b88df2 JS |
926 | |
927 | #endif | |
f0f951fa VZ |
928 | } |
929 | ||
930 | /* static */ | |
931 | void wxDateTime::GetAmPmStrings(wxString *am, wxString *pm) | |
932 | { | |
933 | tm tm; | |
934 | InitTm(tm); | |
653567bb | 935 | wxChar buffer[64]; |
0308fd61 JS |
936 | // @Note: Do not call 'CallStrftime' here! CallStrftime checks the return code |
937 | // and causes an assertion failed if the buffer is to small (which is good) - OR - | |
938 | // if strftime does not return anything because the format string is invalid - OR - | |
939 | // if there are no 'am' / 'pm' tokens defined for the current locale (which is not good). | |
940 | // wxDateTime::ParseTime will try several different formats to parse the time. | |
941 | // As a result, GetAmPmStrings might get called, even if the current locale | |
942 | // does not define any 'am' / 'pm' tokens. In this case, wxStrftime would | |
943 | // assert, even though it is a perfectly legal use. | |
f0f951fa VZ |
944 | if ( am ) |
945 | { | |
10a2749b | 946 | if (wxStrftime(buffer, sizeof(buffer)/sizeof(wxChar), _T("%p"), &tm) > 0) |
0308fd61 JS |
947 | *am = wxString(buffer); |
948 | else | |
949 | *am = wxString(); | |
f0f951fa VZ |
950 | } |
951 | if ( pm ) | |
952 | { | |
953 | tm.tm_hour = 13; | |
10a2749b | 954 | if (wxStrftime(buffer, sizeof(buffer)/sizeof(wxChar), _T("%p"), &tm) > 0) |
0308fd61 JS |
955 | *pm = wxString(buffer); |
956 | else | |
957 | *pm = wxString(); | |
f0f951fa | 958 | } |
b76b015e VZ |
959 | } |
960 | ||
239446b4 VZ |
961 | // ---------------------------------------------------------------------------- |
962 | // Country stuff: date calculations depend on the country (DST, work days, | |
963 | // ...), so we need to know which rules to follow. | |
964 | // ---------------------------------------------------------------------------- | |
965 | ||
966 | /* static */ | |
967 | wxDateTime::Country wxDateTime::GetCountry() | |
968 | { | |
f0f951fa | 969 | // TODO use LOCALE_ICOUNTRY setting under Win32 |
77b88df2 | 970 | #ifndef __WXWINCE__ |
239446b4 VZ |
971 | if ( ms_country == Country_Unknown ) |
972 | { | |
973 | // try to guess from the time zone name | |
974 | time_t t = time(NULL); | |
975 | struct tm *tm = localtime(&t); | |
976 | ||
977 | wxString tz = CallStrftime(_T("%Z"), tm); | |
978 | if ( tz == _T("WET") || tz == _T("WEST") ) | |
979 | { | |
980 | ms_country = UK; | |
981 | } | |
982 | else if ( tz == _T("CET") || tz == _T("CEST") ) | |
983 | { | |
984 | ms_country = Country_EEC; | |
985 | } | |
986 | else if ( tz == _T("MSK") || tz == _T("MSD") ) | |
987 | { | |
988 | ms_country = Russia; | |
989 | } | |
990 | else if ( tz == _T("AST") || tz == _T("ADT") || | |
991 | tz == _T("EST") || tz == _T("EDT") || | |
992 | tz == _T("CST") || tz == _T("CDT") || | |
993 | tz == _T("MST") || tz == _T("MDT") || | |
994 | tz == _T("PST") || tz == _T("PDT") ) | |
995 | { | |
996 | ms_country = USA; | |
997 | } | |
998 | else | |
999 | { | |
1000 | // well, choose a default one | |
1001 | ms_country = USA; | |
1002 | } | |
1003 | } | |
77b88df2 JS |
1004 | #else |
1005 | ms_country = USA; | |
1006 | #endif | |
239446b4 VZ |
1007 | |
1008 | return ms_country; | |
1009 | } | |
1010 | ||
1011 | /* static */ | |
1012 | void wxDateTime::SetCountry(wxDateTime::Country country) | |
1013 | { | |
1014 | ms_country = country; | |
1015 | } | |
1016 | ||
1017 | /* static */ | |
1018 | bool wxDateTime::IsWestEuropeanCountry(Country country) | |
1019 | { | |
1020 | if ( country == Country_Default ) | |
1021 | { | |
1022 | country = GetCountry(); | |
1023 | } | |
1024 | ||
1025 | return (Country_WesternEurope_Start <= country) && | |
1026 | (country <= Country_WesternEurope_End); | |
1027 | } | |
1028 | ||
1029 | // ---------------------------------------------------------------------------- | |
1030 | // DST calculations: we use 3 different rules for the West European countries, | |
1031 | // USA and for the rest of the world. This is undoubtedly false for many | |
1032 | // countries, but I lack the necessary info (and the time to gather it), | |
1033 | // please add the other rules here! | |
1034 | // ---------------------------------------------------------------------------- | |
1035 | ||
1036 | /* static */ | |
1037 | bool wxDateTime::IsDSTApplicable(int year, Country country) | |
1038 | { | |
1039 | if ( year == Inv_Year ) | |
1040 | { | |
1041 | // take the current year if none given | |
1042 | year = GetCurrentYear(); | |
1043 | } | |
1044 | ||
1045 | if ( country == Country_Default ) | |
1046 | { | |
1047 | country = GetCountry(); | |
1048 | } | |
1049 | ||
1050 | switch ( country ) | |
1051 | { | |
1052 | case USA: | |
1053 | case UK: | |
1054 | // DST was first observed in the US and UK during WWI, reused | |
1055 | // during WWII and used again since 1966 | |
1056 | return year >= 1966 || | |
1057 | (year >= 1942 && year <= 1945) || | |
1058 | (year == 1918 || year == 1919); | |
1059 | ||
1060 | default: | |
1061 | // assume that it started after WWII | |
1062 | return year > 1950; | |
1063 | } | |
1064 | } | |
1065 | ||
1066 | /* static */ | |
1067 | wxDateTime wxDateTime::GetBeginDST(int year, Country country) | |
1068 | { | |
1069 | if ( year == Inv_Year ) | |
1070 | { | |
1071 | // take the current year if none given | |
1072 | year = GetCurrentYear(); | |
1073 | } | |
1074 | ||
1075 | if ( country == Country_Default ) | |
1076 | { | |
1077 | country = GetCountry(); | |
1078 | } | |
1079 | ||
1080 | if ( !IsDSTApplicable(year, country) ) | |
1081 | { | |
2ef31e80 | 1082 | return wxInvalidDateTime; |
239446b4 VZ |
1083 | } |
1084 | ||
1085 | wxDateTime dt; | |
1086 | ||
1087 | if ( IsWestEuropeanCountry(country) || (country == Russia) ) | |
1088 | { | |
1089 | // DST begins at 1 a.m. GMT on the last Sunday of March | |
1090 | if ( !dt.SetToLastWeekDay(Sun, Mar, year) ) | |
1091 | { | |
1092 | // weird... | |
1093 | wxFAIL_MSG( _T("no last Sunday in March?") ); | |
1094 | } | |
1095 | ||
1096 | dt += wxTimeSpan::Hours(1); | |
1097 | ||
41acf5c0 | 1098 | // disable DST tests because it could result in an infinite recursion! |
68379eaf | 1099 | dt.MakeGMT(true); |
239446b4 VZ |
1100 | } |
1101 | else switch ( country ) | |
1102 | { | |
1103 | case USA: | |
1104 | switch ( year ) | |
1105 | { | |
1106 | case 1918: | |
1107 | case 1919: | |
1108 | // don't know for sure - assume it was in effect all year | |
1109 | ||
1110 | case 1943: | |
1111 | case 1944: | |
1112 | case 1945: | |
1113 | dt.Set(1, Jan, year); | |
1114 | break; | |
1115 | ||
1116 | case 1942: | |
1117 | // DST was installed Feb 2, 1942 by the Congress | |
1118 | dt.Set(2, Feb, year); | |
1119 | break; | |
1120 | ||
1121 | // Oil embargo changed the DST period in the US | |
1122 | case 1974: | |
1123 | dt.Set(6, Jan, 1974); | |
1124 | break; | |
1125 | ||
1126 | case 1975: | |
1127 | dt.Set(23, Feb, 1975); | |
1128 | break; | |
1129 | ||
1130 | default: | |
1131 | // before 1986, DST begun on the last Sunday of April, but | |
1132 | // in 1986 Reagan changed it to begin at 2 a.m. of the | |
1133 | // first Sunday in April | |
1134 | if ( year < 1986 ) | |
1135 | { | |
1136 | if ( !dt.SetToLastWeekDay(Sun, Apr, year) ) | |
1137 | { | |
1138 | // weird... | |
1139 | wxFAIL_MSG( _T("no first Sunday in April?") ); | |
1140 | } | |
1141 | } | |
1142 | else | |
1143 | { | |
1144 | if ( !dt.SetToWeekDay(Sun, 1, Apr, year) ) | |
1145 | { | |
1146 | // weird... | |
1147 | wxFAIL_MSG( _T("no first Sunday in April?") ); | |
1148 | } | |
1149 | } | |
1150 | ||
1151 | dt += wxTimeSpan::Hours(2); | |
1152 | ||
1153 | // TODO what about timezone?? | |
1154 | } | |
1155 | ||
1156 | break; | |
1157 | ||
1158 | default: | |
1159 | // assume Mar 30 as the start of the DST for the rest of the world | |
1160 | // - totally bogus, of course | |
1161 | dt.Set(30, Mar, year); | |
1162 | } | |
1163 | ||
1164 | return dt; | |
1165 | } | |
1166 | ||
1167 | /* static */ | |
1168 | wxDateTime wxDateTime::GetEndDST(int year, Country country) | |
1169 | { | |
1170 | if ( year == Inv_Year ) | |
1171 | { | |
1172 | // take the current year if none given | |
1173 | year = GetCurrentYear(); | |
1174 | } | |
1175 | ||
1176 | if ( country == Country_Default ) | |
1177 | { | |
1178 | country = GetCountry(); | |
1179 | } | |
1180 | ||
1181 | if ( !IsDSTApplicable(year, country) ) | |
1182 | { | |
2ef31e80 | 1183 | return wxInvalidDateTime; |
239446b4 VZ |
1184 | } |
1185 | ||
1186 | wxDateTime dt; | |
1187 | ||
1188 | if ( IsWestEuropeanCountry(country) || (country == Russia) ) | |
1189 | { | |
5f287370 | 1190 | // DST ends at 1 a.m. GMT on the last Sunday of October |
239446b4 VZ |
1191 | if ( !dt.SetToLastWeekDay(Sun, Oct, year) ) |
1192 | { | |
1193 | // weirder and weirder... | |
1194 | wxFAIL_MSG( _T("no last Sunday in October?") ); | |
1195 | } | |
1196 | ||
1197 | dt += wxTimeSpan::Hours(1); | |
1198 | ||
41acf5c0 | 1199 | // disable DST tests because it could result in an infinite recursion! |
68379eaf | 1200 | dt.MakeGMT(true); |
239446b4 VZ |
1201 | } |
1202 | else switch ( country ) | |
1203 | { | |
1204 | case USA: | |
1205 | switch ( year ) | |
1206 | { | |
1207 | case 1918: | |
1208 | case 1919: | |
1209 | // don't know for sure - assume it was in effect all year | |
1210 | ||
1211 | case 1943: | |
1212 | case 1944: | |
1213 | dt.Set(31, Dec, year); | |
1214 | break; | |
1215 | ||
1216 | case 1945: | |
1217 | // the time was reset after the end of the WWII | |
1218 | dt.Set(30, Sep, year); | |
1219 | break; | |
1220 | ||
1221 | default: | |
5f287370 | 1222 | // DST ends at 2 a.m. on the last Sunday of October |
239446b4 VZ |
1223 | if ( !dt.SetToLastWeekDay(Sun, Oct, year) ) |
1224 | { | |
1225 | // weirder and weirder... | |
1226 | wxFAIL_MSG( _T("no last Sunday in October?") ); | |
1227 | } | |
1228 | ||
1229 | dt += wxTimeSpan::Hours(2); | |
1230 | ||
1231 | // TODO what about timezone?? | |
1232 | } | |
1233 | break; | |
1234 | ||
1235 | default: | |
1236 | // assume October 26th as the end of the DST - totally bogus too | |
1237 | dt.Set(26, Oct, year); | |
1238 | } | |
1239 | ||
1240 | return dt; | |
1241 | } | |
1242 | ||
0979c962 VZ |
1243 | // ---------------------------------------------------------------------------- |
1244 | // constructors and assignment operators | |
1245 | // ---------------------------------------------------------------------------- | |
1246 | ||
f6bcfd97 BP |
1247 | // return the current time with ms precision |
1248 | /* static */ wxDateTime wxDateTime::UNow() | |
1249 | { | |
1250 | return wxDateTime(wxGetLocalTimeMillis()); | |
1251 | } | |
1252 | ||
299fcbfe VZ |
1253 | // the values in the tm structure contain the local time |
1254 | wxDateTime& wxDateTime::Set(const struct tm& tm) | |
0979c962 | 1255 | { |
299fcbfe | 1256 | struct tm tm2(tm); |
b76b015e | 1257 | time_t timet = mktime(&tm2); |
1ef54dcf | 1258 | |
4afd7529 | 1259 | if ( timet == (time_t)-1 ) |
0979c962 | 1260 | { |
4afd7529 VZ |
1261 | // mktime() rather unintuitively fails for Jan 1, 1970 if the hour is |
1262 | // less than timezone - try to make it work for this case | |
1263 | if ( tm2.tm_year == 70 && tm2.tm_mon == 0 && tm2.tm_mday == 1 ) | |
1264 | { | |
3d78a532 RD |
1265 | return Set((time_t)( |
1266 | GetTimeZone() + | |
1267 | tm2.tm_hour * MIN_PER_HOUR * SEC_PER_MIN + | |
1268 | tm2.tm_min * SEC_PER_MIN + | |
1269 | tm2.tm_sec)); | |
4afd7529 VZ |
1270 | } |
1271 | ||
1272 | wxFAIL_MSG( _T("mktime() failed") ); | |
0979c962 | 1273 | |
384223b3 VZ |
1274 | *this = wxInvalidDateTime; |
1275 | ||
1276 | return *this; | |
0979c962 VZ |
1277 | } |
1278 | else | |
1279 | { | |
1280 | return Set(timet); | |
1281 | } | |
1282 | } | |
1283 | ||
1284 | wxDateTime& wxDateTime::Set(wxDateTime_t hour, | |
1285 | wxDateTime_t minute, | |
1286 | wxDateTime_t second, | |
1287 | wxDateTime_t millisec) | |
1288 | { | |
1289 | // we allow seconds to be 61 to account for the leap seconds, even if we | |
1290 | // don't use them really | |
384223b3 VZ |
1291 | wxDATETIME_CHECK( hour < 24 && |
1292 | second < 62 && | |
1293 | minute < 60 && | |
1294 | millisec < 1000, | |
1295 | _T("Invalid time in wxDateTime::Set()") ); | |
0979c962 VZ |
1296 | |
1297 | // get the current date from system | |
9d9b7755 | 1298 | struct tm *tm = GetTmNow(); |
299fcbfe | 1299 | |
384223b3 | 1300 | wxDATETIME_CHECK( tm, _T("localtime() failed") ); |
0979c962 | 1301 | |
456639ac VZ |
1302 | // make a copy so it isn't clobbered by the call to mktime() below |
1303 | struct tm tm1(*tm); | |
1304 | ||
0979c962 | 1305 | // adjust the time |
456639ac VZ |
1306 | tm1.tm_hour = hour; |
1307 | tm1.tm_min = minute; | |
1308 | tm1.tm_sec = second; | |
0979c962 | 1309 | |
9cc311d3 | 1310 | // and the DST in case it changes on this date |
456639ac | 1311 | struct tm tm2(tm1); |
9cc311d3 | 1312 | mktime(&tm2); |
456639ac VZ |
1313 | if ( tm2.tm_isdst != tm1.tm_isdst ) |
1314 | tm1.tm_isdst = tm2.tm_isdst; | |
9cc311d3 | 1315 | |
456639ac | 1316 | (void)Set(tm1); |
0979c962 VZ |
1317 | |
1318 | // and finally adjust milliseconds | |
1319 | return SetMillisecond(millisec); | |
1320 | } | |
1321 | ||
1322 | wxDateTime& wxDateTime::Set(wxDateTime_t day, | |
1323 | Month month, | |
1324 | int year, | |
1325 | wxDateTime_t hour, | |
1326 | wxDateTime_t minute, | |
1327 | wxDateTime_t second, | |
1328 | wxDateTime_t millisec) | |
1329 | { | |
384223b3 VZ |
1330 | wxDATETIME_CHECK( hour < 24 && |
1331 | second < 62 && | |
1332 | minute < 60 && | |
1333 | millisec < 1000, | |
1334 | _T("Invalid time in wxDateTime::Set()") ); | |
0979c962 | 1335 | |
2f02cb89 | 1336 | ReplaceDefaultYearMonthWithCurrent(&year, &month); |
0979c962 | 1337 | |
384223b3 VZ |
1338 | wxDATETIME_CHECK( (0 < day) && (day <= GetNumberOfDays(month, year)), |
1339 | _T("Invalid date in wxDateTime::Set()") ); | |
0979c962 VZ |
1340 | |
1341 | // the range of time_t type (inclusive) | |
1342 | static const int yearMinInRange = 1970; | |
1343 | static const int yearMaxInRange = 2037; | |
1344 | ||
1345 | // test only the year instead of testing for the exact end of the Unix | |
1346 | // time_t range - it doesn't bring anything to do more precise checks | |
2f02cb89 | 1347 | if ( year >= yearMinInRange && year <= yearMaxInRange ) |
0979c962 VZ |
1348 | { |
1349 | // use the standard library version if the date is in range - this is | |
b76b015e | 1350 | // probably more efficient than our code |
0979c962 | 1351 | struct tm tm; |
b76b015e | 1352 | tm.tm_year = year - 1900; |
0979c962 VZ |
1353 | tm.tm_mon = month; |
1354 | tm.tm_mday = day; | |
1355 | tm.tm_hour = hour; | |
1356 | tm.tm_min = minute; | |
1357 | tm.tm_sec = second; | |
299fcbfe | 1358 | tm.tm_isdst = -1; // mktime() will guess it |
0979c962 VZ |
1359 | |
1360 | (void)Set(tm); | |
1361 | ||
1362 | // and finally adjust milliseconds | |
3d78a532 RD |
1363 | if (IsValid()) |
1364 | SetMillisecond(millisec); | |
1365 | ||
1366 | return *this; | |
0979c962 VZ |
1367 | } |
1368 | else | |
1369 | { | |
1370 | // do time calculations ourselves: we want to calculate the number of | |
fcc3d7cb | 1371 | // milliseconds between the given date and the epoch |
e6ec579c VZ |
1372 | |
1373 | // get the JDN for the midnight of this day | |
1374 | m_time = GetTruncatedJDN(day, month, year); | |
1375 | m_time -= EPOCH_JDN; | |
1376 | m_time *= SECONDS_PER_DAY * TIME_T_FACTOR; | |
1377 | ||
299fcbfe VZ |
1378 | // JDN corresponds to GMT, we take localtime |
1379 | Add(wxTimeSpan(hour, minute, second + GetTimeZone(), millisec)); | |
b76b015e VZ |
1380 | } |
1381 | ||
1382 | return *this; | |
1383 | } | |
1384 | ||
e6ec579c VZ |
1385 | wxDateTime& wxDateTime::Set(double jdn) |
1386 | { | |
1ef54dcf VZ |
1387 | // so that m_time will be 0 for the midnight of Jan 1, 1970 which is jdn |
1388 | // EPOCH_JDN + 0.5 | |
1389 | jdn -= EPOCH_JDN + 0.5; | |
1390 | ||
cd0b1709 VZ |
1391 | jdn *= MILLISECONDS_PER_DAY; |
1392 | ||
4f0436e4 VZ |
1393 | m_time.Assign(jdn); |
1394 | ||
8cc00d5f VZ |
1395 | // JDNs always suppose an UTC date, so bring it back to local time zone |
1396 | // (also see GetJulianDayNumber() implementation) | |
1397 | long tzDiff = GetTimeZone(); | |
1398 | if ( IsDST() == 1 ) | |
1399 | { | |
1400 | // FIXME: again, we suppose that DST is always one hour | |
1401 | tzDiff -= 3600; | |
1402 | } | |
1403 | ||
4f0436e4 | 1404 | m_time += tzDiff*1000; // tzDiff is in seconds |
e6ec579c VZ |
1405 | |
1406 | return *this; | |
1407 | } | |
1408 | ||
9d9b7755 VZ |
1409 | wxDateTime& wxDateTime::ResetTime() |
1410 | { | |
1411 | Tm tm = GetTm(); | |
1412 | ||
1413 | if ( tm.hour || tm.min || tm.sec || tm.msec ) | |
1414 | { | |
1415 | tm.msec = | |
1416 | tm.sec = | |
1417 | tm.min = | |
1418 | tm.hour = 0; | |
1419 | ||
1420 | Set(tm); | |
1421 | } | |
1422 | ||
1423 | return *this; | |
1424 | } | |
1425 | ||
2b5f62a0 VZ |
1426 | // ---------------------------------------------------------------------------- |
1427 | // DOS Date and Time Format functions | |
1428 | // ---------------------------------------------------------------------------- | |
1429 | // the dos date and time value is an unsigned 32 bit value in the format: | |
1430 | // YYYYYYYMMMMDDDDDhhhhhmmmmmmsssss | |
1431 | // | |
1432 | // Y = year offset from 1980 (0-127) | |
1433 | // M = month (1-12) | |
1434 | // D = day of month (1-31) | |
1435 | // h = hour (0-23) | |
1436 | // m = minute (0-59) | |
1437 | // s = bisecond (0-29) each bisecond indicates two seconds | |
1438 | // ---------------------------------------------------------------------------- | |
1439 | ||
1440 | wxDateTime& wxDateTime::SetFromDOS(unsigned long ddt) | |
1441 | { | |
1442 | struct tm tm; | |
4739346e | 1443 | InitTm(tm); |
2b5f62a0 VZ |
1444 | |
1445 | long year = ddt & 0xFE000000; | |
1446 | year >>= 25; | |
1447 | year += 80; | |
1448 | tm.tm_year = year; | |
1449 | ||
1450 | long month = ddt & 0x1E00000; | |
1451 | month >>= 21; | |
1452 | month -= 1; | |
1453 | tm.tm_mon = month; | |
1454 | ||
1455 | long day = ddt & 0x1F0000; | |
1456 | day >>= 16; | |
1457 | tm.tm_mday = day; | |
1458 | ||
1459 | long hour = ddt & 0xF800; | |
1460 | hour >>= 11; | |
1461 | tm.tm_hour = hour; | |
1462 | ||
1463 | long minute = ddt & 0x7E0; | |
1464 | minute >>= 5; | |
1465 | tm.tm_min = minute; | |
1466 | ||
1467 | long second = ddt & 0x1F; | |
1468 | tm.tm_sec = second * 2; | |
1469 | ||
1470 | return Set(mktime(&tm)); | |
1471 | } | |
1472 | ||
1473 | unsigned long wxDateTime::GetAsDOS() const | |
1474 | { | |
1475 | unsigned long ddt; | |
1476 | time_t ticks = GetTicks(); | |
1477 | struct tm *tm = localtime(&ticks); | |
1478 | ||
1479 | long year = tm->tm_year; | |
1480 | year -= 80; | |
1481 | year <<= 25; | |
1482 | ||
1483 | long month = tm->tm_mon; | |
1484 | month += 1; | |
1485 | month <<= 21; | |
1486 | ||
1487 | long day = tm->tm_mday; | |
1488 | day <<= 16; | |
1489 | ||
1490 | long hour = tm->tm_hour; | |
1491 | hour <<= 11; | |
1492 | ||
1493 | long minute = tm->tm_min; | |
1494 | minute <<= 5; | |
1495 | ||
1496 | long second = tm->tm_sec; | |
1497 | second /= 2; | |
1498 | ||
1499 | ddt = year | month | day | hour | minute | second; | |
1500 | return ddt; | |
1501 | } | |
1502 | ||
b76b015e VZ |
1503 | // ---------------------------------------------------------------------------- |
1504 | // time_t <-> broken down time conversions | |
1505 | // ---------------------------------------------------------------------------- | |
1506 | ||
299fcbfe | 1507 | wxDateTime::Tm wxDateTime::GetTm(const TimeZone& tz) const |
b76b015e VZ |
1508 | { |
1509 | wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") ); | |
1510 | ||
1511 | time_t time = GetTicks(); | |
1512 | if ( time != (time_t)-1 ) | |
1513 | { | |
1514 | // use C RTL functions | |
299fcbfe VZ |
1515 | tm *tm; |
1516 | if ( tz.GetOffset() == -GetTimeZone() ) | |
1517 | { | |
1518 | // we are working with local time | |
1519 | tm = localtime(&time); | |
c5a1681b VZ |
1520 | |
1521 | // should never happen | |
f6bcfd97 | 1522 | wxCHECK_MSG( tm, Tm(), _T("localtime() failed") ); |
299fcbfe VZ |
1523 | } |
1524 | else | |
1525 | { | |
13111b2a | 1526 | time += (time_t)tz.GetOffset(); |
33ac7e6f | 1527 | #if defined(__VMS__) || defined(__WATCOMC__) // time is unsigned so avoid warning |
13111b2a | 1528 | int time2 = (int) time; |
9d9b7755 | 1529 | if ( time2 >= 0 ) |
fb10f04c | 1530 | #else |
9d9b7755 | 1531 | if ( time >= 0 ) |
fb10f04c | 1532 | #endif |
c5a1681b VZ |
1533 | { |
1534 | tm = gmtime(&time); | |
b76b015e | 1535 | |
c5a1681b VZ |
1536 | // should never happen |
1537 | wxCHECK_MSG( tm, Tm(), _T("gmtime() failed") ); | |
1538 | } | |
1539 | else | |
1540 | { | |
1541 | tm = (struct tm *)NULL; | |
1542 | } | |
1543 | } | |
b76b015e | 1544 | |
c5a1681b VZ |
1545 | if ( tm ) |
1546 | { | |
f6bcfd97 BP |
1547 | // adjust the milliseconds |
1548 | Tm tm2(*tm, tz); | |
1549 | long timeOnly = (m_time % MILLISECONDS_PER_DAY).ToLong(); | |
1550 | tm2.msec = (wxDateTime_t)(timeOnly % 1000); | |
1551 | return tm2; | |
c5a1681b VZ |
1552 | } |
1553 | //else: use generic code below | |
b76b015e | 1554 | } |
e6ec579c | 1555 | |
c5a1681b VZ |
1556 | // remember the time and do the calculations with the date only - this |
1557 | // eliminates rounding errors of the floating point arithmetics | |
299fcbfe | 1558 | |
c5a1681b | 1559 | wxLongLong timeMidnight = m_time + tz.GetOffset() * 1000; |
1ef54dcf | 1560 | |
c5a1681b | 1561 | long timeOnly = (timeMidnight % MILLISECONDS_PER_DAY).ToLong(); |
1ef54dcf | 1562 | |
c5a1681b VZ |
1563 | // we want to always have positive time and timeMidnight to be really |
1564 | // the midnight before it | |
1565 | if ( timeOnly < 0 ) | |
1566 | { | |
1567 | timeOnly = MILLISECONDS_PER_DAY + timeOnly; | |
1568 | } | |
e6ec579c | 1569 | |
c5a1681b | 1570 | timeMidnight -= timeOnly; |
1ef54dcf | 1571 | |
c5a1681b VZ |
1572 | // calculate the Gregorian date from JDN for the midnight of our date: |
1573 | // this will yield day, month (in 1..12 range) and year | |
1ef54dcf | 1574 | |
c5a1681b VZ |
1575 | // actually, this is the JDN for the noon of the previous day |
1576 | long jdn = (timeMidnight / MILLISECONDS_PER_DAY).ToLong() + EPOCH_JDN; | |
1ef54dcf | 1577 | |
c5a1681b | 1578 | // CREDIT: code below is by Scott E. Lee (but bugs are mine) |
1ef54dcf | 1579 | |
c5a1681b | 1580 | wxASSERT_MSG( jdn > -2, _T("JDN out of range") ); |
1ef54dcf | 1581 | |
c5a1681b | 1582 | // calculate the century |
479cd5de VZ |
1583 | long temp = (jdn + JDN_OFFSET) * 4 - 1; |
1584 | long century = temp / DAYS_PER_400_YEARS; | |
1ef54dcf | 1585 | |
c5a1681b VZ |
1586 | // then the year and day of year (1 <= dayOfYear <= 366) |
1587 | temp = ((temp % DAYS_PER_400_YEARS) / 4) * 4 + 3; | |
479cd5de VZ |
1588 | long year = (century * 100) + (temp / DAYS_PER_4_YEARS); |
1589 | long dayOfYear = (temp % DAYS_PER_4_YEARS) / 4 + 1; | |
1ef54dcf | 1590 | |
c5a1681b VZ |
1591 | // and finally the month and day of the month |
1592 | temp = dayOfYear * 5 - 3; | |
479cd5de VZ |
1593 | long month = temp / DAYS_PER_5_MONTHS; |
1594 | long day = (temp % DAYS_PER_5_MONTHS) / 5 + 1; | |
c5a1681b VZ |
1595 | |
1596 | // month is counted from March - convert to normal | |
1597 | if ( month < 10 ) | |
1598 | { | |
1599 | month += 3; | |
1600 | } | |
1601 | else | |
1602 | { | |
1603 | year += 1; | |
1604 | month -= 9; | |
1605 | } | |
1ef54dcf | 1606 | |
c5a1681b VZ |
1607 | // year is offset by 4800 |
1608 | year -= 4800; | |
1ef54dcf | 1609 | |
c5a1681b VZ |
1610 | // check that the algorithm gave us something reasonable |
1611 | wxASSERT_MSG( (0 < month) && (month <= 12), _T("invalid month") ); | |
1612 | wxASSERT_MSG( (1 <= day) && (day < 32), _T("invalid day") ); | |
e6ec579c | 1613 | |
c5a1681b VZ |
1614 | // construct Tm from these values |
1615 | Tm tm; | |
1616 | tm.year = (int)year; | |
1617 | tm.mon = (Month)(month - 1); // algorithm yields 1 for January, not 0 | |
1618 | tm.mday = (wxDateTime_t)day; | |
479cd5de | 1619 | tm.msec = (wxDateTime_t)(timeOnly % 1000); |
c5a1681b VZ |
1620 | timeOnly -= tm.msec; |
1621 | timeOnly /= 1000; // now we have time in seconds | |
e6ec579c | 1622 | |
479cd5de | 1623 | tm.sec = (wxDateTime_t)(timeOnly % 60); |
c5a1681b VZ |
1624 | timeOnly -= tm.sec; |
1625 | timeOnly /= 60; // now we have time in minutes | |
e6ec579c | 1626 | |
479cd5de | 1627 | tm.min = (wxDateTime_t)(timeOnly % 60); |
c5a1681b | 1628 | timeOnly -= tm.min; |
e6ec579c | 1629 | |
479cd5de | 1630 | tm.hour = (wxDateTime_t)(timeOnly / 60); |
b76b015e | 1631 | |
c5a1681b | 1632 | return tm; |
b76b015e VZ |
1633 | } |
1634 | ||
1635 | wxDateTime& wxDateTime::SetYear(int year) | |
1636 | { | |
1637 | wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") ); | |
1638 | ||
1639 | Tm tm(GetTm()); | |
1640 | tm.year = year; | |
1641 | Set(tm); | |
1642 | ||
1643 | return *this; | |
1644 | } | |
1645 | ||
1646 | wxDateTime& wxDateTime::SetMonth(Month month) | |
1647 | { | |
1648 | wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") ); | |
1649 | ||
1650 | Tm tm(GetTm()); | |
1651 | tm.mon = month; | |
1652 | Set(tm); | |
1653 | ||
1654 | return *this; | |
1655 | } | |
1656 | ||
1657 | wxDateTime& wxDateTime::SetDay(wxDateTime_t mday) | |
1658 | { | |
1659 | wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") ); | |
1660 | ||
1661 | Tm tm(GetTm()); | |
1662 | tm.mday = mday; | |
1663 | Set(tm); | |
1664 | ||
1665 | return *this; | |
1666 | } | |
1667 | ||
1668 | wxDateTime& wxDateTime::SetHour(wxDateTime_t hour) | |
1669 | { | |
1670 | wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") ); | |
1671 | ||
1672 | Tm tm(GetTm()); | |
1673 | tm.hour = hour; | |
1674 | Set(tm); | |
1675 | ||
1676 | return *this; | |
1677 | } | |
1678 | ||
1679 | wxDateTime& wxDateTime::SetMinute(wxDateTime_t min) | |
1680 | { | |
1681 | wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") ); | |
1682 | ||
1683 | Tm tm(GetTm()); | |
1684 | tm.min = min; | |
1685 | Set(tm); | |
1686 | ||
1687 | return *this; | |
1688 | } | |
1689 | ||
1690 | wxDateTime& wxDateTime::SetSecond(wxDateTime_t sec) | |
1691 | { | |
1692 | wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") ); | |
1693 | ||
1694 | Tm tm(GetTm()); | |
1695 | tm.sec = sec; | |
1696 | Set(tm); | |
0979c962 VZ |
1697 | |
1698 | return *this; | |
1699 | } | |
b76b015e VZ |
1700 | |
1701 | wxDateTime& wxDateTime::SetMillisecond(wxDateTime_t millisecond) | |
1702 | { | |
1703 | wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") ); | |
1704 | ||
1705 | // we don't need to use GetTm() for this one | |
1706 | m_time -= m_time % 1000l; | |
1707 | m_time += millisecond; | |
1708 | ||
1709 | return *this; | |
1710 | } | |
1711 | ||
1712 | // ---------------------------------------------------------------------------- | |
1713 | // wxDateTime arithmetics | |
1714 | // ---------------------------------------------------------------------------- | |
1715 | ||
1716 | wxDateTime& wxDateTime::Add(const wxDateSpan& diff) | |
1717 | { | |
1718 | Tm tm(GetTm()); | |
1719 | ||
1720 | tm.year += diff.GetYears(); | |
fcc3d7cb | 1721 | tm.AddMonths(diff.GetMonths()); |
4f6aed9c VZ |
1722 | |
1723 | // check that the resulting date is valid | |
1724 | if ( tm.mday > GetNumOfDaysInMonth(tm.year, tm.mon) ) | |
1725 | { | |
1726 | // We suppose that when adding one month to Jan 31 we want to get Feb | |
1727 | // 28 (or 29), i.e. adding a month to the last day of the month should | |
1728 | // give the last day of the next month which is quite logical. | |
1729 | // | |
1730 | // Unfortunately, there is no logic way to understand what should | |
1731 | // Jan 30 + 1 month be - Feb 28 too or Feb 27 (assuming non leap year)? | |
1732 | // We make it Feb 28 (last day too), but it is highly questionable. | |
1733 | tm.mday = GetNumOfDaysInMonth(tm.year, tm.mon); | |
1734 | } | |
1735 | ||
fcc3d7cb | 1736 | tm.AddDays(diff.GetTotalDays()); |
b76b015e VZ |
1737 | |
1738 | Set(tm); | |
1739 | ||
9d9b7755 VZ |
1740 | wxASSERT_MSG( IsSameTime(tm), |
1741 | _T("Add(wxDateSpan) shouldn't modify time") ); | |
1742 | ||
b76b015e VZ |
1743 | return *this; |
1744 | } | |
1745 | ||
2f02cb89 VZ |
1746 | // ---------------------------------------------------------------------------- |
1747 | // Weekday and monthday stuff | |
1748 | // ---------------------------------------------------------------------------- | |
1749 | ||
4c27e2fa VZ |
1750 | // convert Sun, Mon, ..., Sat into 6, 0, ..., 5 |
1751 | static inline int ConvertWeekDayToMondayBase(int wd) | |
1752 | { | |
1753 | return wd == wxDateTime::Sun ? 6 : wd - 1; | |
1754 | } | |
1755 | ||
1756 | /* static */ | |
1757 | wxDateTime | |
1758 | wxDateTime::SetToWeekOfYear(int year, wxDateTime_t numWeek, WeekDay wd) | |
4f6aed9c | 1759 | { |
2b5f62a0 VZ |
1760 | wxASSERT_MSG( numWeek > 0, |
1761 | _T("invalid week number: weeks are counted from 1") ); | |
1762 | ||
4c27e2fa VZ |
1763 | // Jan 4 always lies in the 1st week of the year |
1764 | wxDateTime dt(4, Jan, year); | |
1765 | dt.SetToWeekDayInSameWeek(wd); | |
1766 | dt += wxDateSpan::Weeks(numWeek - 1); | |
1767 | ||
1768 | return dt; | |
1769 | } | |
4f6aed9c | 1770 | |
4c27e2fa VZ |
1771 | // use a separate function to avoid warnings about using deprecated |
1772 | // SetToTheWeek in GetWeek below | |
1773 | static wxDateTime | |
1774 | SetToTheWeek(int year, | |
1775 | wxDateTime::wxDateTime_t numWeek, | |
1776 | wxDateTime::WeekDay weekday, | |
1777 | wxDateTime::WeekFlags flags) | |
1778 | { | |
4f6aed9c | 1779 | // Jan 4 always lies in the 1st week of the year |
4c27e2fa VZ |
1780 | wxDateTime dt(4, wxDateTime::Jan, year); |
1781 | dt.SetToWeekDayInSameWeek(weekday, flags); | |
1782 | dt += wxDateSpan::Weeks(numWeek - 1); | |
4f6aed9c | 1783 | |
4c27e2fa VZ |
1784 | return dt; |
1785 | } | |
1786 | ||
1787 | bool wxDateTime::SetToTheWeek(wxDateTime_t numWeek, | |
1788 | WeekDay weekday, | |
1789 | WeekFlags flags) | |
1790 | { | |
1791 | int year = GetYear(); | |
1792 | *this = ::SetToTheWeek(year, numWeek, weekday, flags); | |
4f6aed9c VZ |
1793 | if ( GetYear() != year ) |
1794 | { | |
1795 | // oops... numWeek was too big | |
68379eaf | 1796 | return false; |
4f6aed9c VZ |
1797 | } |
1798 | ||
68379eaf | 1799 | return true; |
4f6aed9c VZ |
1800 | } |
1801 | ||
4c27e2fa VZ |
1802 | wxDateTime wxDateTime::GetWeek(wxDateTime_t numWeek, |
1803 | WeekDay weekday, | |
1804 | WeekFlags flags) const | |
1805 | { | |
1806 | return ::SetToTheWeek(GetYear(), numWeek, weekday, flags); | |
1807 | } | |
1808 | ||
2f02cb89 VZ |
1809 | wxDateTime& wxDateTime::SetToLastMonthDay(Month month, |
1810 | int year) | |
1811 | { | |
1812 | // take the current month/year if none specified | |
1a8557b1 VZ |
1813 | if ( year == Inv_Year ) |
1814 | year = GetYear(); | |
1815 | if ( month == Inv_Month ) | |
1816 | month = GetMonth(); | |
2f02cb89 | 1817 | |
fcc3d7cb | 1818 | return Set(GetNumOfDaysInMonth(year, month), month, year); |
2f02cb89 VZ |
1819 | } |
1820 | ||
2b5f62a0 | 1821 | wxDateTime& wxDateTime::SetToWeekDayInSameWeek(WeekDay weekday, WeekFlags flags) |
cd0b1709 | 1822 | { |
384223b3 | 1823 | wxDATETIME_CHECK( weekday != Inv_WeekDay, _T("invalid weekday") ); |
cd0b1709 | 1824 | |
2b5f62a0 | 1825 | int wdayThis = GetWeekDay(); |
cd0b1709 VZ |
1826 | if ( weekday == wdayThis ) |
1827 | { | |
1828 | // nothing to do | |
1829 | return *this; | |
1830 | } | |
2b5f62a0 VZ |
1831 | |
1832 | if ( flags == Default_First ) | |
1833 | { | |
1834 | flags = GetCountry() == USA ? Sunday_First : Monday_First; | |
1835 | } | |
1836 | ||
1837 | // the logic below based on comparing weekday and wdayThis works if Sun (0) | |
1838 | // is the first day in the week, but breaks down for Monday_First case so | |
1839 | // we adjust the week days in this case | |
1840 | if( flags == Monday_First ) | |
1841 | { | |
1842 | if ( wdayThis == Sun ) | |
1843 | wdayThis += 7; | |
1844 | } | |
1845 | //else: Sunday_First, nothing to do | |
1846 | ||
1847 | // go forward or back in time to the day we want | |
1848 | if ( weekday < wdayThis ) | |
cd0b1709 | 1849 | { |
f6bcfd97 | 1850 | return Subtract(wxDateSpan::Days(wdayThis - weekday)); |
cd0b1709 VZ |
1851 | } |
1852 | else // weekday > wdayThis | |
1853 | { | |
4f6aed9c | 1854 | return Add(wxDateSpan::Days(weekday - wdayThis)); |
cd0b1709 VZ |
1855 | } |
1856 | } | |
1857 | ||
1858 | wxDateTime& wxDateTime::SetToNextWeekDay(WeekDay weekday) | |
1859 | { | |
384223b3 | 1860 | wxDATETIME_CHECK( weekday != Inv_WeekDay, _T("invalid weekday") ); |
cd0b1709 VZ |
1861 | |
1862 | int diff; | |
1863 | WeekDay wdayThis = GetWeekDay(); | |
1864 | if ( weekday == wdayThis ) | |
1865 | { | |
1866 | // nothing to do | |
1867 | return *this; | |
1868 | } | |
1869 | else if ( weekday < wdayThis ) | |
1870 | { | |
1871 | // need to advance a week | |
1872 | diff = 7 - (wdayThis - weekday); | |
1873 | } | |
1874 | else // weekday > wdayThis | |
1875 | { | |
1876 | diff = weekday - wdayThis; | |
1877 | } | |
1878 | ||
4f6aed9c | 1879 | return Add(wxDateSpan::Days(diff)); |
cd0b1709 VZ |
1880 | } |
1881 | ||
1882 | wxDateTime& wxDateTime::SetToPrevWeekDay(WeekDay weekday) | |
1883 | { | |
384223b3 | 1884 | wxDATETIME_CHECK( weekday != Inv_WeekDay, _T("invalid weekday") ); |
cd0b1709 VZ |
1885 | |
1886 | int diff; | |
1887 | WeekDay wdayThis = GetWeekDay(); | |
1888 | if ( weekday == wdayThis ) | |
1889 | { | |
1890 | // nothing to do | |
1891 | return *this; | |
1892 | } | |
1893 | else if ( weekday > wdayThis ) | |
1894 | { | |
1895 | // need to go to previous week | |
1896 | diff = 7 - (weekday - wdayThis); | |
1897 | } | |
1898 | else // weekday < wdayThis | |
1899 | { | |
1900 | diff = wdayThis - weekday; | |
1901 | } | |
1902 | ||
f6bcfd97 | 1903 | return Subtract(wxDateSpan::Days(diff)); |
cd0b1709 VZ |
1904 | } |
1905 | ||
2f02cb89 VZ |
1906 | bool wxDateTime::SetToWeekDay(WeekDay weekday, |
1907 | int n, | |
1908 | Month month, | |
1909 | int year) | |
1910 | { | |
68379eaf | 1911 | wxCHECK_MSG( weekday != Inv_WeekDay, false, _T("invalid weekday") ); |
2f02cb89 | 1912 | |
68379eaf | 1913 | // we don't check explicitly that -5 <= n <= 5 because we will return false |
2f02cb89 VZ |
1914 | // anyhow in such case - but may be should still give an assert for it? |
1915 | ||
1916 | // take the current month/year if none specified | |
1917 | ReplaceDefaultYearMonthWithCurrent(&year, &month); | |
1918 | ||
1919 | wxDateTime dt; | |
1920 | ||
1921 | // TODO this probably could be optimised somehow... | |
1922 | ||
1923 | if ( n > 0 ) | |
1924 | { | |
1925 | // get the first day of the month | |
1926 | dt.Set(1, month, year); | |
1927 | ||
1928 | // get its wday | |
1929 | WeekDay wdayFirst = dt.GetWeekDay(); | |
1930 | ||
1931 | // go to the first weekday of the month | |
1932 | int diff = weekday - wdayFirst; | |
1933 | if ( diff < 0 ) | |
1934 | diff += 7; | |
1935 | ||
1936 | // add advance n-1 weeks more | |
1937 | diff += 7*(n - 1); | |
1938 | ||
239446b4 | 1939 | dt += wxDateSpan::Days(diff); |
2f02cb89 | 1940 | } |
239446b4 | 1941 | else // count from the end of the month |
2f02cb89 VZ |
1942 | { |
1943 | // get the last day of the month | |
1944 | dt.SetToLastMonthDay(month, year); | |
1945 | ||
1946 | // get its wday | |
1947 | WeekDay wdayLast = dt.GetWeekDay(); | |
1948 | ||
1949 | // go to the last weekday of the month | |
1950 | int diff = wdayLast - weekday; | |
1951 | if ( diff < 0 ) | |
1952 | diff += 7; | |
1953 | ||
1954 | // and rewind n-1 weeks from there | |
239446b4 | 1955 | diff += 7*(-n - 1); |
2f02cb89 VZ |
1956 | |
1957 | dt -= wxDateSpan::Days(diff); | |
1958 | } | |
1959 | ||
1960 | // check that it is still in the same month | |
1961 | if ( dt.GetMonth() == month ) | |
1962 | { | |
1963 | *this = dt; | |
1964 | ||
68379eaf | 1965 | return true; |
2f02cb89 VZ |
1966 | } |
1967 | else | |
1968 | { | |
1969 | // no such day in this month | |
68379eaf | 1970 | return false; |
2f02cb89 VZ |
1971 | } |
1972 | } | |
1973 | ||
1c5d27e2 VZ |
1974 | static inline |
1975 | wxDateTime::wxDateTime_t GetDayOfYearFromTm(const wxDateTime::Tm& tm) | |
1976 | { | |
907173e5 | 1977 | return (wxDateTime::wxDateTime_t)(gs_cumulatedDays[wxDateTime::IsLeapYear(tm.year)][tm.mon] + tm.mday); |
1c5d27e2 VZ |
1978 | } |
1979 | ||
239446b4 VZ |
1980 | wxDateTime::wxDateTime_t wxDateTime::GetDayOfYear(const TimeZone& tz) const |
1981 | { | |
1c5d27e2 VZ |
1982 | return GetDayOfYearFromTm(GetTm(tz)); |
1983 | } | |
239446b4 | 1984 | |
1c5d27e2 VZ |
1985 | wxDateTime::wxDateTime_t |
1986 | wxDateTime::GetWeekOfYear(wxDateTime::WeekFlags flags, const TimeZone& tz) const | |
239446b4 | 1987 | { |
9d9b7755 VZ |
1988 | if ( flags == Default_First ) |
1989 | { | |
1990 | flags = GetCountry() == USA ? Sunday_First : Monday_First; | |
1991 | } | |
239446b4 | 1992 | |
1c5d27e2 VZ |
1993 | Tm tm(GetTm(tz)); |
1994 | wxDateTime_t nDayInYear = GetDayOfYearFromTm(tm); | |
239446b4 | 1995 | |
1c5d27e2 VZ |
1996 | int wdTarget = GetWeekDay(tz); |
1997 | int wdYearStart = wxDateTime(1, Jan, GetYear()).GetWeekDay(); | |
1998 | int week; | |
9d9b7755 VZ |
1999 | if ( flags == Sunday_First ) |
2000 | { | |
1c5d27e2 VZ |
2001 | // FIXME: First week is not calculated correctly. |
2002 | week = (nDayInYear - wdTarget + 7) / 7; | |
2003 | if ( wdYearStart == Wed || wdYearStart == Thu ) | |
2004 | week++; | |
9d9b7755 | 2005 | } |
1c5d27e2 | 2006 | else // week starts with monday |
9d9b7755 | 2007 | { |
1c5d27e2 VZ |
2008 | // adjust the weekdays to non-US style. |
2009 | wdYearStart = ConvertWeekDayToMondayBase(wdYearStart); | |
2010 | wdTarget = ConvertWeekDayToMondayBase(wdTarget); | |
239446b4 | 2011 | |
1c5d27e2 VZ |
2012 | // quoting from http://www.cl.cam.ac.uk/~mgk25/iso-time.html: |
2013 | // | |
2014 | // Week 01 of a year is per definition the first week that has the | |
2015 | // Thursday in this year, which is equivalent to the week that | |
2016 | // contains the fourth day of January. In other words, the first | |
2017 | // week of a new year is the week that has the majority of its | |
2018 | // days in the new year. Week 01 might also contain days from the | |
2019 | // previous year and the week before week 01 of a year is the last | |
2020 | // week (52 or 53) of the previous year even if it contains days | |
2021 | // from the new year. A week starts with Monday (day 1) and ends | |
2022 | // with Sunday (day 7). | |
2023 | // | |
2024 | ||
2025 | // if Jan 1 is Thursday or less, it is in the first week of this year | |
2026 | if ( wdYearStart < 4 ) | |
2027 | { | |
2028 | // count the number of entire weeks between Jan 1 and this date | |
2029 | week = (nDayInYear + wdYearStart + 6 - wdTarget)/7; | |
2030 | ||
2031 | // be careful to check for overflow in the next year | |
2032 | if ( week == 53 && tm.mday - wdTarget > 28 ) | |
2033 | week = 1; | |
2034 | } | |
2035 | else // Jan 1 is in the last week of the previous year | |
2036 | { | |
2037 | // check if we happen to be at the last week of previous year: | |
2038 | if ( tm.mon == Jan && tm.mday < 8 - wdYearStart ) | |
2039 | week = wxDateTime(31, Dec, GetYear()-1).GetWeekOfYear(); | |
2040 | else | |
2041 | week = (nDayInYear + wdYearStart - 1 - wdTarget)/7; | |
2042 | } | |
239446b4 VZ |
2043 | } |
2044 | ||
907173e5 | 2045 | return (wxDateTime::wxDateTime_t)week; |
68ee7c47 VZ |
2046 | } |
2047 | ||
9d9b7755 VZ |
2048 | wxDateTime::wxDateTime_t wxDateTime::GetWeekOfMonth(wxDateTime::WeekFlags flags, |
2049 | const TimeZone& tz) const | |
68ee7c47 | 2050 | { |
9d9b7755 VZ |
2051 | Tm tm = GetTm(tz); |
2052 | wxDateTime dtMonthStart = wxDateTime(1, tm.mon, tm.year); | |
6dc6fda6 | 2053 | int nWeek = GetWeekOfYear(flags) - dtMonthStart.GetWeekOfYear(flags) + 1; |
9d9b7755 | 2054 | if ( nWeek < 0 ) |
68ee7c47 | 2055 | { |
9d9b7755 VZ |
2056 | // this may happen for January when Jan, 1 is the last week of the |
2057 | // previous year | |
2058 | nWeek += IsLeapYear(tm.year - 1) ? 53 : 52; | |
68ee7c47 | 2059 | } |
68ee7c47 | 2060 | |
6dc6fda6 | 2061 | return (wxDateTime::wxDateTime_t)nWeek; |
239446b4 VZ |
2062 | } |
2063 | ||
f0f951fa VZ |
2064 | wxDateTime& wxDateTime::SetToYearDay(wxDateTime::wxDateTime_t yday) |
2065 | { | |
2066 | int year = GetYear(); | |
384223b3 VZ |
2067 | wxDATETIME_CHECK( (0 < yday) && (yday <= GetNumberOfDays(year)), |
2068 | _T("invalid year day") ); | |
f0f951fa VZ |
2069 | |
2070 | bool isLeap = IsLeapYear(year); | |
2071 | for ( Month mon = Jan; mon < Inv_Month; wxNextMonth(mon) ) | |
2072 | { | |
2073 | // for Dec, we can't compare with gs_cumulatedDays[mon + 1], but we | |
2074 | // don't need it neither - because of the CHECK above we know that | |
2075 | // yday lies in December then | |
f49d731f | 2076 | if ( (mon == Dec) || (yday <= gs_cumulatedDays[isLeap][mon + 1]) ) |
f0f951fa | 2077 | { |
42841dfc | 2078 | Set((wxDateTime::wxDateTime_t)(yday - gs_cumulatedDays[isLeap][mon]), mon, year); |
f0f951fa VZ |
2079 | |
2080 | break; | |
2081 | } | |
2082 | } | |
2083 | ||
2084 | return *this; | |
2085 | } | |
2086 | ||
e6ec579c VZ |
2087 | // ---------------------------------------------------------------------------- |
2088 | // Julian day number conversion and related stuff | |
2089 | // ---------------------------------------------------------------------------- | |
2090 | ||
2091 | double wxDateTime::GetJulianDayNumber() const | |
2092 | { | |
8cc00d5f VZ |
2093 | // JDN are always expressed for the UTC dates |
2094 | Tm tm(ToTimezone(UTC).GetTm(UTC)); | |
e6ec579c VZ |
2095 | |
2096 | double result = GetTruncatedJDN(tm.mday, tm.mon, tm.year); | |
2097 | ||
2098 | // add the part GetTruncatedJDN() neglected | |
2099 | result += 0.5; | |
2100 | ||
2101 | // and now add the time: 86400 sec = 1 JDN | |
2102 | return result + ((double)(60*(60*tm.hour + tm.min) + tm.sec)) / 86400; | |
2103 | } | |
2104 | ||
2105 | double wxDateTime::GetRataDie() const | |
2106 | { | |
2107 | // March 1 of the year 0 is Rata Die day -306 and JDN 1721119.5 | |
2108 | return GetJulianDayNumber() - 1721119.5 - 306; | |
2109 | } | |
2110 | ||
fcc3d7cb | 2111 | // ---------------------------------------------------------------------------- |
299fcbfe | 2112 | // timezone and DST stuff |
fcc3d7cb VZ |
2113 | // ---------------------------------------------------------------------------- |
2114 | ||
299fcbfe | 2115 | int wxDateTime::IsDST(wxDateTime::Country country) const |
fcc3d7cb | 2116 | { |
299fcbfe VZ |
2117 | wxCHECK_MSG( country == Country_Default, -1, |
2118 | _T("country support not implemented") ); | |
2119 | ||
2120 | // use the C RTL for the dates in the standard range | |
2121 | time_t timet = GetTicks(); | |
2122 | if ( timet != (time_t)-1 ) | |
2123 | { | |
2124 | tm *tm = localtime(&timet); | |
2125 | ||
2126 | wxCHECK_MSG( tm, -1, _T("localtime() failed") ); | |
2127 | ||
2128 | return tm->tm_isdst; | |
2129 | } | |
2130 | else | |
2131 | { | |
239446b4 VZ |
2132 | int year = GetYear(); |
2133 | ||
2134 | if ( !IsDSTApplicable(year, country) ) | |
2135 | { | |
2136 | // no DST time in this year in this country | |
2137 | return -1; | |
2138 | } | |
299fcbfe | 2139 | |
239446b4 | 2140 | return IsBetween(GetBeginDST(year, country), GetEndDST(year, country)); |
299fcbfe | 2141 | } |
fcc3d7cb VZ |
2142 | } |
2143 | ||
41acf5c0 | 2144 | wxDateTime& wxDateTime::MakeTimezone(const TimeZone& tz, bool noDST) |
fcc3d7cb | 2145 | { |
479cd5de | 2146 | long secDiff = GetTimeZone() + tz.GetOffset(); |
fcc3d7cb | 2147 | |
41acf5c0 VZ |
2148 | // we need to know whether DST is or not in effect for this date unless |
2149 | // the test disabled by the caller | |
2150 | if ( !noDST && (IsDST() == 1) ) | |
299fcbfe VZ |
2151 | { |
2152 | // FIXME we assume that the DST is always shifted by 1 hour | |
2153 | secDiff -= 3600; | |
2154 | } | |
2155 | ||
f6bcfd97 | 2156 | return Subtract(wxTimeSpan::Seconds(secDiff)); |
fcc3d7cb VZ |
2157 | } |
2158 | ||
b76b015e VZ |
2159 | // ---------------------------------------------------------------------------- |
2160 | // wxDateTime to/from text representations | |
2161 | // ---------------------------------------------------------------------------- | |
2162 | ||
299fcbfe | 2163 | wxString wxDateTime::Format(const wxChar *format, const TimeZone& tz) const |
b76b015e | 2164 | { |
525d8583 | 2165 | wxCHECK_MSG( format, wxEmptyString, _T("NULL format in wxDateTime::Format") ); |
e6ec579c | 2166 | |
f6bcfd97 BP |
2167 | // we have to use our own implementation if the date is out of range of |
2168 | // strftime() or if we use non standard specificators | |
b76b015e | 2169 | time_t time = GetTicks(); |
f6bcfd97 | 2170 | if ( (time != (time_t)-1) && !wxStrstr(format, _T("%l")) ) |
b76b015e VZ |
2171 | { |
2172 | // use strftime() | |
299fcbfe VZ |
2173 | tm *tm; |
2174 | if ( tz.GetOffset() == -GetTimeZone() ) | |
2175 | { | |
2176 | // we are working with local time | |
2177 | tm = localtime(&time); | |
c5a1681b VZ |
2178 | |
2179 | // should never happen | |
2180 | wxCHECK_MSG( tm, wxEmptyString, _T("localtime() failed") ); | |
299fcbfe VZ |
2181 | } |
2182 | else | |
2183 | { | |
479cd5de | 2184 | time += (int)tz.GetOffset(); |
299fcbfe | 2185 | |
33ac7e6f | 2186 | #if defined(__VMS__) || defined(__WATCOMC__) // time is unsigned so avoid warning |
13111b2a | 2187 | int time2 = (int) time; |
9c2882d9 | 2188 | if ( time2 >= 0 ) |
fb10f04c | 2189 | #else |
9c2882d9 | 2190 | if ( time >= 0 ) |
fb10f04c | 2191 | #endif |
c5a1681b VZ |
2192 | { |
2193 | tm = gmtime(&time); | |
b76b015e | 2194 | |
c5a1681b VZ |
2195 | // should never happen |
2196 | wxCHECK_MSG( tm, wxEmptyString, _T("gmtime() failed") ); | |
2197 | } | |
2198 | else | |
2199 | { | |
2200 | tm = (struct tm *)NULL; | |
2201 | } | |
2202 | } | |
77b88df2 | 2203 | #ifndef __WXWINCE__ |
ba9bbf13 | 2204 | //Windows CE doesn't support strftime or wcsftime, so we use the generic implementation |
c5a1681b | 2205 | if ( tm ) |
e6ec579c | 2206 | { |
c5a1681b | 2207 | return CallStrftime(format, tm); |
e6ec579c | 2208 | } |
77b88df2 | 2209 | #endif |
c5a1681b VZ |
2210 | //else: use generic code below |
2211 | } | |
2212 | ||
68ee7c47 | 2213 | // we only parse ANSI C format specifications here, no POSIX 2 |
f6bcfd97 BP |
2214 | // complications, no GNU extensions but we do add support for a "%l" format |
2215 | // specifier allowing to get the number of milliseconds | |
68ee7c47 | 2216 | Tm tm = GetTm(tz); |
e6ec579c | 2217 | |
68ee7c47 VZ |
2218 | // used for calls to strftime() when we only deal with time |
2219 | struct tm tmTimeOnly; | |
2220 | tmTimeOnly.tm_hour = tm.hour; | |
2221 | tmTimeOnly.tm_min = tm.min; | |
2222 | tmTimeOnly.tm_sec = tm.sec; | |
2223 | tmTimeOnly.tm_wday = 0; | |
2224 | tmTimeOnly.tm_yday = 0; | |
2225 | tmTimeOnly.tm_mday = 1; // any date will do | |
2226 | tmTimeOnly.tm_mon = 0; | |
2227 | tmTimeOnly.tm_year = 76; | |
2228 | tmTimeOnly.tm_isdst = 0; // no DST, we adjust for tz ourselves | |
e6ec579c | 2229 | |
77c3e48a | 2230 | wxString tmp, res, fmt; |
68ee7c47 VZ |
2231 | for ( const wxChar *p = format; *p; p++ ) |
2232 | { | |
2233 | if ( *p != _T('%') ) | |
2234 | { | |
2235 | // copy as is | |
2236 | res += *p; | |
b76b015e | 2237 | |
68ee7c47 VZ |
2238 | continue; |
2239 | } | |
e6ec579c | 2240 | |
77c3e48a | 2241 | // set the default format |
68ee7c47 | 2242 | switch ( *++p ) |
77c3e48a VZ |
2243 | { |
2244 | case _T('Y'): // year has 4 digits | |
2245 | fmt = _T("%04d"); | |
2246 | break; | |
2247 | ||
2248 | case _T('j'): // day of year has 3 digits | |
f6bcfd97 | 2249 | case _T('l'): // milliseconds have 3 digits |
77c3e48a VZ |
2250 | fmt = _T("%03d"); |
2251 | break; | |
2252 | ||
59d04dff VZ |
2253 | case _T('w'): // week day as number has only one |
2254 | fmt = _T("%d"); | |
2255 | break; | |
2256 | ||
77c3e48a VZ |
2257 | default: |
2258 | // it's either another valid format specifier in which case | |
2259 | // the format is "%02d" (for all the rest) or we have the | |
2260 | // field width preceding the format in which case it will | |
2261 | // override the default format anyhow | |
2262 | fmt = _T("%02d"); | |
2263 | } | |
2264 | ||
68379eaf | 2265 | bool restart = true; |
fc3398f8 | 2266 | while ( restart ) |
68ee7c47 | 2267 | { |
68379eaf | 2268 | restart = false; |
68ee7c47 | 2269 | |
fc3398f8 VZ |
2270 | // start of the format specification |
2271 | switch ( *p ) | |
2272 | { | |
2273 | case _T('a'): // a weekday name | |
2274 | case _T('A'): | |
68379eaf | 2275 | // second parameter should be true for abbreviated names |
fc3398f8 VZ |
2276 | res += GetWeekDayName(tm.GetWeekDay(), |
2277 | *p == _T('a') ? Name_Abbr : Name_Full); | |
2278 | break; | |
68ee7c47 | 2279 | |
fc3398f8 VZ |
2280 | case _T('b'): // a month name |
2281 | case _T('B'): | |
2282 | res += GetMonthName(tm.mon, | |
2283 | *p == _T('b') ? Name_Abbr : Name_Full); | |
2284 | break; | |
2285 | ||
2286 | case _T('c'): // locale default date and time representation | |
2287 | case _T('x'): // locale default date representation | |
77b88df2 | 2288 | #ifndef __WXWINCE__ |
fc3398f8 VZ |
2289 | // |
2290 | // the problem: there is no way to know what do these format | |
2291 | // specifications correspond to for the current locale. | |
2292 | // | |
2293 | // the solution: use a hack and still use strftime(): first | |
2294 | // find the YEAR which is a year in the strftime() range (1970 | |
2295 | // - 2038) whose Jan 1 falls on the same week day as the Jan 1 | |
2296 | // of the real year. Then make a copy of the format and | |
2297 | // replace all occurences of YEAR in it with some unique | |
2298 | // string not appearing anywhere else in it, then use | |
2299 | // strftime() to format the date in year YEAR and then replace | |
2300 | // YEAR back by the real year and the unique replacement | |
2301 | // string back with YEAR. Notice that "all occurences of YEAR" | |
2302 | // means all occurences of 4 digit as well as 2 digit form! | |
2303 | // | |
2304 | // the bugs: we assume that neither of %c nor %x contains any | |
2305 | // fields which may change between the YEAR and real year. For | |
2306 | // example, the week number (%U, %W) and the day number (%j) | |
2307 | // will change if one of these years is leap and the other one | |
2308 | // is not! | |
68ee7c47 | 2309 | { |
fc3398f8 VZ |
2310 | // find the YEAR: normally, for any year X, Jan 1 or the |
2311 | // year X + 28 is the same weekday as Jan 1 of X (because | |
2312 | // the weekday advances by 1 for each normal X and by 2 | |
2313 | // for each leap X, hence by 5 every 4 years or by 35 | |
2314 | // which is 0 mod 7 every 28 years) but this rule breaks | |
2315 | // down if there are years between X and Y which are | |
2316 | // divisible by 4 but not leap (i.e. divisible by 100 but | |
2317 | // not 400), hence the correction. | |
2318 | ||
2319 | int yearReal = GetYear(tz); | |
2320 | int mod28 = yearReal % 28; | |
2321 | ||
2322 | // be careful to not go too far - we risk to leave the | |
2323 | // supported range | |
2324 | int year; | |
2325 | if ( mod28 < 10 ) | |
2326 | { | |
2327 | year = 1988 + mod28; // 1988 == 0 (mod 28) | |
2328 | } | |
2329 | else | |
2330 | { | |
2331 | year = 1970 + mod28 - 10; // 1970 == 10 (mod 28) | |
2332 | } | |
e6ec579c | 2333 | |
fc3398f8 VZ |
2334 | int nCentury = year / 100, |
2335 | nCenturyReal = yearReal / 100; | |
c5a1681b | 2336 | |
fc3398f8 VZ |
2337 | // need to adjust for the years divisble by 400 which are |
2338 | // not leap but are counted like leap ones if we just take | |
2339 | // the number of centuries in between for nLostWeekDays | |
2340 | int nLostWeekDays = (nCentury - nCenturyReal) - | |
2341 | (nCentury / 4 - nCenturyReal / 4); | |
c5a1681b | 2342 | |
fc3398f8 VZ |
2343 | // we have to gain back the "lost" weekdays: note that the |
2344 | // effect of this loop is to not do anything to | |
2345 | // nLostWeekDays (which we won't use any more), but to | |
2346 | // (indirectly) set the year correctly | |
2347 | while ( (nLostWeekDays % 7) != 0 ) | |
2348 | { | |
2349 | nLostWeekDays += year++ % 4 ? 1 : 2; | |
2350 | } | |
c5a1681b | 2351 | |
fc3398f8 VZ |
2352 | // at any rate, we couldn't go further than 1988 + 9 + 28! |
2353 | wxASSERT_MSG( year < 2030, | |
2354 | _T("logic error in wxDateTime::Format") ); | |
c5a1681b | 2355 | |
fc3398f8 VZ |
2356 | wxString strYear, strYear2; |
2357 | strYear.Printf(_T("%d"), year); | |
2358 | strYear2.Printf(_T("%d"), year % 100); | |
c5a1681b | 2359 | |
fc3398f8 VZ |
2360 | // find two strings not occuring in format (this is surely |
2361 | // not optimal way of doing it... improvements welcome!) | |
2362 | wxString fmt = format; | |
2363 | wxString replacement = (wxChar)-1; | |
2364 | while ( fmt.Find(replacement) != wxNOT_FOUND ) | |
2365 | { | |
2366 | replacement << (wxChar)-1; | |
2367 | } | |
c5a1681b | 2368 | |
fc3398f8 VZ |
2369 | wxString replacement2 = (wxChar)-2; |
2370 | while ( fmt.Find(replacement) != wxNOT_FOUND ) | |
2371 | { | |
2372 | replacement << (wxChar)-2; | |
2373 | } | |
2374 | ||
2375 | // replace all occurences of year with it | |
2376 | bool wasReplaced = fmt.Replace(strYear, replacement) > 0; | |
2377 | if ( !wasReplaced ) | |
2378 | wasReplaced = fmt.Replace(strYear2, replacement2) > 0; | |
2379 | ||
2380 | // use strftime() to format the same date but in supported | |
2381 | // year | |
2382 | // | |
2383 | // NB: we assume that strftime() doesn't check for the | |
2384 | // date validity and will happily format the date | |
2385 | // corresponding to Feb 29 of a non leap year (which | |
2386 | // may happen if yearReal was leap and year is not) | |
2387 | struct tm tmAdjusted; | |
2388 | InitTm(tmAdjusted); | |
2389 | tmAdjusted.tm_hour = tm.hour; | |
2390 | tmAdjusted.tm_min = tm.min; | |
2391 | tmAdjusted.tm_sec = tm.sec; | |
2392 | tmAdjusted.tm_wday = tm.GetWeekDay(); | |
2393 | tmAdjusted.tm_yday = GetDayOfYear(); | |
2394 | tmAdjusted.tm_mday = tm.mday; | |
2395 | tmAdjusted.tm_mon = tm.mon; | |
2396 | tmAdjusted.tm_year = year - 1900; | |
2397 | tmAdjusted.tm_isdst = 0; // no DST, already adjusted | |
2398 | wxString str = CallStrftime(*p == _T('c') ? _T("%c") | |
2399 | : _T("%x"), | |
2400 | &tmAdjusted); | |
2401 | ||
2402 | // now replace the occurence of 1999 with the real year | |
2403 | wxString strYearReal, strYearReal2; | |
2404 | strYearReal.Printf(_T("%04d"), yearReal); | |
2405 | strYearReal2.Printf(_T("%02d"), yearReal % 100); | |
2406 | str.Replace(strYear, strYearReal); | |
2407 | str.Replace(strYear2, strYearReal2); | |
2408 | ||
2409 | // and replace back all occurences of replacement string | |
2410 | if ( wasReplaced ) | |
2411 | { | |
2412 | str.Replace(replacement2, strYear2); | |
2413 | str.Replace(replacement, strYear); | |
2414 | } | |
2415 | ||
2416 | res += str; | |
68ee7c47 | 2417 | } |
77b88df2 | 2418 | #else |
ba9bbf13 WS |
2419 | //Use "%m/%d/%y %H:%M:%S" format instead |
2420 | res += wxString::Format(wxT("%02d/%02d/%04d %02d:%02d:%02d"), | |
2421 | tm.mon+1,tm.mday, tm.year, tm.hour, tm.min, tm.sec); | |
77b88df2 | 2422 | #endif |
fc3398f8 | 2423 | break; |
c5a1681b | 2424 | |
fc3398f8 VZ |
2425 | case _T('d'): // day of a month (01-31) |
2426 | res += wxString::Format(fmt, tm.mday); | |
2427 | break; | |
68ee7c47 | 2428 | |
fc3398f8 VZ |
2429 | case _T('H'): // hour in 24h format (00-23) |
2430 | res += wxString::Format(fmt, tm.hour); | |
2431 | break; | |
2432 | ||
2433 | case _T('I'): // hour in 12h format (01-12) | |
68ee7c47 | 2434 | { |
fc3398f8 VZ |
2435 | // 24h -> 12h, 0h -> 12h too |
2436 | int hour12 = tm.hour > 12 ? tm.hour - 12 | |
2437 | : tm.hour ? tm.hour : 12; | |
2438 | res += wxString::Format(fmt, hour12); | |
68ee7c47 | 2439 | } |
fc3398f8 | 2440 | break; |
c5a1681b | 2441 | |
fc3398f8 VZ |
2442 | case _T('j'): // day of the year |
2443 | res += wxString::Format(fmt, GetDayOfYear(tz)); | |
2444 | break; | |
68ee7c47 | 2445 | |
f6bcfd97 BP |
2446 | case _T('l'): // milliseconds (NOT STANDARD) |
2447 | res += wxString::Format(fmt, GetMillisecond(tz)); | |
2448 | break; | |
2449 | ||
fc3398f8 VZ |
2450 | case _T('m'): // month as a number (01-12) |
2451 | res += wxString::Format(fmt, tm.mon + 1); | |
2452 | break; | |
68ee7c47 | 2453 | |
fc3398f8 VZ |
2454 | case _T('M'): // minute as a decimal number (00-59) |
2455 | res += wxString::Format(fmt, tm.min); | |
2456 | break; | |
68ee7c47 | 2457 | |
fc3398f8 | 2458 | case _T('p'): // AM or PM string |
77b88df2 | 2459 | #ifndef __WXWINCE__ |
fc3398f8 | 2460 | res += CallStrftime(_T("%p"), &tmTimeOnly); |
77b88df2 | 2461 | #else |
ba9bbf13 | 2462 | res += (tmTimeOnly.tm_hour > 12) ? wxT("pm") : wxT("am"); |
77b88df2 | 2463 | #endif |
fc3398f8 | 2464 | break; |
68ee7c47 | 2465 | |
fc3398f8 VZ |
2466 | case _T('S'): // second as a decimal number (00-61) |
2467 | res += wxString::Format(fmt, tm.sec); | |
2468 | break; | |
68ee7c47 | 2469 | |
fc3398f8 VZ |
2470 | case _T('U'): // week number in the year (Sunday 1st week day) |
2471 | res += wxString::Format(fmt, GetWeekOfYear(Sunday_First, tz)); | |
2472 | break; | |
68ee7c47 | 2473 | |
fc3398f8 VZ |
2474 | case _T('W'): // week number in the year (Monday 1st week day) |
2475 | res += wxString::Format(fmt, GetWeekOfYear(Monday_First, tz)); | |
2476 | break; | |
68ee7c47 | 2477 | |
fc3398f8 VZ |
2478 | case _T('w'): // weekday as a number (0-6), Sunday = 0 |
2479 | res += wxString::Format(fmt, tm.GetWeekDay()); | |
2480 | break; | |
68ee7c47 | 2481 | |
fc3398f8 | 2482 | // case _T('x'): -- handled with "%c" |
68ee7c47 | 2483 | |
fc3398f8 VZ |
2484 | case _T('X'): // locale default time representation |
2485 | // just use strftime() to format the time for us | |
77b88df2 | 2486 | #ifndef __WXWINCE__ |
fc3398f8 | 2487 | res += CallStrftime(_T("%X"), &tmTimeOnly); |
77b88df2 | 2488 | #else |
ba9bbf13 | 2489 | res += wxString::Format(wxT("%02d:%02d:%02d"),tm.hour, tm.min, tm.sec); |
77b88df2 | 2490 | #endif |
fc3398f8 | 2491 | break; |
68ee7c47 | 2492 | |
fc3398f8 VZ |
2493 | case _T('y'): // year without century (00-99) |
2494 | res += wxString::Format(fmt, tm.year % 100); | |
2495 | break; | |
68ee7c47 | 2496 | |
fc3398f8 VZ |
2497 | case _T('Y'): // year with century |
2498 | res += wxString::Format(fmt, tm.year); | |
2499 | break; | |
68ee7c47 | 2500 | |
fc3398f8 | 2501 | case _T('Z'): // timezone name |
77b88df2 | 2502 | #ifndef __WXWINCE__ |
fc3398f8 | 2503 | res += CallStrftime(_T("%Z"), &tmTimeOnly); |
77b88df2 | 2504 | #endif |
fc3398f8 | 2505 | break; |
68ee7c47 | 2506 | |
fc3398f8 VZ |
2507 | default: |
2508 | // is it the format width? | |
2509 | fmt.Empty(); | |
2510 | while ( *p == _T('-') || *p == _T('+') || | |
2511 | *p == _T(' ') || wxIsdigit(*p) ) | |
2512 | { | |
2513 | fmt += *p; | |
2514 | } | |
68ee7c47 | 2515 | |
bb8a8478 | 2516 | if ( !fmt.empty() ) |
fc3398f8 VZ |
2517 | { |
2518 | // we've only got the flags and width so far in fmt | |
2519 | fmt.Prepend(_T('%')); | |
2520 | fmt.Append(_T('d')); | |
68ee7c47 | 2521 | |
68379eaf | 2522 | restart = true; |
68ee7c47 | 2523 | |
fc3398f8 VZ |
2524 | break; |
2525 | } | |
68ee7c47 | 2526 | |
fc3398f8 VZ |
2527 | // no, it wasn't the width |
2528 | wxFAIL_MSG(_T("unknown format specificator")); | |
68ee7c47 | 2529 | |
fc3398f8 | 2530 | // fall through and just copy it nevertheless |
68ee7c47 | 2531 | |
fc3398f8 VZ |
2532 | case _T('%'): // a percent sign |
2533 | res += *p; | |
2534 | break; | |
2535 | ||
2536 | case 0: // the end of string | |
2537 | wxFAIL_MSG(_T("missing format at the end of string")); | |
2538 | ||
2539 | // just put the '%' which was the last char in format | |
2540 | res += _T('%'); | |
2541 | break; | |
2542 | } | |
68ee7c47 | 2543 | } |
c5a1681b VZ |
2544 | } |
2545 | ||
68ee7c47 | 2546 | return res; |
b76b015e | 2547 | } |
fcc3d7cb | 2548 | |
cd0b1709 VZ |
2549 | // this function parses a string in (strict) RFC 822 format: see the section 5 |
2550 | // of the RFC for the detailed description, but briefly it's something of the | |
2551 | // form "Sat, 18 Dec 1999 00:48:30 +0100" | |
2552 | // | |
2553 | // this function is "strict" by design - it must reject anything except true | |
2554 | // RFC822 time specs. | |
2555 | // | |
2556 | // TODO a great candidate for using reg exps | |
2557 | const wxChar *wxDateTime::ParseRfc822Date(const wxChar* date) | |
2558 | { | |
2559 | wxCHECK_MSG( date, (wxChar *)NULL, _T("NULL pointer in wxDateTime::Parse") ); | |
2560 | ||
2561 | const wxChar *p = date; | |
2562 | const wxChar *comma = wxStrchr(p, _T(',')); | |
2563 | if ( comma ) | |
2564 | { | |
2565 | // the part before comma is the weekday | |
2566 | ||
2567 | // skip it for now - we don't use but might check that it really | |
2568 | // corresponds to the specfied date | |
2569 | p = comma + 1; | |
2570 | ||
2571 | if ( *p != _T(' ') ) | |
2572 | { | |
2573 | wxLogDebug(_T("no space after weekday in RFC822 time spec")); | |
2574 | ||
2575 | return (wxChar *)NULL; | |
2576 | } | |
2577 | ||
2578 | p++; // skip space | |
2579 | } | |
2580 | ||
2581 | // the following 1 or 2 digits are the day number | |
2582 | if ( !wxIsdigit(*p) ) | |
2583 | { | |
2584 | wxLogDebug(_T("day number expected in RFC822 time spec, none found")); | |
2585 | ||
2586 | return (wxChar *)NULL; | |
2587 | } | |
2588 | ||
907173e5 | 2589 | wxDateTime_t day = (wxDateTime_t)(*p++ - _T('0')); |
cd0b1709 VZ |
2590 | if ( wxIsdigit(*p) ) |
2591 | { | |
2592 | day *= 10; | |
907173e5 | 2593 | day = (wxDateTime_t)(day + (*p++ - _T('0'))); |
cd0b1709 VZ |
2594 | } |
2595 | ||
2596 | if ( *p++ != _T(' ') ) | |
2597 | { | |
2598 | return (wxChar *)NULL; | |
2599 | } | |
2600 | ||
2601 | // the following 3 letters specify the month | |
2602 | wxString monName(p, 3); | |
2603 | Month mon; | |
2604 | if ( monName == _T("Jan") ) | |
2605 | mon = Jan; | |
2606 | else if ( monName == _T("Feb") ) | |
2607 | mon = Feb; | |
2608 | else if ( monName == _T("Mar") ) | |
2609 | mon = Mar; | |
2610 | else if ( monName == _T("Apr") ) | |
2611 | mon = Apr; | |
2612 | else if ( monName == _T("May") ) | |
2613 | mon = May; | |
2614 | else if ( monName == _T("Jun") ) | |
2615 | mon = Jun; | |
2616 | else if ( monName == _T("Jul") ) | |
2617 | mon = Jul; | |
2618 | else if ( monName == _T("Aug") ) | |
2619 | mon = Aug; | |
2620 | else if ( monName == _T("Sep") ) | |
2621 | mon = Sep; | |
2622 | else if ( monName == _T("Oct") ) | |
2623 | mon = Oct; | |
2624 | else if ( monName == _T("Nov") ) | |
2625 | mon = Nov; | |
2626 | else if ( monName == _T("Dec") ) | |
2627 | mon = Dec; | |
2628 | else | |
2629 | { | |
2630 | wxLogDebug(_T("Invalid RFC 822 month name '%s'"), monName.c_str()); | |
2631 | ||
2632 | return (wxChar *)NULL; | |
2633 | } | |
2634 | ||
2635 | p += 3; | |
2636 | ||
2637 | if ( *p++ != _T(' ') ) | |
2638 | { | |
2639 | return (wxChar *)NULL; | |
2640 | } | |
2641 | ||
2642 | // next is the year | |
2643 | if ( !wxIsdigit(*p) ) | |
2644 | { | |
2645 | // no year? | |
2646 | return (wxChar *)NULL; | |
2647 | } | |
2648 | ||
2649 | int year = *p++ - _T('0'); | |
2650 | ||
2651 | if ( !wxIsdigit(*p) ) | |
2652 | { | |
2653 | // should have at least 2 digits in the year | |
2654 | return (wxChar *)NULL; | |
2655 | } | |
2656 | ||
2657 | year *= 10; | |
2658 | year += *p++ - _T('0'); | |
2659 | ||
2660 | // is it a 2 digit year (as per original RFC 822) or a 4 digit one? | |
2661 | if ( wxIsdigit(*p) ) | |
2662 | { | |
2663 | year *= 10; | |
2664 | year += *p++ - _T('0'); | |
2665 | ||
2666 | if ( !wxIsdigit(*p) ) | |
2667 | { | |
2668 | // no 3 digit years please | |
2669 | return (wxChar *)NULL; | |
2670 | } | |
2671 | ||
2672 | year *= 10; | |
2673 | year += *p++ - _T('0'); | |
2674 | } | |
2675 | ||
2676 | if ( *p++ != _T(' ') ) | |
2677 | { | |
2678 | return (wxChar *)NULL; | |
2679 | } | |
2680 | ||
2681 | // time is in the format hh:mm:ss and seconds are optional | |
2682 | if ( !wxIsdigit(*p) ) | |
2683 | { | |
2684 | return (wxChar *)NULL; | |
2685 | } | |
2686 | ||
42841dfc | 2687 | wxDateTime_t hour = (wxDateTime_t)(*p++ - _T('0')); |
cd0b1709 VZ |
2688 | |
2689 | if ( !wxIsdigit(*p) ) | |
2690 | { | |
2691 | return (wxChar *)NULL; | |
2692 | } | |
2693 | ||
2694 | hour *= 10; | |
42841dfc | 2695 | hour = (wxDateTime_t)(hour + (*p++ - _T('0'))); |
cd0b1709 VZ |
2696 | |
2697 | if ( *p++ != _T(':') ) | |
2698 | { | |
2699 | return (wxChar *)NULL; | |
2700 | } | |
2701 | ||
2702 | if ( !wxIsdigit(*p) ) | |
2703 | { | |
2704 | return (wxChar *)NULL; | |
2705 | } | |
2706 | ||
42841dfc | 2707 | wxDateTime_t min = (wxDateTime_t)(*p++ - _T('0')); |
cd0b1709 VZ |
2708 | |
2709 | if ( !wxIsdigit(*p) ) | |
2710 | { | |
2711 | return (wxChar *)NULL; | |
2712 | } | |
2713 | ||
2714 | min *= 10; | |
42841dfc | 2715 | min = (wxDateTime_t)(min + *p++ - _T('0')); |
cd0b1709 VZ |
2716 | |
2717 | wxDateTime_t sec = 0; | |
2718 | if ( *p++ == _T(':') ) | |
2719 | { | |
2720 | if ( !wxIsdigit(*p) ) | |
2721 | { | |
2722 | return (wxChar *)NULL; | |
2723 | } | |
2724 | ||
42841dfc | 2725 | sec = (wxDateTime_t)(*p++ - _T('0')); |
cd0b1709 VZ |
2726 | |
2727 | if ( !wxIsdigit(*p) ) | |
2728 | { | |
2729 | return (wxChar *)NULL; | |
2730 | } | |
2731 | ||
2732 | sec *= 10; | |
42841dfc | 2733 | sec = (wxDateTime_t)(sec + *p++ - _T('0')); |
cd0b1709 VZ |
2734 | } |
2735 | ||
2736 | if ( *p++ != _T(' ') ) | |
2737 | { | |
2738 | return (wxChar *)NULL; | |
2739 | } | |
2740 | ||
2741 | // and now the interesting part: the timezone | |
ba9bbf13 | 2742 | int offset wxDUMMY_INITIALIZE(0); |
cd0b1709 VZ |
2743 | if ( *p == _T('-') || *p == _T('+') ) |
2744 | { | |
2745 | // the explicit offset given: it has the form of hhmm | |
2746 | bool plus = *p++ == _T('+'); | |
2747 | ||
2748 | if ( !wxIsdigit(*p) || !wxIsdigit(*(p + 1)) ) | |
2749 | { | |
2750 | return (wxChar *)NULL; | |
2751 | } | |
2752 | ||
2753 | // hours | |
2754 | offset = 60*(10*(*p - _T('0')) + (*(p + 1) - _T('0'))); | |
2755 | ||
2756 | p += 2; | |
2757 | ||
2758 | if ( !wxIsdigit(*p) || !wxIsdigit(*(p + 1)) ) | |
2759 | { | |
2760 | return (wxChar *)NULL; | |
2761 | } | |
2762 | ||
2763 | // minutes | |
2764 | offset += 10*(*p - _T('0')) + (*(p + 1) - _T('0')); | |
2765 | ||
2766 | if ( !plus ) | |
2767 | { | |
2768 | offset = -offset; | |
2769 | } | |
2770 | ||
2771 | p += 2; | |
2772 | } | |
2773 | else | |
2774 | { | |
2775 | // the symbolic timezone given: may be either military timezone or one | |
2776 | // of standard abbreviations | |
2777 | if ( !*(p + 1) ) | |
2778 | { | |
2779 | // military: Z = UTC, J unused, A = -1, ..., Y = +12 | |
2780 | static const int offsets[26] = | |
2781 | { | |
2782 | //A B C D E F G H I J K L M | |
2783 | -1, -2, -3, -4, -5, -6, -7, -8, -9, 0, -10, -11, -12, | |
2784 | //N O P R Q S T U V W Z Y Z | |
2785 | +1, +2, +3, +4, +5, +6, +7, +8, +9, +10, +11, +12, 0 | |
2786 | }; | |
2787 | ||
2788 | if ( *p < _T('A') || *p > _T('Z') || *p == _T('J') ) | |
2789 | { | |
2790 | wxLogDebug(_T("Invalid militaty timezone '%c'"), *p); | |
2791 | ||
2792 | return (wxChar *)NULL; | |
2793 | } | |
2794 | ||
2795 | offset = offsets[*p++ - _T('A')]; | |
2796 | } | |
2797 | else | |
2798 | { | |
2799 | // abbreviation | |
2800 | wxString tz = p; | |
2801 | if ( tz == _T("UT") || tz == _T("UTC") || tz == _T("GMT") ) | |
2802 | offset = 0; | |
2803 | else if ( tz == _T("AST") ) | |
2804 | offset = AST - GMT0; | |
2805 | else if ( tz == _T("ADT") ) | |
2806 | offset = ADT - GMT0; | |
2807 | else if ( tz == _T("EST") ) | |
2808 | offset = EST - GMT0; | |
2809 | else if ( tz == _T("EDT") ) | |
2810 | offset = EDT - GMT0; | |
2811 | else if ( tz == _T("CST") ) | |
2812 | offset = CST - GMT0; | |
2813 | else if ( tz == _T("CDT") ) | |
2814 | offset = CDT - GMT0; | |
2815 | else if ( tz == _T("MST") ) | |
2816 | offset = MST - GMT0; | |
2817 | else if ( tz == _T("MDT") ) | |
2818 | offset = MDT - GMT0; | |
2819 | else if ( tz == _T("PST") ) | |
2820 | offset = PST - GMT0; | |
2821 | else if ( tz == _T("PDT") ) | |
2822 | offset = PDT - GMT0; | |
2823 | else | |
2824 | { | |
2825 | wxLogDebug(_T("Unknown RFC 822 timezone '%s'"), p); | |
2826 | ||
2827 | return (wxChar *)NULL; | |
2828 | } | |
2829 | ||
2830 | p += tz.length(); | |
2831 | } | |
2832 | ||
2833 | // make it minutes | |
2834 | offset *= 60; | |
2835 | } | |
2836 | ||
2837 | // the spec was correct | |
2838 | Set(day, mon, year, hour, min, sec); | |
487c1f7e | 2839 | MakeTimezone((wxDateTime_t)(60*offset)); |
cd0b1709 VZ |
2840 | |
2841 | return p; | |
2842 | } | |
2843 | ||
64f9c100 | 2844 | #ifdef __WINDOWS__ |
e44501a0 | 2845 | |
64f9c100 VS |
2846 | // Get's current locale's date formatting string and stores it in fmt if |
2847 | // the locale is set; otherwise or in case of failure, leaves fmt unchanged | |
e44501a0 | 2848 | static void GetLocaleDateFormat(wxString *fmt) |
64f9c100 | 2849 | { |
05eb2d40 VZ |
2850 | // there is no setlocale() under Windows CE, so just always query the |
2851 | // system there | |
2852 | #ifndef __WXWINCE__ | |
64f9c100 | 2853 | if ( strcmp(setlocale(LC_ALL, NULL), "C") != 0 ) |
e44501a0 | 2854 | #endif |
64f9c100 VS |
2855 | { |
2856 | // The locale was programatically set to non-C. We assume that this was | |
2857 | // done using wxLocale, in which case thread's current locale is also | |
2858 | // set to correct LCID value and we can use GetLocaleInfo to determine | |
2859 | // the correct formatting string: | |
c5fe22f8 JS |
2860 | #ifdef __WXWINCE__ |
2861 | LCID lcid = LOCALE_USER_DEFAULT; | |
2862 | #else | |
64f9c100 | 2863 | LCID lcid = GetThreadLocale(); |
c5fe22f8 | 2864 | #endif |
64f9c100 VS |
2865 | wxChar delim[5]; // fields deliminer, 4 chars max |
2866 | if ( GetLocaleInfo(lcid, LOCALE_SDATE, delim, 5) ) | |
2867 | { | |
2868 | wxChar centurybuf[2]; // use %y or %Y, 1 char max | |
2869 | wxChar century = 'y'; | |
2870 | if ( GetLocaleInfo(lcid, LOCALE_ICENTURY, centurybuf, 2) ) | |
2871 | { | |
2872 | if ( centurybuf[0] == _T('1') ) | |
2873 | century = 'Y'; | |
2874 | // else 'y' as above | |
2875 | } | |
2876 | ||
2877 | wxChar order[2]; // order code, 1 char max | |
2878 | if ( GetLocaleInfo(lcid, LOCALE_IDATE, order, 2) ) | |
2879 | { | |
2880 | if ( order[0] == _T('0') ) // M-D-Y | |
2881 | { | |
2882 | *fmt = wxString::Format(_T("%%m%s%%d%s%%%c"), | |
2883 | delim, delim, century); | |
2884 | } | |
2885 | else if ( order[0] == _T('1') ) // D-M-Y | |
2886 | { | |
2887 | *fmt = wxString::Format(_T("%%d%s%%m%s%%%c"), | |
2888 | delim, delim, century); | |
2889 | } | |
2890 | else if ( order[0] == _T('2') ) // Y-M-D | |
2891 | { | |
2892 | *fmt = wxString::Format(_T("%%%c%s%%m%s%%d"), | |
2893 | century, delim, delim); | |
2894 | } | |
2895 | else | |
2896 | { | |
2897 | wxFAIL_MSG(_T("unexpected GetLocaleInfo return value")); | |
2898 | } | |
2899 | } | |
2900 | } | |
2901 | // if we failed, leave fmtDate value unchanged and | |
2902 | // try our luck with the default set above | |
2903 | } | |
2904 | } | |
e44501a0 | 2905 | |
64f9c100 VS |
2906 | #endif // __WINDOWS__ |
2907 | ||
f0f951fa VZ |
2908 | const wxChar *wxDateTime::ParseFormat(const wxChar *date, |
2909 | const wxChar *format, | |
2910 | const wxDateTime& dateDef) | |
cd0b1709 VZ |
2911 | { |
2912 | wxCHECK_MSG( date && format, (wxChar *)NULL, | |
f0f951fa | 2913 | _T("NULL pointer in wxDateTime::ParseFormat()") ); |
cd0b1709 | 2914 | |
f0f951fa VZ |
2915 | wxString str; |
2916 | unsigned long num; | |
cd0b1709 | 2917 | |
f0f951fa | 2918 | // what fields have we found? |
68379eaf WS |
2919 | bool haveWDay = false, |
2920 | haveYDay = false, | |
2921 | haveDay = false, | |
2922 | haveMon = false, | |
2923 | haveYear = false, | |
2924 | haveHour = false, | |
2925 | haveMin = false, | |
2926 | haveSec = false; | |
2927 | ||
2928 | bool hourIsIn12hFormat = false, // or in 24h one? | |
2929 | isPM = false; // AM by default | |
f0f951fa VZ |
2930 | |
2931 | // and the value of the items we have (init them to get rid of warnings) | |
2932 | wxDateTime_t sec = 0, | |
2933 | min = 0, | |
2934 | hour = 0; | |
2935 | WeekDay wday = Inv_WeekDay; | |
2936 | wxDateTime_t yday = 0, | |
2937 | mday = 0; | |
2938 | wxDateTime::Month mon = Inv_Month; | |
2939 | int year = 0; | |
2940 | ||
2941 | const wxChar *input = date; | |
2942 | for ( const wxChar *fmt = format; *fmt; fmt++ ) | |
2943 | { | |
2944 | if ( *fmt != _T('%') ) | |
2945 | { | |
2946 | if ( wxIsspace(*fmt) ) | |
2947 | { | |
2948 | // a white space in the format string matches 0 or more white | |
2949 | // spaces in the input | |
2950 | while ( wxIsspace(*input) ) | |
2951 | { | |
2952 | input++; | |
2953 | } | |
2954 | } | |
2955 | else // !space | |
2956 | { | |
2957 | // any other character (not whitespace, not '%') must be | |
2958 | // matched by itself in the input | |
2959 | if ( *input++ != *fmt ) | |
2960 | { | |
2961 | // no match | |
2962 | return (wxChar *)NULL; | |
2963 | } | |
2964 | } | |
2965 | ||
2966 | // done with this format char | |
2967 | continue; | |
2968 | } | |
2969 | ||
2970 | // start of a format specification | |
f6bcfd97 BP |
2971 | |
2972 | // parse the optional width | |
2973 | size_t width = 0; | |
1c193821 | 2974 | while ( wxIsdigit(*++fmt) ) |
f6bcfd97 BP |
2975 | { |
2976 | width *= 10; | |
2977 | width += *fmt - _T('0'); | |
2978 | } | |
2979 | ||
4147efe9 VZ |
2980 | // the default widths for the various fields |
2981 | if ( !width ) | |
2982 | { | |
2983 | switch ( *fmt ) | |
2984 | { | |
2985 | case _T('Y'): // year has 4 digits | |
2986 | width = 4; | |
2987 | break; | |
2988 | ||
2989 | case _T('j'): // day of year has 3 digits | |
2990 | case _T('l'): // milliseconds have 3 digits | |
2991 | width = 3; | |
2992 | break; | |
2993 | ||
2994 | case _T('w'): // week day as number has only one | |
2995 | width = 1; | |
2996 | break; | |
2997 | ||
2998 | default: | |
2999 | // default for all other fields | |
3000 | width = 2; | |
3001 | } | |
3002 | } | |
3003 | ||
f6bcfd97 BP |
3004 | // then the format itself |
3005 | switch ( *fmt ) | |
f0f951fa VZ |
3006 | { |
3007 | case _T('a'): // a weekday name | |
3008 | case _T('A'): | |
3009 | { | |
3010 | int flag = *fmt == _T('a') ? Name_Abbr : Name_Full; | |
3011 | wday = GetWeekDayFromName(GetAlphaToken(input), flag); | |
3012 | if ( wday == Inv_WeekDay ) | |
3013 | { | |
3014 | // no match | |
3015 | return (wxChar *)NULL; | |
3016 | } | |
3017 | } | |
68379eaf | 3018 | haveWDay = true; |
f0f951fa VZ |
3019 | break; |
3020 | ||
3021 | case _T('b'): // a month name | |
3022 | case _T('B'): | |
3023 | { | |
3024 | int flag = *fmt == _T('b') ? Name_Abbr : Name_Full; | |
3025 | mon = GetMonthFromName(GetAlphaToken(input), flag); | |
3026 | if ( mon == Inv_Month ) | |
3027 | { | |
3028 | // no match | |
3029 | return (wxChar *)NULL; | |
3030 | } | |
3031 | } | |
68379eaf | 3032 | haveMon = true; |
f0f951fa VZ |
3033 | break; |
3034 | ||
3035 | case _T('c'): // locale default date and time representation | |
3036 | { | |
3037 | wxDateTime dt; | |
3038 | ||
3039 | // this is the format which corresponds to ctime() output | |
3040 | // and strptime("%c") should parse it, so try it first | |
41acf5c0 | 3041 | static const wxChar *fmtCtime = _T("%a %b %d %H:%M:%S %Y"); |
f0f951fa VZ |
3042 | |
3043 | const wxChar *result = dt.ParseFormat(input, fmtCtime); | |
3044 | if ( !result ) | |
3045 | { | |
3046 | result = dt.ParseFormat(input, _T("%x %X")); | |
3047 | } | |
3048 | ||
3049 | if ( !result ) | |
3050 | { | |
3051 | result = dt.ParseFormat(input, _T("%X %x")); | |
3052 | } | |
3053 | ||
3054 | if ( !result ) | |
3055 | { | |
3056 | // we've tried everything and still no match | |
3057 | return (wxChar *)NULL; | |
3058 | } | |
3059 | ||
be4017f8 VZ |
3060 | Tm tm = dt.GetTm(); |
3061 | ||
3062 | haveDay = haveMon = haveYear = | |
68379eaf | 3063 | haveHour = haveMin = haveSec = true; |
be4017f8 VZ |
3064 | |
3065 | hour = tm.hour; | |
3066 | min = tm.min; | |
3067 | sec = tm.sec; | |
3068 | ||
3069 | year = tm.year; | |
3070 | mon = tm.mon; | |
3071 | mday = tm.mday; | |
3072 | ||
f0f951fa VZ |
3073 | input = result; |
3074 | } | |
3075 | break; | |
3076 | ||
3077 | case _T('d'): // day of a month (01-31) | |
f6bcfd97 BP |
3078 | if ( !GetNumericToken(width, input, &num) || |
3079 | (num > 31) || (num < 1) ) | |
f0f951fa VZ |
3080 | { |
3081 | // no match | |
3082 | return (wxChar *)NULL; | |
3083 | } | |
3084 | ||
3085 | // we can't check whether the day range is correct yet, will | |
3086 | // do it later - assume ok for now | |
68379eaf | 3087 | haveDay = true; |
f0f951fa VZ |
3088 | mday = (wxDateTime_t)num; |
3089 | break; | |
5f287370 | 3090 | |
f0f951fa | 3091 | case _T('H'): // hour in 24h format (00-23) |
f6bcfd97 | 3092 | if ( !GetNumericToken(width, input, &num) || (num > 23) ) |
f0f951fa VZ |
3093 | { |
3094 | // no match | |
3095 | return (wxChar *)NULL; | |
3096 | } | |
3097 | ||
68379eaf | 3098 | haveHour = true; |
f0f951fa VZ |
3099 | hour = (wxDateTime_t)num; |
3100 | break; | |
3101 | ||
3102 | case _T('I'): // hour in 12h format (01-12) | |
f6bcfd97 | 3103 | if ( !GetNumericToken(width, input, &num) || !num || (num > 12) ) |
f0f951fa VZ |
3104 | { |
3105 | // no match | |
3106 | return (wxChar *)NULL; | |
3107 | } | |
3108 | ||
68379eaf WS |
3109 | haveHour = true; |
3110 | hourIsIn12hFormat = true; | |
479cd5de | 3111 | hour = (wxDateTime_t)(num % 12); // 12 should be 0 |
f0f951fa VZ |
3112 | break; |
3113 | ||
3114 | case _T('j'): // day of the year | |
f6bcfd97 | 3115 | if ( !GetNumericToken(width, input, &num) || !num || (num > 366) ) |
f0f951fa VZ |
3116 | { |
3117 | // no match | |
3118 | return (wxChar *)NULL; | |
3119 | } | |
3120 | ||
68379eaf | 3121 | haveYDay = true; |
f0f951fa VZ |
3122 | yday = (wxDateTime_t)num; |
3123 | break; | |
3124 | ||
3125 | case _T('m'): // month as a number (01-12) | |
f6bcfd97 | 3126 | if ( !GetNumericToken(width, input, &num) || !num || (num > 12) ) |
f0f951fa VZ |
3127 | { |
3128 | // no match | |
3129 | return (wxChar *)NULL; | |
3130 | } | |
3131 | ||
68379eaf | 3132 | haveMon = true; |
be4017f8 | 3133 | mon = (Month)(num - 1); |
f0f951fa VZ |
3134 | break; |
3135 | ||
3136 | case _T('M'): // minute as a decimal number (00-59) | |
f6bcfd97 | 3137 | if ( !GetNumericToken(width, input, &num) || (num > 59) ) |
f0f951fa VZ |
3138 | { |
3139 | // no match | |
3140 | return (wxChar *)NULL; | |
3141 | } | |
3142 | ||
68379eaf | 3143 | haveMin = true; |
f0f951fa VZ |
3144 | min = (wxDateTime_t)num; |
3145 | break; | |
3146 | ||
3147 | case _T('p'): // AM or PM string | |
3148 | { | |
3149 | wxString am, pm, token = GetAlphaToken(input); | |
3150 | ||
3151 | GetAmPmStrings(&am, &pm); | |
bb8a8478 | 3152 | if (am.empty() && pm.empty()) |
0308fd61 | 3153 | return (wxChar *)NULL; // no am/pm strings defined |
f0f951fa VZ |
3154 | if ( token.CmpNoCase(pm) == 0 ) |
3155 | { | |
68379eaf | 3156 | isPM = true; |
f0f951fa VZ |
3157 | } |
3158 | else if ( token.CmpNoCase(am) != 0 ) | |
3159 | { | |
3160 | // no match | |
3161 | return (wxChar *)NULL; | |
3162 | } | |
3163 | } | |
3164 | break; | |
3165 | ||
3166 | case _T('r'): // time as %I:%M:%S %p | |
3167 | { | |
3168 | wxDateTime dt; | |
3169 | input = dt.ParseFormat(input, _T("%I:%M:%S %p")); | |
3170 | if ( !input ) | |
3171 | { | |
3172 | // no match | |
3173 | return (wxChar *)NULL; | |
3174 | } | |
3175 | ||
68379eaf | 3176 | haveHour = haveMin = haveSec = true; |
f0f951fa VZ |
3177 | |
3178 | Tm tm = dt.GetTm(); | |
3179 | hour = tm.hour; | |
3180 | min = tm.min; | |
3181 | sec = tm.sec; | |
3182 | } | |
3183 | break; | |
3184 | ||
3185 | case _T('R'): // time as %H:%M | |
3186 | { | |
3187 | wxDateTime dt; | |
3188 | input = dt.ParseFormat(input, _T("%H:%M")); | |
3189 | if ( !input ) | |
3190 | { | |
3191 | // no match | |
3192 | return (wxChar *)NULL; | |
3193 | } | |
3194 | ||
68379eaf | 3195 | haveHour = haveMin = true; |
f0f951fa VZ |
3196 | |
3197 | Tm tm = dt.GetTm(); | |
3198 | hour = tm.hour; | |
3199 | min = tm.min; | |
3200 | } | |
3201 | ||
3202 | case _T('S'): // second as a decimal number (00-61) | |
f6bcfd97 | 3203 | if ( !GetNumericToken(width, input, &num) || (num > 61) ) |
f0f951fa VZ |
3204 | { |
3205 | // no match | |
3206 | return (wxChar *)NULL; | |
3207 | } | |
3208 | ||
68379eaf | 3209 | haveSec = true; |
f0f951fa VZ |
3210 | sec = (wxDateTime_t)num; |
3211 | break; | |
3212 | ||
3213 | case _T('T'): // time as %H:%M:%S | |
3214 | { | |
3215 | wxDateTime dt; | |
3216 | input = dt.ParseFormat(input, _T("%H:%M:%S")); | |
3217 | if ( !input ) | |
3218 | { | |
3219 | // no match | |
3220 | return (wxChar *)NULL; | |
3221 | } | |
3222 | ||
68379eaf | 3223 | haveHour = haveMin = haveSec = true; |
f0f951fa VZ |
3224 | |
3225 | Tm tm = dt.GetTm(); | |
3226 | hour = tm.hour; | |
3227 | min = tm.min; | |
3228 | sec = tm.sec; | |
3229 | } | |
be4017f8 | 3230 | break; |
f0f951fa VZ |
3231 | |
3232 | case _T('w'): // weekday as a number (0-6), Sunday = 0 | |
f6bcfd97 | 3233 | if ( !GetNumericToken(width, input, &num) || (wday > 6) ) |
f0f951fa VZ |
3234 | { |
3235 | // no match | |
3236 | return (wxChar *)NULL; | |
3237 | } | |
3238 | ||
68379eaf | 3239 | haveWDay = true; |
f0f951fa VZ |
3240 | wday = (WeekDay)num; |
3241 | break; | |
3242 | ||
3243 | case _T('x'): // locale default date representation | |
3244 | #ifdef HAVE_STRPTIME | |
2423ef22 | 3245 | // try using strptime() -- it may fail even if the input is |
f0f951fa | 3246 | // correct but the date is out of range, so we will fall back |
2423ef22 | 3247 | // to our generic code anyhow |
f0f951fa VZ |
3248 | { |
3249 | struct tm tm; | |
2423ef22 VZ |
3250 | |
3251 | const wxChar *result = CallStrptime(input, "%x", &tm); | |
f0f951fa VZ |
3252 | if ( result ) |
3253 | { | |
3254 | input = result; | |
3255 | ||
68379eaf | 3256 | haveDay = haveMon = haveYear = true; |
f0f951fa VZ |
3257 | |
3258 | year = 1900 + tm.tm_year; | |
3259 | mon = (Month)tm.tm_mon; | |
3260 | mday = tm.tm_mday; | |
3261 | ||
3262 | break; | |
3263 | } | |
3264 | } | |
3265 | #endif // HAVE_STRPTIME | |
3266 | ||
f0f951fa VZ |
3267 | { |
3268 | wxDateTime dt; | |
3269 | ||
41acf5c0 | 3270 | wxString fmtDate, fmtDateAlt; |
64f9c100 | 3271 | |
f0f951fa VZ |
3272 | if ( IsWestEuropeanCountry(GetCountry()) || |
3273 | GetCountry() == Russia ) | |
3274 | { | |
3275 | fmtDate = _T("%d/%m/%y"); | |
41acf5c0 | 3276 | fmtDateAlt = _T("%m/%d/%y"); |
f0f951fa VZ |
3277 | } |
3278 | else // assume USA | |
3279 | { | |
3280 | fmtDate = _T("%m/%d/%y"); | |
41acf5c0 | 3281 | fmtDateAlt = _T("%d/%m/%y"); |
f0f951fa VZ |
3282 | } |
3283 | ||
64f9c100 VS |
3284 | #ifdef __WINDOWS__ |
3285 | // The above doesn't work for all locales, try to query | |
3286 | // Windows for the right way of formatting the date: | |
3287 | GetLocaleDateFormat(&fmtDate); | |
3288 | #endif | |
3289 | ||
f0f951fa | 3290 | const wxChar *result = dt.ParseFormat(input, fmtDate); |
41acf5c0 VZ |
3291 | |
3292 | if ( !result ) | |
3293 | { | |
3294 | // ok, be nice and try another one | |
3295 | result = dt.ParseFormat(input, fmtDateAlt); | |
3296 | } | |
3297 | ||
f0f951fa VZ |
3298 | if ( !result ) |
3299 | { | |
3300 | // bad luck | |
3301 | return (wxChar *)NULL; | |
3302 | } | |
3303 | ||
3304 | Tm tm = dt.GetTm(); | |
3305 | ||
68379eaf | 3306 | haveDay = haveMon = haveYear = true; |
f0f951fa VZ |
3307 | |
3308 | year = tm.year; | |
3309 | mon = tm.mon; | |
3310 | mday = tm.mday; | |
3311 | ||
3312 | input = result; | |
3313 | } | |
3314 | ||
3315 | break; | |
3316 | ||
3317 | case _T('X'): // locale default time representation | |
3318 | #ifdef HAVE_STRPTIME | |
3319 | { | |
3320 | // use strptime() to do it for us (FIXME !Unicode friendly) | |
3321 | struct tm tm; | |
2423ef22 | 3322 | input = CallStrptime(input, "%X", &tm); |
f0f951fa VZ |
3323 | if ( !input ) |
3324 | { | |
3325 | return (wxChar *)NULL; | |
3326 | } | |
3327 | ||
68379eaf | 3328 | haveHour = haveMin = haveSec = true; |
f0f951fa VZ |
3329 | |
3330 | hour = tm.tm_hour; | |
3331 | min = tm.tm_min; | |
3332 | sec = tm.tm_sec; | |
3333 | } | |
3334 | #else // !HAVE_STRPTIME | |
3335 | // TODO under Win32 we can query the LOCALE_ITIME system | |
3336 | // setting which says whether the default time format is | |
3337 | // 24 or 12 hour | |
3338 | { | |
3339 | // try to parse what follows as "%H:%M:%S" and, if this | |
3340 | // fails, as "%I:%M:%S %p" - this should catch the most | |
3341 | // common cases | |
3342 | wxDateTime dt; | |
3343 | ||
3344 | const wxChar *result = dt.ParseFormat(input, _T("%T")); | |
3345 | if ( !result ) | |
3346 | { | |
3347 | result = dt.ParseFormat(input, _T("%r")); | |
3348 | } | |
3349 | ||
3350 | if ( !result ) | |
3351 | { | |
3352 | // no match | |
3353 | return (wxChar *)NULL; | |
3354 | } | |
3355 | ||
68379eaf | 3356 | haveHour = haveMin = haveSec = true; |
f0f951fa VZ |
3357 | |
3358 | Tm tm = dt.GetTm(); | |
3359 | hour = tm.hour; | |
3360 | min = tm.min; | |
3361 | sec = tm.sec; | |
3362 | ||
3363 | input = result; | |
3364 | } | |
3365 | #endif // HAVE_STRPTIME/!HAVE_STRPTIME | |
3366 | break; | |
3367 | ||
3368 | case _T('y'): // year without century (00-99) | |
f6bcfd97 | 3369 | if ( !GetNumericToken(width, input, &num) || (num > 99) ) |
f0f951fa VZ |
3370 | { |
3371 | // no match | |
3372 | return (wxChar *)NULL; | |
3373 | } | |
3374 | ||
68379eaf | 3375 | haveYear = true; |
f6bcfd97 BP |
3376 | |
3377 | // TODO should have an option for roll over date instead of | |
3378 | // hard coding it here | |
3379 | year = (num > 30 ? 1900 : 2000) + (wxDateTime_t)num; | |
f0f951fa VZ |
3380 | break; |
3381 | ||
3382 | case _T('Y'): // year with century | |
f6bcfd97 | 3383 | if ( !GetNumericToken(width, input, &num) ) |
f0f951fa VZ |
3384 | { |
3385 | // no match | |
3386 | return (wxChar *)NULL; | |
3387 | } | |
3388 | ||
68379eaf | 3389 | haveYear = true; |
be4017f8 | 3390 | year = (wxDateTime_t)num; |
f0f951fa VZ |
3391 | break; |
3392 | ||
3393 | case _T('Z'): // timezone name | |
3394 | wxFAIL_MSG(_T("TODO")); | |
3395 | break; | |
3396 | ||
3397 | case _T('%'): // a percent sign | |
3398 | if ( *input++ != _T('%') ) | |
3399 | { | |
3400 | // no match | |
3401 | return (wxChar *)NULL; | |
3402 | } | |
3403 | break; | |
3404 | ||
3405 | case 0: // the end of string | |
3406 | wxFAIL_MSG(_T("unexpected format end")); | |
3407 | ||
3408 | // fall through | |
3409 | ||
3410 | default: // not a known format spec | |
3411 | return (wxChar *)NULL; | |
3412 | } | |
3413 | } | |
3414 | ||
3415 | // format matched, try to construct a date from what we have now | |
3416 | Tm tmDef; | |
3417 | if ( dateDef.IsValid() ) | |
3418 | { | |
3419 | // take this date as default | |
3420 | tmDef = dateDef.GetTm(); | |
3421 | } | |
c3302e7e | 3422 | else if ( IsValid() ) |
f0f951fa VZ |
3423 | { |
3424 | // if this date is valid, don't change it | |
3425 | tmDef = GetTm(); | |
3426 | } | |
3427 | else | |
3428 | { | |
3429 | // no default and this date is invalid - fall back to Today() | |
3430 | tmDef = Today().GetTm(); | |
3431 | } | |
3432 | ||
3433 | Tm tm = tmDef; | |
3434 | ||
3435 | // set the date | |
3436 | if ( haveYear ) | |
3437 | { | |
3438 | tm.year = year; | |
3439 | } | |
3440 | ||
3441 | // TODO we don't check here that the values are consistent, if both year | |
3442 | // day and month/day were found, we just ignore the year day and we | |
3443 | // also always ignore the week day | |
3444 | if ( haveMon && haveDay ) | |
3445 | { | |
be4017f8 VZ |
3446 | if ( mday > GetNumOfDaysInMonth(tm.year, mon) ) |
3447 | { | |
3448 | wxLogDebug(_T("bad month day in wxDateTime::ParseFormat")); | |
3449 | ||
3450 | return (wxChar *)NULL; | |
3451 | } | |
3452 | ||
f0f951fa VZ |
3453 | tm.mon = mon; |
3454 | tm.mday = mday; | |
3455 | } | |
3456 | else if ( haveYDay ) | |
3457 | { | |
be4017f8 VZ |
3458 | if ( yday > GetNumberOfDays(tm.year) ) |
3459 | { | |
3460 | wxLogDebug(_T("bad year day in wxDateTime::ParseFormat")); | |
3461 | ||
3462 | return (wxChar *)NULL; | |
3463 | } | |
3464 | ||
f0f951fa VZ |
3465 | Tm tm2 = wxDateTime(1, Jan, tm.year).SetToYearDay(yday).GetTm(); |
3466 | ||
3467 | tm.mon = tm2.mon; | |
3468 | tm.mday = tm2.mday; | |
3469 | } | |
3470 | ||
3471 | // deal with AM/PM | |
3472 | if ( haveHour && hourIsIn12hFormat && isPM ) | |
3473 | { | |
3474 | // translate to 24hour format | |
3475 | hour += 12; | |
3476 | } | |
3477 | //else: either already in 24h format or no translation needed | |
3478 | ||
3479 | // set the time | |
3480 | if ( haveHour ) | |
3481 | { | |
5f287370 | 3482 | tm.hour = hour; |
f0f951fa VZ |
3483 | } |
3484 | ||
3485 | if ( haveMin ) | |
3486 | { | |
3487 | tm.min = min; | |
3488 | } | |
3489 | ||
3490 | if ( haveSec ) | |
3491 | { | |
3492 | tm.sec = sec; | |
3493 | } | |
3494 | ||
3495 | Set(tm); | |
3496 | ||
88a036ce VZ |
3497 | // finally check that the week day is consistent -- if we had it |
3498 | if ( haveWDay && GetWeekDay() != wday ) | |
3499 | { | |
3500 | wxLogDebug(_T("inconsistsnet week day in wxDateTime::ParseFormat()")); | |
3501 | ||
3502 | return NULL; | |
3503 | } | |
3504 | ||
f0f951fa | 3505 | return input; |
cd0b1709 VZ |
3506 | } |
3507 | ||
3508 | const wxChar *wxDateTime::ParseDateTime(const wxChar *date) | |
3509 | { | |
3510 | wxCHECK_MSG( date, (wxChar *)NULL, _T("NULL pointer in wxDateTime::Parse") ); | |
3511 | ||
de07d200 VZ |
3512 | // Set to current day and hour, so strings like '14:00' becomes today at |
3513 | // 14, not some other random date | |
3514 | wxDateTime dtDate = wxDateTime::Today(); | |
3515 | wxDateTime dtTime = wxDateTime::Today(); | |
cd0b1709 | 3516 | |
de07d200 VZ |
3517 | const wxChar* pchTime; |
3518 | ||
3519 | // Try to parse the beginning of the string as a date | |
3520 | const wxChar* pchDate = dtDate.ParseDate(date); | |
3521 | ||
3522 | // We got a date in the beginning, see if there is a time specified after the date | |
3523 | if ( pchDate ) | |
3524 | { | |
3525 | // Skip spaces, as the ParseTime() function fails on spaces | |
3526 | while ( wxIsspace(*pchDate) ) | |
3527 | pchDate++; | |
3528 | ||
3529 | pchTime = dtTime.ParseTime(pchDate); | |
3530 | } | |
3531 | else // no date in the beginning | |
3532 | { | |
3533 | // check and see if we have a time followed by a date | |
3534 | pchTime = dtTime.ParseTime(date); | |
3535 | if ( pchTime ) | |
3536 | { | |
3537 | while ( wxIsspace(*pchTime) ) | |
3538 | pchTime++; | |
3539 | ||
3540 | pchDate = dtDate.ParseDate(pchTime); | |
3541 | } | |
3542 | } | |
3543 | ||
3544 | // If we have a date specified, set our own data to the same date | |
3545 | if ( !pchDate || !pchTime ) | |
3546 | return NULL; | |
3547 | ||
3548 | Set(dtDate.GetDay(), dtDate.GetMonth(), dtDate.GetYear(), | |
3549 | dtTime.GetHour(), dtTime.GetMinute(), dtTime.GetSecond(), | |
3550 | dtTime.GetMillisecond()); | |
3551 | ||
3552 | // Return endpoint of scan | |
3553 | return pchDate > pchTime ? pchDate : pchTime; | |
cd0b1709 VZ |
3554 | } |
3555 | ||
3556 | const wxChar *wxDateTime::ParseDate(const wxChar *date) | |
3557 | { | |
3558 | // this is a simplified version of ParseDateTime() which understands only | |
3559 | // "today" (for wxDate compatibility) and digits only otherwise (and not | |
3560 | // all esoteric constructions ParseDateTime() knows about) | |
3561 | ||
3562 | wxCHECK_MSG( date, (wxChar *)NULL, _T("NULL pointer in wxDateTime::Parse") ); | |
3563 | ||
3564 | const wxChar *p = date; | |
3565 | while ( wxIsspace(*p) ) | |
3566 | p++; | |
3567 | ||
fd9f9f4c VZ |
3568 | // some special cases |
3569 | static struct | |
cd0b1709 | 3570 | { |
fd9f9f4c VZ |
3571 | const wxChar *str; |
3572 | int dayDiffFromToday; | |
3573 | } literalDates[] = | |
3574 | { | |
3575 | { wxTRANSLATE("today"), 0 }, | |
3576 | { wxTRANSLATE("yesterday"), -1 }, | |
3577 | { wxTRANSLATE("tomorrow"), 1 }, | |
3578 | }; | |
3579 | ||
3580 | for ( size_t n = 0; n < WXSIZEOF(literalDates); n++ ) | |
3581 | { | |
3582 | wxString date = wxGetTranslation(literalDates[n].str); | |
3583 | size_t len = date.length(); | |
907173e5 | 3584 | if ( wxStrlen(p) >= len ) |
fd9f9f4c | 3585 | { |
907173e5 WS |
3586 | wxString str(p, len); |
3587 | if ( str.CmpNoCase(date) == 0 ) | |
fd9f9f4c | 3588 | { |
907173e5 WS |
3589 | // nothing can follow this, so stop here |
3590 | p += len; | |
3591 | ||
3592 | int dayDiffFromToday = literalDates[n].dayDiffFromToday; | |
3593 | *this = Today(); | |
3594 | if ( dayDiffFromToday ) | |
3595 | { | |
3596 | *this += wxDateSpan::Days(dayDiffFromToday); | |
3597 | } | |
cd0b1709 | 3598 | |
907173e5 WS |
3599 | return p; |
3600 | } | |
fd9f9f4c | 3601 | } |
cd0b1709 VZ |
3602 | } |
3603 | ||
3ca6a5f0 BP |
3604 | // We try to guess what we have here: for each new (numeric) token, we |
3605 | // determine if it can be a month, day or a year. Of course, there is an | |
3606 | // ambiguity as some numbers may be days as well as months, so we also | |
3607 | // have the ability to back track. | |
3608 | ||
cd0b1709 | 3609 | // what do we have? |
68379eaf WS |
3610 | bool haveDay = false, // the months day? |
3611 | haveWDay = false, // the day of week? | |
3612 | haveMon = false, // the month? | |
3613 | haveYear = false; // the year? | |
cd0b1709 VZ |
3614 | |
3615 | // and the value of the items we have (init them to get rid of warnings) | |
3616 | WeekDay wday = Inv_WeekDay; | |
3617 | wxDateTime_t day = 0; | |
3618 | wxDateTime::Month mon = Inv_Month; | |
3619 | int year = 0; | |
3620 | ||
3621 | // tokenize the string | |
f6bcfd97 | 3622 | size_t nPosCur = 0; |
62ae2780 | 3623 | static const wxChar *dateDelimiters = _T(".,/-\t\r\n "); |
f6bcfd97 | 3624 | wxStringTokenizer tok(p, dateDelimiters); |
cd0b1709 VZ |
3625 | while ( tok.HasMoreTokens() ) |
3626 | { | |
3627 | wxString token = tok.GetNextToken(); | |
3ca6a5f0 BP |
3628 | if ( !token ) |
3629 | continue; | |
cd0b1709 VZ |
3630 | |
3631 | // is it a number? | |
3632 | unsigned long val; | |
fde5a86b | 3633 | if ( token.ToULong(&val) ) |
cd0b1709 VZ |
3634 | { |
3635 | // guess what this number is | |
3636 | ||
68379eaf WS |
3637 | bool isDay = false, |
3638 | isMonth = false, | |
3639 | isYear = false; | |
3ca6a5f0 BP |
3640 | |
3641 | if ( !haveMon && val > 0 && val <= 12 ) | |
cd0b1709 | 3642 | { |
3ca6a5f0 | 3643 | // assume it is month |
68379eaf | 3644 | isMonth = true; |
3ca6a5f0 BP |
3645 | } |
3646 | else // not the month | |
3647 | { | |
560e2916 | 3648 | if ( haveDay ) |
cd0b1709 | 3649 | { |
560e2916 | 3650 | // this can only be the year |
68379eaf | 3651 | isYear = true; |
cd0b1709 | 3652 | } |
560e2916 | 3653 | else // may be either day or year |
cd0b1709 | 3654 | { |
6a27c749 | 3655 | wxDateTime_t max_days = (wxDateTime_t)( |
42841dfc | 3656 | haveMon |
560e2916 | 3657 | ? GetNumOfDaysInMonth(haveYear ? year : Inv_Year, mon) |
42841dfc WS |
3658 | : 31 |
3659 | ); | |
560e2916 VZ |
3660 | |
3661 | // can it be day? | |
6a27c749 | 3662 | if ( (val == 0) || (val > (unsigned long)max_days) ) |
560e2916 VZ |
3663 | { |
3664 | // no | |
68379eaf | 3665 | isYear = true; |
560e2916 VZ |
3666 | } |
3667 | else // yes, suppose it's the day | |
3668 | { | |
68379eaf | 3669 | isDay = true; |
560e2916 | 3670 | } |
cd0b1709 VZ |
3671 | } |
3672 | } | |
3673 | ||
cd0b1709 VZ |
3674 | if ( isYear ) |
3675 | { | |
3676 | if ( haveYear ) | |
cd0b1709 | 3677 | break; |
cd0b1709 | 3678 | |
68379eaf | 3679 | haveYear = true; |
cd0b1709 | 3680 | |
479cd5de | 3681 | year = (wxDateTime_t)val; |
cd0b1709 | 3682 | } |
3ca6a5f0 | 3683 | else if ( isDay ) |
cd0b1709 | 3684 | { |
3ca6a5f0 | 3685 | if ( haveDay ) |
cd0b1709 | 3686 | break; |
cd0b1709 | 3687 | |
68379eaf | 3688 | haveDay = true; |
cd0b1709 | 3689 | |
3ca6a5f0 | 3690 | day = (wxDateTime_t)val; |
cd0b1709 | 3691 | } |
3ca6a5f0 | 3692 | else if ( isMonth ) |
cd0b1709 | 3693 | { |
68379eaf | 3694 | haveMon = true; |
cd0b1709 | 3695 | |
3ca6a5f0 | 3696 | mon = (Month)(val - 1); |
cd0b1709 VZ |
3697 | } |
3698 | } | |
3699 | else // not a number | |
3700 | { | |
f6bcfd97 BP |
3701 | // be careful not to overwrite the current mon value |
3702 | Month mon2 = GetMonthFromName(token, Name_Full | Name_Abbr); | |
3703 | if ( mon2 != Inv_Month ) | |
cd0b1709 VZ |
3704 | { |
3705 | // it's a month | |
3706 | if ( haveMon ) | |
3707 | { | |
6411a32c VZ |
3708 | // but we already have a month - maybe we guessed wrong? |
3709 | if ( !haveDay ) | |
3710 | { | |
f5cd9787 | 3711 | // no need to check in month range as always < 12, but |
6411a32c | 3712 | // the days are counted from 1 unlike the months |
42841dfc | 3713 | day = (wxDateTime_t)(mon + 1); |
68379eaf | 3714 | haveDay = true; |
6411a32c VZ |
3715 | } |
3716 | else | |
3717 | { | |
3718 | // could possible be the year (doesn't the year come | |
3719 | // before the month in the japanese format?) (FIXME) | |
3720 | break; | |
f5cd9787 | 3721 | } |
cd0b1709 VZ |
3722 | } |
3723 | ||
f6bcfd97 BP |
3724 | mon = mon2; |
3725 | ||
68379eaf | 3726 | haveMon = true; |
cd0b1709 | 3727 | } |
6411a32c | 3728 | else // not a valid month name |
cd0b1709 | 3729 | { |
f0f951fa | 3730 | wday = GetWeekDayFromName(token, Name_Full | Name_Abbr); |
cd0b1709 VZ |
3731 | if ( wday != Inv_WeekDay ) |
3732 | { | |
3733 | // a week day | |
3734 | if ( haveWDay ) | |
3735 | { | |
3736 | break; | |
3737 | } | |
3738 | ||
68379eaf | 3739 | haveWDay = true; |
cd0b1709 | 3740 | } |
6411a32c | 3741 | else // not a valid weekday name |
cd0b1709 VZ |
3742 | { |
3743 | // try the ordinals | |
3744 | static const wxChar *ordinals[] = | |
3745 | { | |
3746 | wxTRANSLATE("first"), | |
3747 | wxTRANSLATE("second"), | |
3748 | wxTRANSLATE("third"), | |
3749 | wxTRANSLATE("fourth"), | |
3750 | wxTRANSLATE("fifth"), | |
3751 | wxTRANSLATE("sixth"), | |
3752 | wxTRANSLATE("seventh"), | |
3753 | wxTRANSLATE("eighth"), | |
3754 | wxTRANSLATE("ninth"), | |
3755 | wxTRANSLATE("tenth"), | |
3756 | wxTRANSLATE("eleventh"), | |
3757 | wxTRANSLATE("twelfth"), | |
3758 | wxTRANSLATE("thirteenth"), | |
3759 | wxTRANSLATE("fourteenth"), | |
3760 | wxTRANSLATE("fifteenth"), | |
3761 | wxTRANSLATE("sixteenth"), | |
3762 | wxTRANSLATE("seventeenth"), | |
3763 | wxTRANSLATE("eighteenth"), | |
3764 | wxTRANSLATE("nineteenth"), | |
3765 | wxTRANSLATE("twentieth"), | |
3766 | // that's enough - otherwise we'd have problems with | |
6411a32c | 3767 | // composite (or not) ordinals |
cd0b1709 VZ |
3768 | }; |
3769 | ||
3770 | size_t n; | |
3771 | for ( n = 0; n < WXSIZEOF(ordinals); n++ ) | |
3772 | { | |
3773 | if ( token.CmpNoCase(ordinals[n]) == 0 ) | |
3774 | { | |
3775 | break; | |
3776 | } | |
3777 | } | |
3778 | ||
3779 | if ( n == WXSIZEOF(ordinals) ) | |
3780 | { | |
3781 | // stop here - something unknown | |
3782 | break; | |
3783 | } | |
3784 | ||
3785 | // it's a day | |
3786 | if ( haveDay ) | |
3787 | { | |
3788 | // don't try anything here (as in case of numeric day | |
3789 | // above) - the symbolic day spec should always | |
3790 | // precede the month/year | |
3791 | break; | |
3792 | } | |
3793 | ||
68379eaf | 3794 | haveDay = true; |
cd0b1709 | 3795 | |
33ac7e6f | 3796 | day = (wxDateTime_t)(n + 1); |
cd0b1709 VZ |
3797 | } |
3798 | } | |
3799 | } | |
f6bcfd97 BP |
3800 | |
3801 | nPosCur = tok.GetPosition(); | |
cd0b1709 VZ |
3802 | } |
3803 | ||
3804 | // either no more tokens or the scan was stopped by something we couldn't | |
3805 | // parse - in any case, see if we can construct a date from what we have | |
3806 | if ( !haveDay && !haveWDay ) | |
3807 | { | |
f6bcfd97 | 3808 | wxLogDebug(_T("ParseDate: no day, no weekday hence no date.")); |
cd0b1709 VZ |
3809 | |
3810 | return (wxChar *)NULL; | |
3811 | } | |
3812 | ||
3813 | if ( haveWDay && (haveMon || haveYear || haveDay) && | |
f6bcfd97 | 3814 | !(haveDay && haveMon && haveYear) ) |
cd0b1709 VZ |
3815 | { |
3816 | // without adjectives (which we don't support here) the week day only | |
3817 | // makes sense completely separately or with the full date | |
3818 | // specification (what would "Wed 1999" mean?) | |
3819 | return (wxChar *)NULL; | |
3820 | } | |
3821 | ||
f6bcfd97 BP |
3822 | if ( !haveWDay && haveYear && !(haveDay && haveMon) ) |
3823 | { | |
3824 | // may be we have month and day instead of day and year? | |
3825 | if ( haveDay && !haveMon ) | |
3826 | { | |
3827 | if ( day <= 12 ) | |
3828 | { | |
3829 | // exchange day and month | |
3830 | mon = (wxDateTime::Month)(day - 1); | |
3831 | ||
3832 | // we're in the current year then | |
196be0f1 | 3833 | if ( (year > 0) && (year <= (int)GetNumOfDaysInMonth(Inv_Year, mon)) ) |
f6bcfd97 | 3834 | { |
42841dfc | 3835 | day = (wxDateTime_t)year; |
f6bcfd97 | 3836 | |
68379eaf WS |
3837 | haveMon = true; |
3838 | haveYear = false; | |
f6bcfd97 | 3839 | } |
68379eaf | 3840 | //else: no, can't exchange, leave haveMon == false |
f6bcfd97 BP |
3841 | } |
3842 | } | |
3843 | ||
3844 | if ( !haveMon ) | |
3845 | { | |
3846 | // if we give the year, month and day must be given too | |
3847 | wxLogDebug(_T("ParseDate: day and month should be specified if year is.")); | |
3848 | ||
3849 | return (wxChar *)NULL; | |
3850 | } | |
3851 | } | |
3852 | ||
cd0b1709 VZ |
3853 | if ( !haveMon ) |
3854 | { | |
3855 | mon = GetCurrentMonth(); | |
3856 | } | |
3857 | ||
3858 | if ( !haveYear ) | |
3859 | { | |
3860 | year = GetCurrentYear(); | |
3861 | } | |
3862 | ||
3863 | if ( haveDay ) | |
3864 | { | |
3865 | Set(day, mon, year); | |
3866 | ||
3867 | if ( haveWDay ) | |
3868 | { | |
3869 | // check that it is really the same | |
3870 | if ( GetWeekDay() != wday ) | |
3871 | { | |
3872 | // inconsistency detected | |
f6bcfd97 BP |
3873 | wxLogDebug(_T("ParseDate: inconsistent day/weekday.")); |
3874 | ||
cd0b1709 VZ |
3875 | return (wxChar *)NULL; |
3876 | } | |
3877 | } | |
3878 | } | |
3879 | else // haveWDay | |
3880 | { | |
3881 | *this = Today(); | |
3882 | ||
3883 | SetToWeekDayInSameWeek(wday); | |
3884 | } | |
3885 | ||
f6bcfd97 BP |
3886 | // return the pointer to the first unparsed char |
3887 | p += nPosCur; | |
3888 | if ( nPosCur && wxStrchr(dateDelimiters, *(p - 1)) ) | |
3889 | { | |
3890 | // if we couldn't parse the token after the delimiter, put back the | |
3891 | // delimiter as well | |
3892 | p--; | |
3893 | } | |
3894 | ||
3895 | return p; | |
cd0b1709 VZ |
3896 | } |
3897 | ||
3898 | const wxChar *wxDateTime::ParseTime(const wxChar *time) | |
3899 | { | |
3900 | wxCHECK_MSG( time, (wxChar *)NULL, _T("NULL pointer in wxDateTime::Parse") ); | |
3901 | ||
f0f951fa VZ |
3902 | // first try some extra things |
3903 | static const struct | |
3904 | { | |
3905 | const wxChar *name; | |
3906 | wxDateTime_t hour; | |
3907 | } stdTimes[] = | |
3908 | { | |
3909 | { wxTRANSLATE("noon"), 12 }, | |
3910 | { wxTRANSLATE("midnight"), 00 }, | |
3911 | // anything else? | |
3912 | }; | |
cd0b1709 | 3913 | |
f0f951fa VZ |
3914 | for ( size_t n = 0; n < WXSIZEOF(stdTimes); n++ ) |
3915 | { | |
3916 | wxString timeString = wxGetTranslation(stdTimes[n].name); | |
3917 | size_t len = timeString.length(); | |
3918 | if ( timeString.CmpNoCase(wxString(time, len)) == 0 ) | |
3919 | { | |
4ded51f2 CE |
3920 | // casts required by DigitalMars |
3921 | Set(stdTimes[n].hour, wxDateTime_t(0), wxDateTime_t(0)); | |
f0f951fa VZ |
3922 | |
3923 | return time + len; | |
3924 | } | |
3925 | } | |
3926 | ||
c7f9a482 VZ |
3927 | // try all time formats we may think about in the order from longest to |
3928 | // shortest | |
3929 | ||
3930 | // 12hour with AM/PM? | |
3931 | const wxChar *result = ParseFormat(time, _T("%I:%M:%S %p")); | |
3932 | ||
f0f951fa VZ |
3933 | if ( !result ) |
3934 | { | |
3935 | // normally, it's the same, but why not try it? | |
3936 | result = ParseFormat(time, _T("%H:%M:%S")); | |
3937 | } | |
3938 | ||
3939 | if ( !result ) | |
3940 | { | |
c7f9a482 VZ |
3941 | // 12hour with AM/PM but without seconds? |
3942 | result = ParseFormat(time, _T("%I:%M %p")); | |
f0f951fa VZ |
3943 | } |
3944 | ||
3945 | if ( !result ) | |
3946 | { | |
3947 | // without seconds? | |
3948 | result = ParseFormat(time, _T("%H:%M")); | |
3949 | } | |
3950 | ||
3951 | if ( !result ) | |
3952 | { | |
c7f9a482 VZ |
3953 | // just the hour and AM/PM? |
3954 | result = ParseFormat(time, _T("%I %p")); | |
f0f951fa VZ |
3955 | } |
3956 | ||
3957 | if ( !result ) | |
3958 | { | |
3959 | // just the hour? | |
3960 | result = ParseFormat(time, _T("%H")); | |
3961 | } | |
3962 | ||
3963 | if ( !result ) | |
3964 | { | |
c7f9a482 VZ |
3965 | // parse the standard format: normally it is one of the formats above |
3966 | // but it may be set to something completely different by the user | |
3967 | result = ParseFormat(time, _T("%X")); | |
f0f951fa VZ |
3968 | } |
3969 | ||
3970 | // TODO: parse timezones | |
3971 | ||
3972 | return result; | |
cd0b1709 VZ |
3973 | } |
3974 | ||
4f6aed9c VZ |
3975 | // ---------------------------------------------------------------------------- |
3976 | // Workdays and holidays support | |
3977 | // ---------------------------------------------------------------------------- | |
3978 | ||
3979 | bool wxDateTime::IsWorkDay(Country WXUNUSED(country)) const | |
3980 | { | |
3981 | return !wxDateTimeHolidayAuthority::IsHoliday(*this); | |
3982 | } | |
3983 | ||
26364344 WS |
3984 | // ============================================================================ |
3985 | // wxDateSpan | |
3986 | // ============================================================================ | |
3987 | ||
3988 | wxDateSpan WXDLLIMPEXP_BASE operator*(int n, const wxDateSpan& ds) | |
3989 | { | |
3990 | wxDateSpan ds1(ds); | |
3991 | return ds1.Multiply(n); | |
3992 | } | |
3993 | ||
fcc3d7cb VZ |
3994 | // ============================================================================ |
3995 | // wxTimeSpan | |
3996 | // ============================================================================ | |
3997 | ||
26364344 WS |
3998 | wxTimeSpan WXDLLIMPEXP_BASE operator*(int n, const wxTimeSpan& ts) |
3999 | { | |
4000 | return wxTimeSpan(ts).Multiply(n); | |
4001 | } | |
4002 | ||
033400eb VZ |
4003 | // this enum is only used in wxTimeSpan::Format() below but we can't declare |
4004 | // it locally to the method as it provokes an internal compiler error in egcs | |
4005 | // 2.91.60 when building with -O2 | |
4006 | enum TimeSpanPart | |
4007 | { | |
4008 | Part_Week, | |
4009 | Part_Day, | |
4010 | Part_Hour, | |
4011 | Part_Min, | |
4012 | Part_Sec, | |
4013 | Part_MSec | |
4014 | }; | |
4015 | ||
e6ec579c VZ |
4016 | // not all strftime(3) format specifiers make sense here because, for example, |
4017 | // a time span doesn't have a year nor a timezone | |
4018 | // | |
4019 | // Here are the ones which are supported (all of them are supported by strftime | |
4020 | // as well): | |
4021 | // %H hour in 24 hour format | |
4022 | // %M minute (00 - 59) | |
4023 | // %S second (00 - 59) | |
4024 | // %% percent sign | |
4025 | // | |
4026 | // Also, for MFC CTimeSpan compatibility, we support | |
4027 | // %D number of days | |
4028 | // | |
4029 | // And, to be better than MFC :-), we also have | |
4030 | // %E number of wEeks | |
4031 | // %l milliseconds (000 - 999) | |
fcc3d7cb VZ |
4032 | wxString wxTimeSpan::Format(const wxChar *format) const |
4033 | { | |
525d8583 | 4034 | wxCHECK_MSG( format, wxEmptyString, _T("NULL format in wxTimeSpan::Format") ); |
fcc3d7cb VZ |
4035 | |
4036 | wxString str; | |
5b735202 | 4037 | str.Alloc(wxStrlen(format)); |
e6ec579c | 4038 | |
df05cdc5 VZ |
4039 | // Suppose we have wxTimeSpan ts(1 /* hour */, 2 /* min */, 3 /* sec */) |
4040 | // | |
4041 | // Then, of course, ts.Format("%H:%M:%S") must return "01:02:03", but the | |
4042 | // question is what should ts.Format("%S") do? The code here returns "3273" | |
4043 | // in this case (i.e. the total number of seconds, not just seconds % 60) | |
4044 | // because, for me, this call means "give me entire time interval in | |
4045 | // seconds" and not "give me the seconds part of the time interval" | |
4046 | // | |
4047 | // If we agree that it should behave like this, it is clear that the | |
4048 | // interpretation of each format specifier depends on the presence of the | |
4049 | // other format specs in the string: if there was "%H" before "%M", we | |
4050 | // should use GetMinutes() % 60, otherwise just GetMinutes() &c | |
4051 | ||
4052 | // we remember the most important unit found so far | |
033400eb | 4053 | TimeSpanPart partBiggest = Part_MSec; |
df05cdc5 | 4054 | |
f6bcfd97 | 4055 | for ( const wxChar *pch = format; *pch; pch++ ) |
e6ec579c VZ |
4056 | { |
4057 | wxChar ch = *pch; | |
4058 | ||
f6bcfd97 | 4059 | if ( ch == _T('%') ) |
e6ec579c | 4060 | { |
df05cdc5 VZ |
4061 | // the start of the format specification of the printf() below |
4062 | wxString fmtPrefix = _T('%'); | |
4063 | ||
4064 | // the number | |
4065 | long n; | |
e6ec579c | 4066 | |
f6bcfd97 | 4067 | ch = *++pch; // get the format spec char |
e6ec579c VZ |
4068 | switch ( ch ) |
4069 | { | |
4070 | default: | |
4071 | wxFAIL_MSG( _T("invalid format character") ); | |
4072 | // fall through | |
4073 | ||
f6bcfd97 | 4074 | case _T('%'): |
df05cdc5 VZ |
4075 | str += ch; |
4076 | ||
4077 | // skip the part below switch | |
4078 | continue; | |
e6ec579c | 4079 | |
f6bcfd97 | 4080 | case _T('D'): |
df05cdc5 VZ |
4081 | n = GetDays(); |
4082 | if ( partBiggest < Part_Day ) | |
4083 | { | |
4084 | n %= DAYS_PER_WEEK; | |
4085 | } | |
4086 | else | |
4087 | { | |
4088 | partBiggest = Part_Day; | |
4089 | } | |
e6ec579c VZ |
4090 | break; |
4091 | ||
f6bcfd97 | 4092 | case _T('E'): |
df05cdc5 VZ |
4093 | partBiggest = Part_Week; |
4094 | n = GetWeeks(); | |
e6ec579c VZ |
4095 | break; |
4096 | ||
f6bcfd97 | 4097 | case _T('H'): |
df05cdc5 VZ |
4098 | n = GetHours(); |
4099 | if ( partBiggest < Part_Hour ) | |
4100 | { | |
4101 | n %= HOURS_PER_DAY; | |
4102 | } | |
4103 | else | |
4104 | { | |
4105 | partBiggest = Part_Hour; | |
4106 | } | |
4107 | ||
4108 | fmtPrefix += _T("02"); | |
e6ec579c VZ |
4109 | break; |
4110 | ||
f6bcfd97 | 4111 | case _T('l'): |
df05cdc5 VZ |
4112 | n = GetMilliseconds().ToLong(); |
4113 | if ( partBiggest < Part_MSec ) | |
4114 | { | |
4115 | n %= 1000; | |
4116 | } | |
4117 | //else: no need to reset partBiggest to Part_MSec, it is | |
4118 | // the least significant one anyhow | |
4119 | ||
4120 | fmtPrefix += _T("03"); | |
e6ec579c VZ |
4121 | break; |
4122 | ||
f6bcfd97 | 4123 | case _T('M'): |
df05cdc5 VZ |
4124 | n = GetMinutes(); |
4125 | if ( partBiggest < Part_Min ) | |
4126 | { | |
4127 | n %= MIN_PER_HOUR; | |
4128 | } | |
4129 | else | |
4130 | { | |
4131 | partBiggest = Part_Min; | |
4132 | } | |
4133 | ||
4134 | fmtPrefix += _T("02"); | |
e6ec579c VZ |
4135 | break; |
4136 | ||
f6bcfd97 | 4137 | case _T('S'): |
df05cdc5 VZ |
4138 | n = GetSeconds().ToLong(); |
4139 | if ( partBiggest < Part_Sec ) | |
4140 | { | |
4141 | n %= SEC_PER_MIN; | |
4142 | } | |
4143 | else | |
4144 | { | |
4145 | partBiggest = Part_Sec; | |
4146 | } | |
4147 | ||
4148 | fmtPrefix += _T("02"); | |
e6ec579c VZ |
4149 | break; |
4150 | } | |
4151 | ||
df05cdc5 VZ |
4152 | str += wxString::Format(fmtPrefix + _T("ld"), n); |
4153 | } | |
4154 | else | |
4155 | { | |
4156 | // normal character, just copy | |
4157 | str += ch; | |
e6ec579c | 4158 | } |
e6ec579c | 4159 | } |
fcc3d7cb VZ |
4160 | |
4161 | return str; | |
4162 | } | |
4f6aed9c VZ |
4163 | |
4164 | // ============================================================================ | |
4165 | // wxDateTimeHolidayAuthority and related classes | |
4166 | // ============================================================================ | |
4167 | ||
4168 | #include "wx/arrimpl.cpp" | |
4169 | ||
f6bcfd97 | 4170 | WX_DEFINE_OBJARRAY(wxDateTimeArray); |
4f6aed9c VZ |
4171 | |
4172 | static int wxCMPFUNC_CONV | |
4173 | wxDateTimeCompareFunc(wxDateTime **first, wxDateTime **second) | |
4174 | { | |
4175 | wxDateTime dt1 = **first, | |
4176 | dt2 = **second; | |
4177 | ||
4178 | return dt1 == dt2 ? 0 : dt1 < dt2 ? -1 : +1; | |
4179 | } | |
4180 | ||
4181 | // ---------------------------------------------------------------------------- | |
4182 | // wxDateTimeHolidayAuthority | |
4183 | // ---------------------------------------------------------------------------- | |
4184 | ||
4185 | wxHolidayAuthoritiesArray wxDateTimeHolidayAuthority::ms_authorities; | |
4186 | ||
4187 | /* static */ | |
4188 | bool wxDateTimeHolidayAuthority::IsHoliday(const wxDateTime& dt) | |
4189 | { | |
df5168c4 | 4190 | size_t count = ms_authorities.size(); |
4f6aed9c VZ |
4191 | for ( size_t n = 0; n < count; n++ ) |
4192 | { | |
4193 | if ( ms_authorities[n]->DoIsHoliday(dt) ) | |
4194 | { | |
68379eaf | 4195 | return true; |
4f6aed9c VZ |
4196 | } |
4197 | } | |
4198 | ||
68379eaf | 4199 | return false; |
4f6aed9c VZ |
4200 | } |
4201 | ||
4202 | /* static */ | |
4203 | size_t | |
4204 | wxDateTimeHolidayAuthority::GetHolidaysInRange(const wxDateTime& dtStart, | |
4205 | const wxDateTime& dtEnd, | |
4206 | wxDateTimeArray& holidays) | |
4207 | { | |
4208 | wxDateTimeArray hol; | |
4209 | ||
df5168c4 | 4210 | holidays.Clear(); |
4f6aed9c | 4211 | |
df5168c4 | 4212 | size_t count = ms_authorities.size(); |
6dc6fda6 | 4213 | for ( size_t nAuth = 0; nAuth < count; nAuth++ ) |
4f6aed9c | 4214 | { |
6dc6fda6 | 4215 | ms_authorities[nAuth]->DoGetHolidaysInRange(dtStart, dtEnd, hol); |
4f6aed9c VZ |
4216 | |
4217 | WX_APPEND_ARRAY(holidays, hol); | |
4218 | } | |
4219 | ||
4220 | holidays.Sort(wxDateTimeCompareFunc); | |
4221 | ||
df5168c4 | 4222 | return holidays.size(); |
4f6aed9c VZ |
4223 | } |
4224 | ||
4225 | /* static */ | |
4226 | void wxDateTimeHolidayAuthority::ClearAllAuthorities() | |
4227 | { | |
4228 | WX_CLEAR_ARRAY(ms_authorities); | |
4229 | } | |
4230 | ||
4231 | /* static */ | |
4232 | void wxDateTimeHolidayAuthority::AddAuthority(wxDateTimeHolidayAuthority *auth) | |
4233 | { | |
df5168c4 | 4234 | ms_authorities.push_back(auth); |
4f6aed9c VZ |
4235 | } |
4236 | ||
5e233068 WS |
4237 | wxDateTimeHolidayAuthority::~wxDateTimeHolidayAuthority() |
4238 | { | |
4239 | // required here for Darwin | |
4240 | } | |
4241 | ||
4f6aed9c VZ |
4242 | // ---------------------------------------------------------------------------- |
4243 | // wxDateTimeWorkDays | |
4244 | // ---------------------------------------------------------------------------- | |
4245 | ||
4246 | bool wxDateTimeWorkDays::DoIsHoliday(const wxDateTime& dt) const | |
4247 | { | |
4248 | wxDateTime::WeekDay wd = dt.GetWeekDay(); | |
4249 | ||
4250 | return (wd == wxDateTime::Sun) || (wd == wxDateTime::Sat); | |
4251 | } | |
4252 | ||
4253 | size_t wxDateTimeWorkDays::DoGetHolidaysInRange(const wxDateTime& dtStart, | |
4254 | const wxDateTime& dtEnd, | |
4255 | wxDateTimeArray& holidays) const | |
4256 | { | |
4257 | if ( dtStart > dtEnd ) | |
4258 | { | |
4259 | wxFAIL_MSG( _T("invalid date range in GetHolidaysInRange") ); | |
4260 | ||
4261 | return 0u; | |
4262 | } | |
4263 | ||
4264 | holidays.Empty(); | |
4265 | ||
4266 | // instead of checking all days, start with the first Sat after dtStart and | |
4267 | // end with the last Sun before dtEnd | |
4268 | wxDateTime dtSatFirst = dtStart.GetNextWeekDay(wxDateTime::Sat), | |
4269 | dtSatLast = dtEnd.GetPrevWeekDay(wxDateTime::Sat), | |
4270 | dtSunFirst = dtStart.GetNextWeekDay(wxDateTime::Sun), | |
4271 | dtSunLast = dtEnd.GetPrevWeekDay(wxDateTime::Sun), | |
4272 | dt; | |
4273 | ||
4274 | for ( dt = dtSatFirst; dt <= dtSatLast; dt += wxDateSpan::Week() ) | |
4275 | { | |
4276 | holidays.Add(dt); | |
4277 | } | |
4278 | ||
4279 | for ( dt = dtSunFirst; dt <= dtSunLast; dt += wxDateSpan::Week() ) | |
4280 | { | |
4281 | holidays.Add(dt); | |
4282 | } | |
4283 | ||
4284 | return holidays.GetCount(); | |
4285 | } | |
3e0b743f | 4286 | |
26364344 WS |
4287 | // ============================================================================ |
4288 | // other helper functions | |
4289 | // ============================================================================ | |
4290 | ||
4291 | // ---------------------------------------------------------------------------- | |
4292 | // iteration helpers: can be used to write a for loop over enum variable like | |
4293 | // this: | |
4294 | // for ( m = wxDateTime::Jan; m < wxDateTime::Inv_Month; wxNextMonth(m) ) | |
4295 | // ---------------------------------------------------------------------------- | |
4296 | ||
4297 | WXDLLIMPEXP_BASE void wxNextMonth(wxDateTime::Month& m) | |
4298 | { | |
4299 | wxASSERT_MSG( m < wxDateTime::Inv_Month, _T("invalid month") ); | |
4300 | ||
4301 | // no wrapping or the for loop above would never end! | |
4302 | m = (wxDateTime::Month)(m + 1); | |
4303 | } | |
4304 | ||
4305 | WXDLLIMPEXP_BASE void wxPrevMonth(wxDateTime::Month& m) | |
4306 | { | |
4307 | wxASSERT_MSG( m < wxDateTime::Inv_Month, _T("invalid month") ); | |
4308 | ||
4309 | m = m == wxDateTime::Jan ? wxDateTime::Inv_Month | |
4310 | : (wxDateTime::Month)(m - 1); | |
4311 | } | |
4312 | ||
4313 | WXDLLIMPEXP_BASE void wxNextWDay(wxDateTime::WeekDay& wd) | |
4314 | { | |
4315 | wxASSERT_MSG( wd < wxDateTime::Inv_WeekDay, _T("invalid week day") ); | |
4316 | ||
4317 | // no wrapping or the for loop above would never end! | |
4318 | wd = (wxDateTime::WeekDay)(wd + 1); | |
4319 | } | |
4320 | ||
4321 | WXDLLIMPEXP_BASE void wxPrevWDay(wxDateTime::WeekDay& wd) | |
4322 | { | |
4323 | wxASSERT_MSG( wd < wxDateTime::Inv_WeekDay, _T("invalid week day") ); | |
4324 | ||
4325 | wd = wd == wxDateTime::Sun ? wxDateTime::Inv_WeekDay | |
4326 | : (wxDateTime::WeekDay)(wd - 1); | |
4327 | } | |
4328 | ||
1e6feb95 | 4329 | #endif // wxUSE_DATETIME |