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