]>
Commit | Line | Data |
---|---|---|
1ef54dcf | 1 | /////////////////////////////////////////////////////////////////////////////// |
96531a71 | 2 | // Name: src/common/datetime.cpp |
0979c962 | 3 | // Purpose: implementation of time/date related classes |
98919134 | 4 | // (for formatting&parsing see datetimefmt.cpp) |
0979c962 VZ |
5 | // Author: Vadim Zeitlin |
6 | // Modified by: | |
7 | // Created: 11.05.99 | |
8 | // RCS-ID: $Id$ | |
1ef54dcf VZ |
9 | // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> |
10 | // parts of code taken from sndcal library by Scott E. Lee: | |
11 | // | |
12 | // Copyright 1993-1995, Scott E. Lee, all rights reserved. | |
13 | // Permission granted to use, copy, modify, distribute and sell | |
14 | // so long as the above copyright and this permission statement | |
15 | // are retained in all copies. | |
16 | // | |
65571936 | 17 | // Licence: wxWindows licence |
1ef54dcf VZ |
18 | /////////////////////////////////////////////////////////////////////////////// |
19 | ||
20 | /* | |
21 | * Implementation notes: | |
22 | * | |
23 | * 1. the time is stored as a 64bit integer containing the signed number of | |
299fcbfe VZ |
24 | * milliseconds since Jan 1. 1970 (the Unix Epoch) - so it is always |
25 | * expressed in GMT. | |
1ef54dcf VZ |
26 | * |
27 | * 2. the range is thus something about 580 million years, but due to current | |
28 | * algorithms limitations, only dates from Nov 24, 4714BC are handled | |
29 | * | |
30 | * 3. standard ANSI C functions are used to do time calculations whenever | |
31 | * possible, i.e. when the date is in the range Jan 1, 1970 to 2038 | |
32 | * | |
33 | * 4. otherwise, the calculations are done by converting the date to/from JDN | |
34 | * first (the range limitation mentioned above comes from here: the | |
35 | * algorithm used by Scott E. Lee's code only works for positive JDNs, more | |
36 | * or less) | |
37 | * | |
299fcbfe VZ |
38 | * 5. the object constructed for the given DD-MM-YYYY HH:MM:SS corresponds to |
39 | * this moment in local time and may be converted to the object | |
40 | * corresponding to the same date/time in another time zone by using | |
41 | * ToTimezone() | |
42 | * | |
43 | * 6. the conversions to the current (or any other) timezone are done when the | |
44 | * internal time representation is converted to the broken-down one in | |
45 | * wxDateTime::Tm. | |
1ef54dcf | 46 | */ |
0979c962 VZ |
47 | |
48 | // ============================================================================ | |
49 | // declarations | |
50 | // ============================================================================ | |
51 | ||
52 | // ---------------------------------------------------------------------------- | |
53 | // headers | |
54 | // ---------------------------------------------------------------------------- | |
55 | ||
0979c962 VZ |
56 | // For compilers that support precompilation, includes "wx.h". |
57 | #include "wx/wxprec.h" | |
58 | ||
59 | #ifdef __BORLANDC__ | |
60 | #pragma hdrstop | |
61 | #endif | |
62 | ||
1e6feb95 VZ |
63 | #if !defined(wxUSE_DATETIME) || wxUSE_DATETIME |
64 | ||
0979c962 | 65 | #ifndef WX_PRECOMP |
57bd4c60 WS |
66 | #ifdef __WXMSW__ |
67 | #include "wx/msw/wrapwin.h" | |
68 | #endif | |
0979c962 | 69 | #include "wx/string.h" |
0979c962 | 70 | #include "wx/log.h" |
88a7a4e1 | 71 | #include "wx/intl.h" |
bb90a3e6 | 72 | #include "wx/stopwatch.h" // for wxGetLocalTimeMillis() |
02761f6c | 73 | #include "wx/module.h" |
0bf751e7 | 74 | #include "wx/crt.h" |
0979c962 VZ |
75 | #endif // WX_PRECOMP |
76 | ||
fcc3d7cb | 77 | #include "wx/thread.h" |
cd0b1709 | 78 | #include "wx/tokenzr.h" |
fcc3d7cb | 79 | |
ff0ea71c GT |
80 | #include <ctype.h> |
81 | ||
64f9c100 | 82 | #ifdef __WINDOWS__ |
64f9c100 | 83 | #include <winnls.h> |
74b99d0f WS |
84 | #ifndef __WXWINCE__ |
85 | #include <locale.h> | |
86 | #endif | |
64f9c100 VS |
87 | #endif |
88 | ||
0979c962 VZ |
89 | #include "wx/datetime.h" |
90 | ||
48fd6e9d FM |
91 | // ---------------------------------------------------------------------------- |
92 | // wxXTI | |
93 | // ---------------------------------------------------------------------------- | |
bd4a25bc | 94 | |
cb73e600 SC |
95 | #if wxUSE_EXTENDED_RTTI |
96 | ||
97 | template<> void wxStringReadValue(const wxString &s , wxDateTime &data ) | |
98 | { | |
1fcca711 | 99 | data.ParseFormat(s,"%Y-%m-%d %H:%M:%S", NULL); |
cb73e600 SC |
100 | } |
101 | ||
102 | template<> void wxStringWriteValue(wxString &s , const wxDateTime &data ) | |
103 | { | |
c20d5805 | 104 | s = data.Format("%Y-%m-%d %H:%M:%S"); |
cb73e600 SC |
105 | } |
106 | ||
8805dbab | 107 | wxCUSTOM_TYPE_INFO(wxDateTime, wxToStringConverter<wxDateTime> , wxFromStringConverter<wxDateTime>) |
cb73e600 | 108 | |
c20d5805 | 109 | #endif // wxUSE_EXTENDED_RTTI |
cb73e600 | 110 | |
48fd6e9d | 111 | |
f0f951fa VZ |
112 | // ---------------------------------------------------------------------------- |
113 | // conditional compilation | |
114 | // ---------------------------------------------------------------------------- | |
6de20863 | 115 | |
31907d03 SC |
116 | #if defined(__MWERKS__) && wxUSE_UNICODE |
117 | #include <wtime.h> | |
118 | #endif | |
119 | ||
8c3aa3ff VZ |
120 | #if defined(__DJGPP__) || defined(__WINE__) |
121 | #include <sys/timeb.h> | |
122 | #include <values.h> | |
123 | #endif | |
124 | ||
125 | #ifndef WX_GMTOFF_IN_TM | |
126 | // Define it for some systems which don't (always) use configure but are | |
127 | // known to have tm_gmtoff field. | |
128 | #if defined(__WXPALMOS__) || defined(__DARWIN__) | |
0779cc9a | 129 | #define WX_GMTOFF_IN_TM |
f0f951fa | 130 | #endif |
8c3aa3ff | 131 | #endif |
c25a510b | 132 | |
e09080ec VZ |
133 | // NB: VC8 safe time functions could/should be used for wxMSW as well probably |
134 | #if defined(__WXWINCE__) && defined(__VISUALC8__) | |
135 | ||
136 | struct tm *wxLocaltime_r(const time_t *t, struct tm* tm) | |
137 | { | |
138 | __time64_t t64 = *t; | |
139 | return _localtime64_s(tm, &t64) == 0 ? tm : NULL; | |
140 | } | |
141 | ||
142 | struct tm *wxGmtime_r(const time_t* t, struct tm* tm) | |
143 | { | |
144 | __time64_t t64 = *t; | |
145 | return _gmtime64_s(tm, &t64) == 0 ? tm : NULL; | |
146 | } | |
147 | ||
148 | #else // !wxWinCE with VC8 | |
149 | ||
5b52a684 | 150 | #if (!defined(HAVE_LOCALTIME_R) || !defined(HAVE_GMTIME_R)) && wxUSE_THREADS && !defined(__WINDOWS__) |
cf44a61c SN |
151 | static wxMutex timeLock; |
152 | #endif | |
153 | ||
154 | #ifndef HAVE_LOCALTIME_R | |
155 | struct tm *wxLocaltime_r(const time_t* ticks, struct tm* temp) | |
156 | { | |
157 | #if wxUSE_THREADS && !defined(__WINDOWS__) | |
158 | // No need to waste time with a mutex on windows since it's using | |
159 | // thread local storage for localtime anyway. | |
6f721331 | 160 | wxMutexLocker locker(timeLock); |
cf44a61c | 161 | #endif |
55809d13 VZ |
162 | |
163 | // Borland CRT crashes when passed 0 ticks for some reason, see SF bug 1704438 | |
164 | #ifdef __BORLANDC__ | |
165 | if ( !*ticks ) | |
166 | return NULL; | |
167 | #endif | |
168 | ||
169 | const tm * const t = localtime(ticks); | |
170 | if ( !t ) | |
171 | return NULL; | |
172 | ||
173 | memcpy(temp, t, sizeof(struct tm)); | |
cf44a61c SN |
174 | return temp; |
175 | } | |
e09080ec | 176 | #endif // !HAVE_LOCALTIME_R |
cf44a61c SN |
177 | |
178 | #ifndef HAVE_GMTIME_R | |
179 | struct tm *wxGmtime_r(const time_t* ticks, struct tm* temp) | |
180 | { | |
181 | #if wxUSE_THREADS && !defined(__WINDOWS__) | |
182 | // No need to waste time with a mutex on windows since it's | |
183 | // using thread local storage for gmtime anyway. | |
6f721331 | 184 | wxMutexLocker locker(timeLock); |
cf44a61c | 185 | #endif |
55809d13 VZ |
186 | |
187 | #ifdef __BORLANDC__ | |
188 | if ( !*ticks ) | |
189 | return NULL; | |
190 | #endif | |
191 | ||
192 | const tm * const t = gmtime(ticks); | |
193 | if ( !t ) | |
194 | return NULL; | |
195 | ||
cf44a61c SN |
196 | memcpy(temp, gmtime(ticks), sizeof(struct tm)); |
197 | return temp; | |
198 | } | |
e09080ec VZ |
199 | #endif // !HAVE_GMTIME_R |
200 | ||
201 | #endif // wxWinCE with VC8/other platforms | |
cf44a61c | 202 | |
384223b3 VZ |
203 | // ---------------------------------------------------------------------------- |
204 | // macros | |
205 | // ---------------------------------------------------------------------------- | |
206 | ||
207 | // debugging helper: just a convenient replacement of wxCHECK() | |
637b7e4f VZ |
208 | #define wxDATETIME_CHECK(expr, msg) \ |
209 | wxCHECK2_MSG(expr, *this = wxInvalidDateTime; return *this, msg) | |
384223b3 | 210 | |
4f6aed9c VZ |
211 | // ---------------------------------------------------------------------------- |
212 | // private classes | |
213 | // ---------------------------------------------------------------------------- | |
214 | ||
215 | class wxDateTimeHolidaysModule : public wxModule | |
216 | { | |
217 | public: | |
218 | virtual bool OnInit() | |
219 | { | |
220 | wxDateTimeHolidayAuthority::AddAuthority(new wxDateTimeWorkDays); | |
221 | ||
68379eaf | 222 | return true; |
4f6aed9c VZ |
223 | } |
224 | ||
225 | virtual void OnExit() | |
226 | { | |
227 | wxDateTimeHolidayAuthority::ClearAllAuthorities(); | |
df5168c4 | 228 | wxDateTimeHolidayAuthority::ms_authorities.clear(); |
4f6aed9c VZ |
229 | } |
230 | ||
231 | private: | |
232 | DECLARE_DYNAMIC_CLASS(wxDateTimeHolidaysModule) | |
233 | }; | |
234 | ||
235 | IMPLEMENT_DYNAMIC_CLASS(wxDateTimeHolidaysModule, wxModule) | |
236 | ||
b76b015e VZ |
237 | // ---------------------------------------------------------------------------- |
238 | // constants | |
239 | // ---------------------------------------------------------------------------- | |
240 | ||
e6ec579c | 241 | // some trivial ones |
fcc3d7cb VZ |
242 | static const int MONTHS_IN_YEAR = 12; |
243 | ||
df05cdc5 VZ |
244 | static const int SEC_PER_MIN = 60; |
245 | ||
246 | static const int MIN_PER_HOUR = 60; | |
247 | ||
e6ec579c VZ |
248 | static const long SECONDS_PER_DAY = 86400l; |
249 | ||
df05cdc5 VZ |
250 | static const int DAYS_PER_WEEK = 7; |
251 | ||
e6ec579c VZ |
252 | static const long MILLISECONDS_PER_DAY = 86400000l; |
253 | ||
254 | // this is the integral part of JDN of the midnight of Jan 1, 1970 | |
255 | // (i.e. JDN(Jan 1, 1970) = 2440587.5) | |
cd0b1709 | 256 | static const long EPOCH_JDN = 2440587l; |
b76b015e | 257 | |
4b6a582b VZ |
258 | // these values are only used in asserts so don't define them if asserts are |
259 | // disabled to avoid warnings about unused static variables | |
260 | #if wxDEBUG_LEVEL | |
1ef54dcf VZ |
261 | // the date of JDN -0.5 (as we don't work with fractional parts, this is the |
262 | // reference date for us) is Nov 24, 4714BC | |
263 | static const int JDN_0_YEAR = -4713; | |
264 | static const int JDN_0_MONTH = wxDateTime::Nov; | |
265 | static const int JDN_0_DAY = 24; | |
4b6a582b | 266 | #endif // wxDEBUG_LEVEL |
1ef54dcf VZ |
267 | |
268 | // the constants used for JDN calculations | |
cd0b1709 VZ |
269 | static const long JDN_OFFSET = 32046l; |
270 | static const long DAYS_PER_5_MONTHS = 153l; | |
271 | static const long DAYS_PER_4_YEARS = 1461l; | |
272 | static const long DAYS_PER_400_YEARS = 146097l; | |
1ef54dcf | 273 | |
f0f951fa VZ |
274 | // this array contains the cumulated number of days in all previous months for |
275 | // normal and leap years | |
276 | static const wxDateTime::wxDateTime_t gs_cumulatedDays[2][MONTHS_IN_YEAR] = | |
277 | { | |
278 | { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }, | |
279 | { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 } | |
280 | }; | |
281 | ||
48fd6e9d FM |
282 | const long wxDateTime::TIME_T_FACTOR = 1000l; |
283 | ||
fcc3d7cb | 284 | // ---------------------------------------------------------------------------- |
2ef31e80 VZ |
285 | // global data |
286 | // ---------------------------------------------------------------------------- | |
287 | ||
a243da29 PC |
288 | const char wxDefaultDateTimeFormat[] = "%c"; |
289 | const char wxDefaultTimeSpanFormat[] = "%H:%M:%S"; | |
1aaf88d2 | 290 | |
384223b3 VZ |
291 | // in the fine tradition of ANSI C we use our equivalent of (time_t)-1 to |
292 | // indicate an invalid wxDateTime object | |
56d0c5a6 | 293 | const wxDateTime wxDefaultDateTime; |
2ef31e80 VZ |
294 | |
295 | wxDateTime::Country wxDateTime::ms_country = wxDateTime::Country_Unknown; | |
296 | ||
b76b015e VZ |
297 | // ---------------------------------------------------------------------------- |
298 | // private functions | |
299 | // ---------------------------------------------------------------------------- | |
300 | ||
4b6a582b VZ |
301 | // debugger helper: this function can be called from a debugger to show what |
302 | // the date really is | |
77fa3d82 | 303 | extern const char *wxDumpDate(const wxDateTime* dt) |
4f6aed9c | 304 | { |
77fa3d82 | 305 | static char buf[128]; |
4f6aed9c | 306 | |
77fa3d82 | 307 | wxString fmt(dt->Format("%Y-%m-%d (%a) %H:%M:%S")); |
b3483429 VZ |
308 | wxStrlcpy(buf, |
309 | (fmt + " (" + dt->GetValue().ToString() + " ticks)").ToAscii(), | |
77fa3d82 | 310 | WXSIZEOF(buf)); |
4f6aed9c VZ |
311 | |
312 | return buf; | |
313 | } | |
4f6aed9c | 314 | |
fcc3d7cb | 315 | // get the number of days in the given month of the given year |
c4e08560 | 316 | static inline |
fcc3d7cb VZ |
317 | wxDateTime::wxDateTime_t GetNumOfDaysInMonth(int year, wxDateTime::Month month) |
318 | { | |
e6ec579c VZ |
319 | // the number of days in month in Julian/Gregorian calendar: the first line |
320 | // is for normal years, the second one is for the leap ones | |
a243da29 | 321 | static const wxDateTime::wxDateTime_t daysInMonth[2][MONTHS_IN_YEAR] = |
e6ec579c VZ |
322 | { |
323 | { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, | |
324 | { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } | |
325 | }; | |
326 | ||
327 | return daysInMonth[wxDateTime::IsLeapYear(year)][month]; | |
fcc3d7cb VZ |
328 | } |
329 | ||
c5432233 VZ |
330 | // returns the time zone in the C sense, i.e. the difference UTC - local |
331 | // (in seconds) | |
1ee2f9d9 FM |
332 | // NOTE: not static because used by datetimefmt.cpp |
333 | int GetTimeZone() | |
fcc3d7cb | 334 | { |
5e3566ff | 335 | #ifdef WX_GMTOFF_IN_TM |
68379eaf WS |
336 | // set to true when the timezone is set |
337 | static bool s_timezoneSet = false; | |
0779cc9a | 338 | static long gmtoffset = LONG_MAX; // invalid timezone |
c5432233 | 339 | |
a452689b | 340 | // ensure that the timezone variable is set by calling wxLocaltime_r |
fcc3d7cb VZ |
341 | if ( !s_timezoneSet ) |
342 | { | |
a452689b SN |
343 | // just call wxLocaltime_r() instead of figuring out whether this |
344 | // system supports tzset(), _tzset() or something else | |
c5432233 | 345 | time_t t = 0; |
3d561414 | 346 | struct tm tm; |
fcc3d7cb | 347 | |
3d561414 | 348 | wxLocaltime_r(&t, &tm); |
68379eaf | 349 | s_timezoneSet = true; |
c5432233 | 350 | |
c5432233 VZ |
351 | // note that GMT offset is the opposite of time zone and so to return |
352 | // consistent results in both WX_GMTOFF_IN_TM and !WX_GMTOFF_IN_TM | |
353 | // cases we have to negate it | |
3d561414 | 354 | gmtoffset = -tm.tm_gmtoff; |
fcc3d7cb | 355 | } |
0779cc9a | 356 | return (int)gmtoffset; |
8c3aa3ff VZ |
357 | #elif defined(__DJGPP__) || defined(__WINE__) |
358 | struct timeb tb; | |
359 | ftime(&tb); | |
360 | return tb.timezone*60; | |
361 | #elif defined(__VISUALC__) | |
362 | // We must initialize the time zone information before using it (this will | |
363 | // be done only once internally). | |
364 | _tzset(); | |
365 | ||
366 | // Starting with VC++ 8 timezone variable is deprecated and is not even | |
367 | // available in some standard library version so use the new function for | |
368 | // accessing it instead. | |
369 | #if wxCHECK_VISUALC_VERSION(8) | |
370 | long t; | |
371 | _get_timezone(&t); | |
372 | return t; | |
373 | #else // VC++ < 8 | |
374 | return timezone; | |
375 | #endif | |
376 | #elif defined(WX_TIMEZONE) // If WX_TIMEZONE was defined by configure, use it. | |
5e3566ff | 377 | return WX_TIMEZONE; |
8c3aa3ff VZ |
378 | #elif defined(__BORLANDC__) || defined(__MINGW32__) || defined(__VISAGECPP__) |
379 | return _timezone; | |
380 | #elif defined(__MWERKS__) | |
381 | return 28800; | |
382 | #else // unknown platform -- assume it has timezone | |
383 | return timezone; | |
5e3566ff | 384 | #endif // WX_GMTOFF_IN_TM/!WX_GMTOFF_IN_TM |
fcc3d7cb VZ |
385 | } |
386 | ||
e6ec579c | 387 | // return the integral part of the JDN for the midnight of the given date (to |
1ef54dcf VZ |
388 | // get the real JDN you need to add 0.5, this is, in fact, JDN of the |
389 | // noon of the previous day) | |
e6ec579c VZ |
390 | static long GetTruncatedJDN(wxDateTime::wxDateTime_t day, |
391 | wxDateTime::Month mon, | |
392 | int year) | |
393 | { | |
1ef54dcf VZ |
394 | // CREDIT: code below is by Scott E. Lee (but bugs are mine) |
395 | ||
396 | // check the date validity | |
397 | wxASSERT_MSG( | |
398 | (year > JDN_0_YEAR) || | |
399 | ((year == JDN_0_YEAR) && (mon > JDN_0_MONTH)) || | |
400 | ((year == JDN_0_YEAR) && (mon == JDN_0_MONTH) && (day >= JDN_0_DAY)), | |
9a83f860 | 401 | wxT("date out of range - can't convert to JDN") |
1ef54dcf VZ |
402 | ); |
403 | ||
404 | // make the year positive to avoid problems with negative numbers division | |
405 | year += 4800; | |
406 | ||
407 | // months are counted from March here | |
408 | int month; | |
409 | if ( mon >= wxDateTime::Mar ) | |
e6ec579c | 410 | { |
1ef54dcf | 411 | month = mon - 2; |
e6ec579c | 412 | } |
1ef54dcf | 413 | else |
e6ec579c | 414 | { |
1ef54dcf VZ |
415 | month = mon + 10; |
416 | year--; | |
417 | } | |
e6ec579c | 418 | |
1ef54dcf VZ |
419 | // now we can simply add all the contributions together |
420 | return ((year / 100) * DAYS_PER_400_YEARS) / 4 | |
421 | + ((year % 100) * DAYS_PER_4_YEARS) / 4 | |
422 | + (month * DAYS_PER_5_MONTHS + 2) / 5 | |
423 | + day | |
424 | - JDN_OFFSET; | |
e6ec579c VZ |
425 | } |
426 | ||
dd36b5a3 | 427 | #ifdef wxHAS_STRFTIME |
bb7afa49 | 428 | |
2423ef22 | 429 | // this function is a wrapper around strftime(3) adding error checking |
1ee2f9d9 FM |
430 | // NOTE: not static because used by datetimefmt.cpp |
431 | wxString CallStrftime(const wxString& format, const tm* tm) | |
b76b015e | 432 | { |
68ee7c47 | 433 | wxChar buf[4096]; |
594588aa MW |
434 | // Create temp wxString here to work around mingw/cygwin bug 1046059 |
435 | // http://sourceforge.net/tracker/?func=detail&atid=102435&aid=1046059&group_id=2435 | |
436 | wxString s; | |
437 | ||
ba9bbf13 | 438 | if ( !wxStrftime(buf, WXSIZEOF(buf), format, tm) ) |
b76b015e | 439 | { |
adec277f | 440 | // if the format is valid, buffer must be too small? |
9a83f860 | 441 | wxFAIL_MSG(wxT("strftime() failed")); |
adec277f VZ |
442 | |
443 | buf[0] = '\0'; | |
b76b015e VZ |
444 | } |
445 | ||
594588aa MW |
446 | s = buf; |
447 | return s; | |
b76b015e | 448 | } |
bb7afa49 | 449 | |
dd36b5a3 | 450 | #endif // wxHAS_STRFTIME |
b76b015e | 451 | |
2f02cb89 VZ |
452 | // if year and/or month have invalid values, replace them with the current ones |
453 | static void ReplaceDefaultYearMonthWithCurrent(int *year, | |
454 | wxDateTime::Month *month) | |
455 | { | |
456 | struct tm *tmNow = NULL; | |
a452689b | 457 | struct tm tmstruct; |
2f02cb89 VZ |
458 | |
459 | if ( *year == wxDateTime::Inv_Year ) | |
460 | { | |
a452689b | 461 | tmNow = wxDateTime::GetTmNow(&tmstruct); |
2f02cb89 VZ |
462 | |
463 | *year = 1900 + tmNow->tm_year; | |
464 | } | |
465 | ||
466 | if ( *month == wxDateTime::Inv_Month ) | |
467 | { | |
468 | if ( !tmNow ) | |
a452689b | 469 | tmNow = wxDateTime::GetTmNow(&tmstruct); |
2f02cb89 VZ |
470 | |
471 | *month = (wxDateTime::Month)tmNow->tm_mon; | |
472 | } | |
473 | } | |
474 | ||
1ee2f9d9 FM |
475 | // fill the struct tm with default values |
476 | // NOTE: not static because used by datetimefmt.cpp | |
477 | void InitTm(struct tm& tm) | |
f0f951fa VZ |
478 | { |
479 | // struct tm may have etxra fields (undocumented and with unportable | |
480 | // names) which, nevertheless, must be set to 0 | |
481 | memset(&tm, 0, sizeof(struct tm)); | |
482 | ||
483 | tm.tm_mday = 1; // mday 0 is invalid | |
484 | tm.tm_year = 76; // any valid year | |
485 | tm.tm_isdst = -1; // auto determine | |
486 | } | |
487 | ||
0979c962 VZ |
488 | // ============================================================================ |
489 | // implementation of wxDateTime | |
490 | // ============================================================================ | |
491 | ||
b76b015e VZ |
492 | // ---------------------------------------------------------------------------- |
493 | // struct Tm | |
494 | // ---------------------------------------------------------------------------- | |
495 | ||
496 | wxDateTime::Tm::Tm() | |
497 | { | |
498 | year = (wxDateTime_t)wxDateTime::Inv_Year; | |
499 | mon = wxDateTime::Inv_Month; | |
fdb44eb2 VZ |
500 | mday = |
501 | yday = 0; | |
502 | hour = | |
503 | min = | |
504 | sec = | |
505 | msec = 0; | |
b76b015e VZ |
506 | wday = wxDateTime::Inv_WeekDay; |
507 | } | |
508 | ||
299fcbfe VZ |
509 | wxDateTime::Tm::Tm(const struct tm& tm, const TimeZone& tz) |
510 | : m_tz(tz) | |
b76b015e | 511 | { |
e6ec579c | 512 | msec = 0; |
907173e5 WS |
513 | sec = (wxDateTime::wxDateTime_t)tm.tm_sec; |
514 | min = (wxDateTime::wxDateTime_t)tm.tm_min; | |
515 | hour = (wxDateTime::wxDateTime_t)tm.tm_hour; | |
516 | mday = (wxDateTime::wxDateTime_t)tm.tm_mday; | |
fcc3d7cb | 517 | mon = (wxDateTime::Month)tm.tm_mon; |
b76b015e | 518 | year = 1900 + tm.tm_year; |
bb8a8478 WS |
519 | wday = (wxDateTime::wxDateTime_t)tm.tm_wday; |
520 | yday = (wxDateTime::wxDateTime_t)tm.tm_yday; | |
b76b015e VZ |
521 | } |
522 | ||
523 | bool wxDateTime::Tm::IsValid() const | |
524 | { | |
8b3eb4a0 VZ |
525 | if ( mon == wxDateTime::Inv_Month ) |
526 | return false; | |
527 | ||
528 | // We need to check this here to avoid crashing in GetNumOfDaysInMonth() if | |
529 | // somebody passed us "(wxDateTime::Month)1000". | |
530 | wxCHECK_MSG( mon >= wxDateTime::Jan && mon < wxDateTime::Inv_Month, false, | |
531 | wxS("Invalid month value") ); | |
532 | ||
b76b015e | 533 | // we allow for the leap seconds, although we don't use them (yet) |
fcc3d7cb | 534 | return (year != wxDateTime::Inv_Year) && (mon != wxDateTime::Inv_Month) && |
8b3eb4a0 | 535 | (mday > 0 && mday <= GetNumOfDaysInMonth(year, mon)) && |
e6ec579c | 536 | (hour < 24) && (min < 60) && (sec < 62) && (msec < 1000); |
b76b015e VZ |
537 | } |
538 | ||
539 | void wxDateTime::Tm::ComputeWeekDay() | |
540 | { | |
c5a1681b VZ |
541 | // compute the week day from day/month/year: we use the dumbest algorithm |
542 | // possible: just compute our JDN and then use the (simple to derive) | |
543 | // formula: weekday = (JDN + 1.5) % 7 | |
5a572f6c | 544 | wday = (wxDateTime::wxDateTime_t)((GetTruncatedJDN(mday, mon, year) + 2) % 7); |
b76b015e VZ |
545 | } |
546 | ||
e6ec579c | 547 | void wxDateTime::Tm::AddMonths(int monDiff) |
fcc3d7cb VZ |
548 | { |
549 | // normalize the months field | |
550 | while ( monDiff < -mon ) | |
551 | { | |
552 | year--; | |
553 | ||
554 | monDiff += MONTHS_IN_YEAR; | |
555 | } | |
556 | ||
2ef31e80 | 557 | while ( monDiff + mon >= MONTHS_IN_YEAR ) |
fcc3d7cb VZ |
558 | { |
559 | year++; | |
239446b4 VZ |
560 | |
561 | monDiff -= MONTHS_IN_YEAR; | |
fcc3d7cb VZ |
562 | } |
563 | ||
564 | mon = (wxDateTime::Month)(mon + monDiff); | |
565 | ||
9a83f860 | 566 | wxASSERT_MSG( mon >= 0 && mon < MONTHS_IN_YEAR, wxT("logic error") ); |
4f6aed9c VZ |
567 | |
568 | // NB: we don't check here that the resulting date is valid, this function | |
569 | // is private and the caller must check it if needed | |
fcc3d7cb VZ |
570 | } |
571 | ||
e6ec579c | 572 | void wxDateTime::Tm::AddDays(int dayDiff) |
fcc3d7cb VZ |
573 | { |
574 | // normalize the days field | |
9d9b7755 | 575 | while ( dayDiff + mday < 1 ) |
fcc3d7cb VZ |
576 | { |
577 | AddMonths(-1); | |
578 | ||
9d9b7755 | 579 | dayDiff += GetNumOfDaysInMonth(year, mon); |
fcc3d7cb VZ |
580 | } |
581 | ||
907173e5 | 582 | mday = (wxDateTime::wxDateTime_t)( mday + dayDiff ); |
fcc3d7cb VZ |
583 | while ( mday > GetNumOfDaysInMonth(year, mon) ) |
584 | { | |
585 | mday -= GetNumOfDaysInMonth(year, mon); | |
586 | ||
587 | AddMonths(1); | |
588 | } | |
589 | ||
590 | wxASSERT_MSG( mday > 0 && mday <= GetNumOfDaysInMonth(year, mon), | |
9a83f860 | 591 | wxT("logic error") ); |
fcc3d7cb VZ |
592 | } |
593 | ||
594 | // ---------------------------------------------------------------------------- | |
595 | // class TimeZone | |
596 | // ---------------------------------------------------------------------------- | |
597 | ||
598 | wxDateTime::TimeZone::TimeZone(wxDateTime::TZ tz) | |
599 | { | |
600 | switch ( tz ) | |
601 | { | |
602 | case wxDateTime::Local: | |
299fcbfe VZ |
603 | // get the offset from C RTL: it returns the difference GMT-local |
604 | // while we want to have the offset _from_ GMT, hence the '-' | |
605 | m_offset = -GetTimeZone(); | |
fcc3d7cb VZ |
606 | break; |
607 | ||
608 | case wxDateTime::GMT_12: | |
609 | case wxDateTime::GMT_11: | |
610 | case wxDateTime::GMT_10: | |
611 | case wxDateTime::GMT_9: | |
612 | case wxDateTime::GMT_8: | |
613 | case wxDateTime::GMT_7: | |
614 | case wxDateTime::GMT_6: | |
615 | case wxDateTime::GMT_5: | |
616 | case wxDateTime::GMT_4: | |
617 | case wxDateTime::GMT_3: | |
618 | case wxDateTime::GMT_2: | |
619 | case wxDateTime::GMT_1: | |
299fcbfe | 620 | m_offset = -3600*(wxDateTime::GMT0 - tz); |
fcc3d7cb VZ |
621 | break; |
622 | ||
623 | case wxDateTime::GMT0: | |
624 | case wxDateTime::GMT1: | |
625 | case wxDateTime::GMT2: | |
626 | case wxDateTime::GMT3: | |
627 | case wxDateTime::GMT4: | |
628 | case wxDateTime::GMT5: | |
629 | case wxDateTime::GMT6: | |
630 | case wxDateTime::GMT7: | |
631 | case wxDateTime::GMT8: | |
632 | case wxDateTime::GMT9: | |
633 | case wxDateTime::GMT10: | |
634 | case wxDateTime::GMT11: | |
635 | case wxDateTime::GMT12: | |
34f90a1c | 636 | case wxDateTime::GMT13: |
299fcbfe | 637 | m_offset = 3600*(tz - wxDateTime::GMT0); |
fcc3d7cb VZ |
638 | break; |
639 | ||
640 | case wxDateTime::A_CST: | |
641 | // Central Standard Time in use in Australia = UTC + 9.5 | |
d26adb9d | 642 | m_offset = 60l*(9*MIN_PER_HOUR + MIN_PER_HOUR/2); |
fcc3d7cb VZ |
643 | break; |
644 | ||
645 | default: | |
9a83f860 | 646 | wxFAIL_MSG( wxT("unknown time zone") ); |
fcc3d7cb VZ |
647 | } |
648 | } | |
649 | ||
b76b015e VZ |
650 | // ---------------------------------------------------------------------------- |
651 | // static functions | |
652 | // ---------------------------------------------------------------------------- | |
653 | ||
1ee2f9d9 FM |
654 | /* static */ |
655 | struct tm *wxDateTime::GetTmNow(struct tm *tmstruct) | |
656 | { | |
657 | time_t t = GetTimeNow(); | |
658 | return wxLocaltime_r(&t, tmstruct); | |
659 | } | |
660 | ||
b76b015e VZ |
661 | /* static */ |
662 | bool wxDateTime::IsLeapYear(int year, wxDateTime::Calendar cal) | |
663 | { | |
2f02cb89 VZ |
664 | if ( year == Inv_Year ) |
665 | year = GetCurrentYear(); | |
666 | ||
b76b015e VZ |
667 | if ( cal == Gregorian ) |
668 | { | |
669 | // in Gregorian calendar leap years are those divisible by 4 except | |
670 | // those divisible by 100 unless they're also divisible by 400 | |
671 | // (in some countries, like Russia and Greece, additional corrections | |
672 | // exist, but they won't manifest themselves until 2700) | |
673 | return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)); | |
674 | } | |
675 | else if ( cal == Julian ) | |
676 | { | |
677 | // in Julian calendar the rule is simpler | |
678 | return year % 4 == 0; | |
679 | } | |
680 | else | |
681 | { | |
9a83f860 | 682 | wxFAIL_MSG(wxT("unknown calendar")); |
b76b015e | 683 | |
68379eaf | 684 | return false; |
b76b015e VZ |
685 | } |
686 | } | |
687 | ||
fcc3d7cb VZ |
688 | /* static */ |
689 | int wxDateTime::GetCentury(int year) | |
690 | { | |
691 | return year > 0 ? year / 100 : year / 100 - 1; | |
692 | } | |
693 | ||
b76b015e VZ |
694 | /* static */ |
695 | int wxDateTime::ConvertYearToBC(int year) | |
696 | { | |
697 | // year 0 is BC 1 | |
698 | return year > 0 ? year : year - 1; | |
699 | } | |
700 | ||
701 | /* static */ | |
702 | int wxDateTime::GetCurrentYear(wxDateTime::Calendar cal) | |
703 | { | |
704 | switch ( cal ) | |
705 | { | |
706 | case Gregorian: | |
707 | return Now().GetYear(); | |
708 | ||
709 | case Julian: | |
9a83f860 | 710 | wxFAIL_MSG(wxT("TODO")); |
b76b015e VZ |
711 | break; |
712 | ||
713 | default: | |
9a83f860 | 714 | wxFAIL_MSG(wxT("unsupported calendar")); |
b76b015e VZ |
715 | break; |
716 | } | |
717 | ||
718 | return Inv_Year; | |
719 | } | |
720 | ||
721 | /* static */ | |
722 | wxDateTime::Month wxDateTime::GetCurrentMonth(wxDateTime::Calendar cal) | |
723 | { | |
724 | switch ( cal ) | |
725 | { | |
726 | case Gregorian: | |
727 | return Now().GetMonth(); | |
b76b015e VZ |
728 | |
729 | case Julian: | |
9a83f860 | 730 | wxFAIL_MSG(wxT("TODO")); |
b76b015e VZ |
731 | break; |
732 | ||
733 | default: | |
9a83f860 | 734 | wxFAIL_MSG(wxT("unsupported calendar")); |
b76b015e VZ |
735 | break; |
736 | } | |
737 | ||
738 | return Inv_Month; | |
739 | } | |
740 | ||
2f02cb89 VZ |
741 | /* static */ |
742 | wxDateTime::wxDateTime_t wxDateTime::GetNumberOfDays(int year, Calendar cal) | |
743 | { | |
744 | if ( year == Inv_Year ) | |
745 | { | |
746 | // take the current year if none given | |
747 | year = GetCurrentYear(); | |
748 | } | |
749 | ||
750 | switch ( cal ) | |
751 | { | |
752 | case Gregorian: | |
753 | case Julian: | |
754 | return IsLeapYear(year) ? 366 : 365; | |
2f02cb89 VZ |
755 | |
756 | default: | |
9a83f860 | 757 | wxFAIL_MSG(wxT("unsupported calendar")); |
2f02cb89 VZ |
758 | break; |
759 | } | |
760 | ||
761 | return 0; | |
762 | } | |
763 | ||
b76b015e VZ |
764 | /* static */ |
765 | wxDateTime::wxDateTime_t wxDateTime::GetNumberOfDays(wxDateTime::Month month, | |
766 | int year, | |
767 | wxDateTime::Calendar cal) | |
768 | { | |
9a83f860 | 769 | wxCHECK_MSG( month < MONTHS_IN_YEAR, 0, wxT("invalid month") ); |
b76b015e VZ |
770 | |
771 | if ( cal == Gregorian || cal == Julian ) | |
772 | { | |
773 | if ( year == Inv_Year ) | |
774 | { | |
775 | // take the current year if none given | |
776 | year = GetCurrentYear(); | |
777 | } | |
778 | ||
fcc3d7cb | 779 | return GetNumOfDaysInMonth(year, month); |
b76b015e VZ |
780 | } |
781 | else | |
782 | { | |
9a83f860 | 783 | wxFAIL_MSG(wxT("unsupported calendar")); |
b76b015e VZ |
784 | |
785 | return 0; | |
786 | } | |
787 | } | |
788 | ||
e538985e VZ |
789 | namespace |
790 | { | |
791 | ||
792 | // helper function used by GetEnglish/WeekDayName(): returns 0 if flags is | |
793 | // Name_Full and 1 if it is Name_Abbr or -1 if the flags is incorrect (and | |
794 | // asserts in this case) | |
795 | // | |
796 | // the return value of this function is used as an index into 2D array | |
797 | // containing full names in its first row and abbreviated ones in the 2nd one | |
798 | int NameArrayIndexFromFlag(wxDateTime::NameFlags flags) | |
799 | { | |
800 | switch ( flags ) | |
801 | { | |
802 | case wxDateTime::Name_Full: | |
803 | return 0; | |
804 | ||
805 | case wxDateTime::Name_Abbr: | |
806 | return 1; | |
807 | ||
808 | default: | |
809 | wxFAIL_MSG( "unknown wxDateTime::NameFlags value" ); | |
810 | } | |
811 | ||
812 | return -1; | |
813 | } | |
814 | ||
815 | } // anonymous namespace | |
816 | ||
817 | /* static */ | |
818 | wxString wxDateTime::GetEnglishMonthName(Month month, NameFlags flags) | |
819 | { | |
820 | wxCHECK_MSG( month != Inv_Month, wxEmptyString, "invalid month" ); | |
821 | ||
a243da29 | 822 | static const char *const monthNames[2][MONTHS_IN_YEAR] = |
e538985e VZ |
823 | { |
824 | { "January", "February", "March", "April", "May", "June", | |
825 | "July", "August", "September", "October", "November", "December" }, | |
826 | { "Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
827 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" } | |
828 | }; | |
829 | ||
830 | const int idx = NameArrayIndexFromFlag(flags); | |
831 | if ( idx == -1 ) | |
832 | return wxString(); | |
833 | ||
834 | return monthNames[idx][month]; | |
835 | } | |
836 | ||
b76b015e | 837 | /* static */ |
f0f951fa VZ |
838 | wxString wxDateTime::GetMonthName(wxDateTime::Month month, |
839 | wxDateTime::NameFlags flags) | |
b76b015e | 840 | { |
dd36b5a3 | 841 | #ifdef wxHAS_STRFTIME |
9a83f860 | 842 | wxCHECK_MSG( month != Inv_Month, wxEmptyString, wxT("invalid month") ); |
e538985e | 843 | |
68ee7c47 VZ |
844 | // notice that we must set all the fields to avoid confusing libc (GNU one |
845 | // gets confused to a crash if we don't do this) | |
cd0b1709 | 846 | tm tm; |
f0f951fa | 847 | InitTm(tm); |
cd0b1709 | 848 | tm.tm_mon = month; |
b76b015e | 849 | |
9a83f860 | 850 | return CallStrftime(flags == Name_Abbr ? wxT("%b") : wxT("%B"), &tm); |
dd36b5a3 | 851 | #else // !wxHAS_STRFTIME |
e538985e | 852 | return GetEnglishMonthName(month, flags); |
dd36b5a3 | 853 | #endif // wxHAS_STRFTIME/!wxHAS_STRFTIME |
b76b015e VZ |
854 | } |
855 | ||
e538985e VZ |
856 | /* static */ |
857 | wxString wxDateTime::GetEnglishWeekDayName(WeekDay wday, NameFlags flags) | |
858 | { | |
9a83f860 | 859 | wxCHECK_MSG( wday != Inv_WeekDay, wxEmptyString, wxT("invalid weekday") ); |
e538985e | 860 | |
a243da29 | 861 | static const char *const weekdayNames[2][DAYS_PER_WEEK] = |
e538985e VZ |
862 | { |
863 | { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", | |
864 | "Saturday" }, | |
865 | { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }, | |
866 | }; | |
867 | ||
868 | const int idx = NameArrayIndexFromFlag(flags); | |
869 | if ( idx == -1 ) | |
870 | return wxString(); | |
871 | ||
872 | return weekdayNames[idx][wday]; | |
873 | } | |
874 | ||
b76b015e | 875 | /* static */ |
f0f951fa VZ |
876 | wxString wxDateTime::GetWeekDayName(wxDateTime::WeekDay wday, |
877 | wxDateTime::NameFlags flags) | |
b76b015e | 878 | { |
dd36b5a3 | 879 | #ifdef wxHAS_STRFTIME |
9a83f860 | 880 | wxCHECK_MSG( wday != Inv_WeekDay, wxEmptyString, wxT("invalid weekday") ); |
e538985e | 881 | |
2c622d71 VZ |
882 | // take some arbitrary Sunday (but notice that the day should be such that |
883 | // after adding wday to it below we still have a valid date, e.g. don't | |
884 | // take 28 here!) | |
f0f951fa VZ |
885 | tm tm; |
886 | InitTm(tm); | |
2c622d71 | 887 | tm.tm_mday = 21; |
f0f951fa VZ |
888 | tm.tm_mon = Nov; |
889 | tm.tm_year = 99; | |
b76b015e | 890 | |
1ef54dcf | 891 | // and offset it by the number of days needed to get the correct wday |
b76b015e VZ |
892 | tm.tm_mday += wday; |
893 | ||
c5a1681b VZ |
894 | // call mktime() to normalize it... |
895 | (void)mktime(&tm); | |
896 | ||
897 | // ... and call strftime() | |
9a83f860 | 898 | return CallStrftime(flags == Name_Abbr ? wxT("%a") : wxT("%A"), &tm); |
dd36b5a3 | 899 | #else // !wxHAS_STRFTIME |
e538985e | 900 | return GetEnglishWeekDayName(wday, flags); |
dd36b5a3 | 901 | #endif // wxHAS_STRFTIME/!wxHAS_STRFTIME |
f0f951fa VZ |
902 | } |
903 | ||
904 | /* static */ | |
905 | void wxDateTime::GetAmPmStrings(wxString *am, wxString *pm) | |
906 | { | |
907 | tm tm; | |
908 | InitTm(tm); | |
653567bb | 909 | wxChar buffer[64]; |
0308fd61 JS |
910 | // @Note: Do not call 'CallStrftime' here! CallStrftime checks the return code |
911 | // and causes an assertion failed if the buffer is to small (which is good) - OR - | |
912 | // if strftime does not return anything because the format string is invalid - OR - | |
913 | // if there are no 'am' / 'pm' tokens defined for the current locale (which is not good). | |
914 | // wxDateTime::ParseTime will try several different formats to parse the time. | |
915 | // As a result, GetAmPmStrings might get called, even if the current locale | |
916 | // does not define any 'am' / 'pm' tokens. In this case, wxStrftime would | |
917 | // assert, even though it is a perfectly legal use. | |
f0f951fa VZ |
918 | if ( am ) |
919 | { | |
9a83f860 | 920 | if (wxStrftime(buffer, WXSIZEOF(buffer), wxT("%p"), &tm) > 0) |
0308fd61 JS |
921 | *am = wxString(buffer); |
922 | else | |
923 | *am = wxString(); | |
f0f951fa VZ |
924 | } |
925 | if ( pm ) | |
926 | { | |
927 | tm.tm_hour = 13; | |
9a83f860 | 928 | if (wxStrftime(buffer, WXSIZEOF(buffer), wxT("%p"), &tm) > 0) |
0308fd61 JS |
929 | *pm = wxString(buffer); |
930 | else | |
931 | *pm = wxString(); | |
f0f951fa | 932 | } |
b76b015e VZ |
933 | } |
934 | ||
54e660f8 | 935 | |
239446b4 VZ |
936 | // ---------------------------------------------------------------------------- |
937 | // Country stuff: date calculations depend on the country (DST, work days, | |
938 | // ...), so we need to know which rules to follow. | |
939 | // ---------------------------------------------------------------------------- | |
940 | ||
941 | /* static */ | |
942 | wxDateTime::Country wxDateTime::GetCountry() | |
943 | { | |
f0f951fa | 944 | // TODO use LOCALE_ICOUNTRY setting under Win32 |
77b88df2 | 945 | #ifndef __WXWINCE__ |
239446b4 VZ |
946 | if ( ms_country == Country_Unknown ) |
947 | { | |
948 | // try to guess from the time zone name | |
949 | time_t t = time(NULL); | |
a452689b SN |
950 | struct tm tmstruct; |
951 | struct tm *tm = wxLocaltime_r(&t, &tmstruct); | |
239446b4 | 952 | |
9a83f860 VZ |
953 | wxString tz = CallStrftime(wxT("%Z"), tm); |
954 | if ( tz == wxT("WET") || tz == wxT("WEST") ) | |
239446b4 VZ |
955 | { |
956 | ms_country = UK; | |
957 | } | |
9a83f860 | 958 | else if ( tz == wxT("CET") || tz == wxT("CEST") ) |
239446b4 VZ |
959 | { |
960 | ms_country = Country_EEC; | |
961 | } | |
9a83f860 | 962 | else if ( tz == wxT("MSK") || tz == wxT("MSD") ) |
239446b4 VZ |
963 | { |
964 | ms_country = Russia; | |
965 | } | |
9a83f860 VZ |
966 | else if ( tz == wxT("AST") || tz == wxT("ADT") || |
967 | tz == wxT("EST") || tz == wxT("EDT") || | |
968 | tz == wxT("CST") || tz == wxT("CDT") || | |
969 | tz == wxT("MST") || tz == wxT("MDT") || | |
970 | tz == wxT("PST") || tz == wxT("PDT") ) | |
239446b4 VZ |
971 | { |
972 | ms_country = USA; | |
973 | } | |
974 | else | |
975 | { | |
976 | // well, choose a default one | |
977 | ms_country = USA; | |
978 | } | |
979 | } | |
bb7afa49 | 980 | #else // __WXWINCE__ |
77b88df2 | 981 | ms_country = USA; |
bb7afa49 | 982 | #endif // !__WXWINCE__/__WXWINCE__ |
239446b4 VZ |
983 | |
984 | return ms_country; | |
985 | } | |
986 | ||
987 | /* static */ | |
988 | void wxDateTime::SetCountry(wxDateTime::Country country) | |
989 | { | |
990 | ms_country = country; | |
991 | } | |
992 | ||
993 | /* static */ | |
994 | bool wxDateTime::IsWestEuropeanCountry(Country country) | |
995 | { | |
996 | if ( country == Country_Default ) | |
997 | { | |
998 | country = GetCountry(); | |
999 | } | |
1000 | ||
1001 | return (Country_WesternEurope_Start <= country) && | |
1002 | (country <= Country_WesternEurope_End); | |
1003 | } | |
1004 | ||
1005 | // ---------------------------------------------------------------------------- | |
1006 | // DST calculations: we use 3 different rules for the West European countries, | |
1007 | // USA and for the rest of the world. This is undoubtedly false for many | |
1008 | // countries, but I lack the necessary info (and the time to gather it), | |
1009 | // please add the other rules here! | |
1010 | // ---------------------------------------------------------------------------- | |
1011 | ||
1012 | /* static */ | |
1013 | bool wxDateTime::IsDSTApplicable(int year, Country country) | |
1014 | { | |
1015 | if ( year == Inv_Year ) | |
1016 | { | |
1017 | // take the current year if none given | |
1018 | year = GetCurrentYear(); | |
1019 | } | |
1020 | ||
1021 | if ( country == Country_Default ) | |
1022 | { | |
1023 | country = GetCountry(); | |
1024 | } | |
1025 | ||
1026 | switch ( country ) | |
1027 | { | |
1028 | case USA: | |
1029 | case UK: | |
1030 | // DST was first observed in the US and UK during WWI, reused | |
1031 | // during WWII and used again since 1966 | |
1032 | return year >= 1966 || | |
1033 | (year >= 1942 && year <= 1945) || | |
1034 | (year == 1918 || year == 1919); | |
1035 | ||
1036 | default: | |
1037 | // assume that it started after WWII | |
1038 | return year > 1950; | |
1039 | } | |
1040 | } | |
1041 | ||
1042 | /* static */ | |
1043 | wxDateTime wxDateTime::GetBeginDST(int year, Country country) | |
1044 | { | |
1045 | if ( year == Inv_Year ) | |
1046 | { | |
1047 | // take the current year if none given | |
1048 | year = GetCurrentYear(); | |
1049 | } | |
1050 | ||
1051 | if ( country == Country_Default ) | |
1052 | { | |
1053 | country = GetCountry(); | |
1054 | } | |
1055 | ||
1056 | if ( !IsDSTApplicable(year, country) ) | |
1057 | { | |
2ef31e80 | 1058 | return wxInvalidDateTime; |
239446b4 VZ |
1059 | } |
1060 | ||
1061 | wxDateTime dt; | |
1062 | ||
1063 | if ( IsWestEuropeanCountry(country) || (country == Russia) ) | |
1064 | { | |
1065 | // DST begins at 1 a.m. GMT on the last Sunday of March | |
1066 | if ( !dt.SetToLastWeekDay(Sun, Mar, year) ) | |
1067 | { | |
1068 | // weird... | |
9a83f860 | 1069 | wxFAIL_MSG( wxT("no last Sunday in March?") ); |
239446b4 VZ |
1070 | } |
1071 | ||
1072 | dt += wxTimeSpan::Hours(1); | |
239446b4 VZ |
1073 | } |
1074 | else switch ( country ) | |
1075 | { | |
1076 | case USA: | |
1077 | switch ( year ) | |
1078 | { | |
1079 | case 1918: | |
1080 | case 1919: | |
1081 | // don't know for sure - assume it was in effect all year | |
1082 | ||
1083 | case 1943: | |
1084 | case 1944: | |
1085 | case 1945: | |
1086 | dt.Set(1, Jan, year); | |
1087 | break; | |
1088 | ||
1089 | case 1942: | |
1090 | // DST was installed Feb 2, 1942 by the Congress | |
1091 | dt.Set(2, Feb, year); | |
1092 | break; | |
1093 | ||
1094 | // Oil embargo changed the DST period in the US | |
1095 | case 1974: | |
1096 | dt.Set(6, Jan, 1974); | |
1097 | break; | |
1098 | ||
1099 | case 1975: | |
1100 | dt.Set(23, Feb, 1975); | |
1101 | break; | |
1102 | ||
1103 | default: | |
1104 | // before 1986, DST begun on the last Sunday of April, but | |
1105 | // in 1986 Reagan changed it to begin at 2 a.m. of the | |
1106 | // first Sunday in April | |
1107 | if ( year < 1986 ) | |
1108 | { | |
1109 | if ( !dt.SetToLastWeekDay(Sun, Apr, year) ) | |
1110 | { | |
1111 | // weird... | |
9a83f860 | 1112 | wxFAIL_MSG( wxT("no first Sunday in April?") ); |
239446b4 VZ |
1113 | } |
1114 | } | |
6b522db5 VZ |
1115 | else if ( year > 2006 ) |
1116 | // Energy Policy Act of 2005, Pub. L. no. 109-58, 119 Stat 594 (2005). | |
1117 | // Starting in 2007, daylight time begins in the United States on the | |
1118 | // second Sunday in March and ends on the first Sunday in November | |
1119 | { | |
1120 | if ( !dt.SetToWeekDay(Sun, 2, Mar, year) ) | |
1121 | { | |
1122 | // weird... | |
9a83f860 | 1123 | wxFAIL_MSG( wxT("no second Sunday in March?") ); |
6b522db5 VZ |
1124 | } |
1125 | } | |
239446b4 VZ |
1126 | else |
1127 | { | |
1128 | if ( !dt.SetToWeekDay(Sun, 1, Apr, year) ) | |
1129 | { | |
1130 | // weird... | |
9a83f860 | 1131 | wxFAIL_MSG( wxT("no first Sunday in April?") ); |
239446b4 VZ |
1132 | } |
1133 | } | |
1134 | ||
1135 | dt += wxTimeSpan::Hours(2); | |
1136 | ||
1137 | // TODO what about timezone?? | |
1138 | } | |
1139 | ||
1140 | break; | |
1141 | ||
1142 | default: | |
1143 | // assume Mar 30 as the start of the DST for the rest of the world | |
1144 | // - totally bogus, of course | |
1145 | dt.Set(30, Mar, year); | |
1146 | } | |
1147 | ||
1148 | return dt; | |
1149 | } | |
1150 | ||
1151 | /* static */ | |
1152 | wxDateTime wxDateTime::GetEndDST(int year, Country country) | |
1153 | { | |
1154 | if ( year == Inv_Year ) | |
1155 | { | |
1156 | // take the current year if none given | |
1157 | year = GetCurrentYear(); | |
1158 | } | |
1159 | ||
1160 | if ( country == Country_Default ) | |
1161 | { | |
1162 | country = GetCountry(); | |
1163 | } | |
1164 | ||
1165 | if ( !IsDSTApplicable(year, country) ) | |
1166 | { | |
2ef31e80 | 1167 | return wxInvalidDateTime; |
239446b4 VZ |
1168 | } |
1169 | ||
1170 | wxDateTime dt; | |
1171 | ||
1172 | if ( IsWestEuropeanCountry(country) || (country == Russia) ) | |
1173 | { | |
5f287370 | 1174 | // DST ends at 1 a.m. GMT on the last Sunday of October |
239446b4 VZ |
1175 | if ( !dt.SetToLastWeekDay(Sun, Oct, year) ) |
1176 | { | |
1177 | // weirder and weirder... | |
9a83f860 | 1178 | wxFAIL_MSG( wxT("no last Sunday in October?") ); |
239446b4 VZ |
1179 | } |
1180 | ||
1181 | dt += wxTimeSpan::Hours(1); | |
239446b4 VZ |
1182 | } |
1183 | else switch ( country ) | |
1184 | { | |
1185 | case USA: | |
1186 | switch ( year ) | |
1187 | { | |
1188 | case 1918: | |
1189 | case 1919: | |
1190 | // don't know for sure - assume it was in effect all year | |
1191 | ||
1192 | case 1943: | |
1193 | case 1944: | |
1194 | dt.Set(31, Dec, year); | |
1195 | break; | |
1196 | ||
1197 | case 1945: | |
1198 | // the time was reset after the end of the WWII | |
1199 | dt.Set(30, Sep, year); | |
1200 | break; | |
1201 | ||
6b522db5 VZ |
1202 | default: // default for switch (year) |
1203 | if ( year > 2006 ) | |
1204 | // Energy Policy Act of 2005, Pub. L. no. 109-58, 119 Stat 594 (2005). | |
1205 | // Starting in 2007, daylight time begins in the United States on the | |
1206 | // second Sunday in March and ends on the first Sunday in November | |
1207 | { | |
1208 | if ( !dt.SetToWeekDay(Sun, 1, Nov, year) ) | |
1209 | { | |
1210 | // weird... | |
9a83f860 | 1211 | wxFAIL_MSG( wxT("no first Sunday in November?") ); |
6b522db5 VZ |
1212 | } |
1213 | } | |
1214 | else | |
1215 | // pre-2007 | |
1216 | // DST ends at 2 a.m. on the last Sunday of October | |
239446b4 | 1217 | { |
6b522db5 VZ |
1218 | if ( !dt.SetToLastWeekDay(Sun, Oct, year) ) |
1219 | { | |
1220 | // weirder and weirder... | |
9a83f860 | 1221 | wxFAIL_MSG( wxT("no last Sunday in October?") ); |
6b522db5 | 1222 | } |
239446b4 VZ |
1223 | } |
1224 | ||
1225 | dt += wxTimeSpan::Hours(2); | |
1226 | ||
6b522db5 | 1227 | // TODO: what about timezone?? |
239446b4 VZ |
1228 | } |
1229 | break; | |
1230 | ||
6b522db5 | 1231 | default: // default for switch (country) |
239446b4 VZ |
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 | ||
9a83f860 | 1268 | wxFAIL_MSG( wxT("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, | |
9a83f860 | 1291 | wxT("Invalid time in wxDateTime::Set()") ); |
0979c962 VZ |
1292 | |
1293 | // get the current date from system | |
a452689b SN |
1294 | struct tm tmstruct; |
1295 | struct tm *tm = GetTmNow(&tmstruct); | |
299fcbfe | 1296 | |
9a83f860 | 1297 | wxDATETIME_CHECK( tm, wxT("wxLocaltime_r() failed") ); |
0979c962 | 1298 | |
456639ac VZ |
1299 | // make a copy so it isn't clobbered by the call to mktime() below |
1300 | struct tm tm1(*tm); | |
1301 | ||
0979c962 | 1302 | // adjust the time |
456639ac VZ |
1303 | tm1.tm_hour = hour; |
1304 | tm1.tm_min = minute; | |
1305 | tm1.tm_sec = second; | |
0979c962 | 1306 | |
9cc311d3 | 1307 | // and the DST in case it changes on this date |
456639ac | 1308 | struct tm tm2(tm1); |
9cc311d3 | 1309 | mktime(&tm2); |
456639ac VZ |
1310 | if ( tm2.tm_isdst != tm1.tm_isdst ) |
1311 | tm1.tm_isdst = tm2.tm_isdst; | |
9cc311d3 | 1312 | |
456639ac | 1313 | (void)Set(tm1); |
0979c962 VZ |
1314 | |
1315 | // and finally adjust milliseconds | |
1316 | return SetMillisecond(millisec); | |
1317 | } | |
1318 | ||
1319 | wxDateTime& wxDateTime::Set(wxDateTime_t day, | |
1320 | Month month, | |
1321 | int year, | |
1322 | wxDateTime_t hour, | |
1323 | wxDateTime_t minute, | |
1324 | wxDateTime_t second, | |
1325 | wxDateTime_t millisec) | |
1326 | { | |
384223b3 VZ |
1327 | wxDATETIME_CHECK( hour < 24 && |
1328 | second < 62 && | |
1329 | minute < 60 && | |
1330 | millisec < 1000, | |
9a83f860 | 1331 | wxT("Invalid time in wxDateTime::Set()") ); |
0979c962 | 1332 | |
2f02cb89 | 1333 | ReplaceDefaultYearMonthWithCurrent(&year, &month); |
0979c962 | 1334 | |
384223b3 | 1335 | wxDATETIME_CHECK( (0 < day) && (day <= GetNumberOfDays(month, year)), |
9a83f860 | 1336 | wxT("Invalid date in wxDateTime::Set()") ); |
0979c962 VZ |
1337 | |
1338 | // the range of time_t type (inclusive) | |
1339 | static const int yearMinInRange = 1970; | |
1340 | static const int yearMaxInRange = 2037; | |
1341 | ||
1342 | // test only the year instead of testing for the exact end of the Unix | |
1343 | // time_t range - it doesn't bring anything to do more precise checks | |
2f02cb89 | 1344 | if ( year >= yearMinInRange && year <= yearMaxInRange ) |
0979c962 VZ |
1345 | { |
1346 | // use the standard library version if the date is in range - this is | |
b76b015e | 1347 | // probably more efficient than our code |
0979c962 | 1348 | struct tm tm; |
b76b015e | 1349 | tm.tm_year = year - 1900; |
0979c962 VZ |
1350 | tm.tm_mon = month; |
1351 | tm.tm_mday = day; | |
1352 | tm.tm_hour = hour; | |
1353 | tm.tm_min = minute; | |
1354 | tm.tm_sec = second; | |
299fcbfe | 1355 | tm.tm_isdst = -1; // mktime() will guess it |
0979c962 VZ |
1356 | |
1357 | (void)Set(tm); | |
1358 | ||
1359 | // and finally adjust milliseconds | |
3d78a532 RD |
1360 | if (IsValid()) |
1361 | SetMillisecond(millisec); | |
1362 | ||
1363 | return *this; | |
0979c962 VZ |
1364 | } |
1365 | else | |
1366 | { | |
1367 | // do time calculations ourselves: we want to calculate the number of | |
fcc3d7cb | 1368 | // milliseconds between the given date and the epoch |
e6ec579c VZ |
1369 | |
1370 | // get the JDN for the midnight of this day | |
1371 | m_time = GetTruncatedJDN(day, month, year); | |
1372 | m_time -= EPOCH_JDN; | |
1373 | m_time *= SECONDS_PER_DAY * TIME_T_FACTOR; | |
1374 | ||
299fcbfe VZ |
1375 | // JDN corresponds to GMT, we take localtime |
1376 | Add(wxTimeSpan(hour, minute, second + GetTimeZone(), millisec)); | |
b76b015e VZ |
1377 | } |
1378 | ||
1379 | return *this; | |
1380 | } | |
1381 | ||
e6ec579c VZ |
1382 | wxDateTime& wxDateTime::Set(double jdn) |
1383 | { | |
1ef54dcf VZ |
1384 | // so that m_time will be 0 for the midnight of Jan 1, 1970 which is jdn |
1385 | // EPOCH_JDN + 0.5 | |
1386 | jdn -= EPOCH_JDN + 0.5; | |
1387 | ||
d26adb9d | 1388 | m_time.Assign(jdn*MILLISECONDS_PER_DAY); |
cd0b1709 | 1389 | |
d26adb9d | 1390 | // JDNs always are in UTC, so we don't need any adjustments for time zone |
e6ec579c VZ |
1391 | |
1392 | return *this; | |
1393 | } | |
1394 | ||
9d9b7755 VZ |
1395 | wxDateTime& wxDateTime::ResetTime() |
1396 | { | |
1397 | Tm tm = GetTm(); | |
1398 | ||
1399 | if ( tm.hour || tm.min || tm.sec || tm.msec ) | |
1400 | { | |
1401 | tm.msec = | |
1402 | tm.sec = | |
1403 | tm.min = | |
1404 | tm.hour = 0; | |
1405 | ||
1406 | Set(tm); | |
1407 | } | |
1408 | ||
1409 | return *this; | |
1410 | } | |
1411 | ||
fb96cf85 VZ |
1412 | wxDateTime wxDateTime::GetDateOnly() const |
1413 | { | |
1414 | Tm tm = GetTm(); | |
1415 | tm.msec = | |
1416 | tm.sec = | |
1417 | tm.min = | |
1418 | tm.hour = 0; | |
1419 | return wxDateTime(tm); | |
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(); | |
a452689b SN |
1473 | struct tm tmstruct; |
1474 | struct tm *tm = wxLocaltime_r(&ticks, &tmstruct); | |
9a83f860 | 1475 | wxCHECK_MSG( tm, ULONG_MAX, wxT("time can't be represented in DOS format") ); |
2b5f62a0 VZ |
1476 | |
1477 | long year = tm->tm_year; | |
1478 | year -= 80; | |
1479 | year <<= 25; | |
1480 | ||
1481 | long month = tm->tm_mon; | |
1482 | month += 1; | |
1483 | month <<= 21; | |
1484 | ||
1485 | long day = tm->tm_mday; | |
1486 | day <<= 16; | |
1487 | ||
1488 | long hour = tm->tm_hour; | |
1489 | hour <<= 11; | |
1490 | ||
1491 | long minute = tm->tm_min; | |
1492 | minute <<= 5; | |
1493 | ||
1494 | long second = tm->tm_sec; | |
1495 | second /= 2; | |
1496 | ||
1497 | ddt = year | month | day | hour | minute | second; | |
1498 | return ddt; | |
1499 | } | |
1500 | ||
b76b015e VZ |
1501 | // ---------------------------------------------------------------------------- |
1502 | // time_t <-> broken down time conversions | |
1503 | // ---------------------------------------------------------------------------- | |
1504 | ||
299fcbfe | 1505 | wxDateTime::Tm wxDateTime::GetTm(const TimeZone& tz) const |
b76b015e | 1506 | { |
9a83f860 | 1507 | wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime") ); |
b76b015e VZ |
1508 | |
1509 | time_t time = GetTicks(); | |
1510 | if ( time != (time_t)-1 ) | |
1511 | { | |
1512 | // use C RTL functions | |
a452689b | 1513 | struct tm tmstruct; |
299fcbfe VZ |
1514 | tm *tm; |
1515 | if ( tz.GetOffset() == -GetTimeZone() ) | |
1516 | { | |
1517 | // we are working with local time | |
a452689b | 1518 | tm = wxLocaltime_r(&time, &tmstruct); |
c5a1681b VZ |
1519 | |
1520 | // should never happen | |
9a83f860 | 1521 | wxCHECK_MSG( tm, Tm(), wxT("wxLocaltime_r() failed") ); |
299fcbfe VZ |
1522 | } |
1523 | else | |
1524 | { | |
13111b2a | 1525 | time += (time_t)tz.GetOffset(); |
33ac7e6f | 1526 | #if defined(__VMS__) || defined(__WATCOMC__) // time is unsigned so avoid warning |
13111b2a | 1527 | int time2 = (int) time; |
9d9b7755 | 1528 | if ( time2 >= 0 ) |
fb10f04c | 1529 | #else |
9d9b7755 | 1530 | if ( time >= 0 ) |
fb10f04c | 1531 | #endif |
c5a1681b | 1532 | { |
a452689b | 1533 | tm = wxGmtime_r(&time, &tmstruct); |
b76b015e | 1534 | |
c5a1681b | 1535 | // should never happen |
9a83f860 | 1536 | wxCHECK_MSG( tm, Tm(), wxT("wxGmtime_r() failed") ); |
c5a1681b VZ |
1537 | } |
1538 | else | |
1539 | { | |
1540 | tm = (struct tm *)NULL; | |
1541 | } | |
1542 | } | |
b76b015e | 1543 | |
c5a1681b VZ |
1544 | if ( tm ) |
1545 | { | |
f6bcfd97 BP |
1546 | // adjust the milliseconds |
1547 | Tm tm2(*tm, tz); | |
1548 | long timeOnly = (m_time % MILLISECONDS_PER_DAY).ToLong(); | |
1549 | tm2.msec = (wxDateTime_t)(timeOnly % 1000); | |
1550 | return tm2; | |
c5a1681b VZ |
1551 | } |
1552 | //else: use generic code below | |
b76b015e | 1553 | } |
e6ec579c | 1554 | |
c5a1681b VZ |
1555 | // remember the time and do the calculations with the date only - this |
1556 | // eliminates rounding errors of the floating point arithmetics | |
299fcbfe | 1557 | |
c5a1681b | 1558 | wxLongLong timeMidnight = m_time + tz.GetOffset() * 1000; |
1ef54dcf | 1559 | |
c5a1681b | 1560 | long timeOnly = (timeMidnight % MILLISECONDS_PER_DAY).ToLong(); |
1ef54dcf | 1561 | |
c5a1681b VZ |
1562 | // we want to always have positive time and timeMidnight to be really |
1563 | // the midnight before it | |
1564 | if ( timeOnly < 0 ) | |
1565 | { | |
1566 | timeOnly = MILLISECONDS_PER_DAY + timeOnly; | |
1567 | } | |
e6ec579c | 1568 | |
c5a1681b | 1569 | timeMidnight -= timeOnly; |
1ef54dcf | 1570 | |
c5a1681b VZ |
1571 | // calculate the Gregorian date from JDN for the midnight of our date: |
1572 | // this will yield day, month (in 1..12 range) and year | |
1ef54dcf | 1573 | |
c5a1681b VZ |
1574 | // actually, this is the JDN for the noon of the previous day |
1575 | long jdn = (timeMidnight / MILLISECONDS_PER_DAY).ToLong() + EPOCH_JDN; | |
1ef54dcf | 1576 | |
c5a1681b | 1577 | // CREDIT: code below is by Scott E. Lee (but bugs are mine) |
1ef54dcf | 1578 | |
9a83f860 | 1579 | wxASSERT_MSG( jdn > -2, wxT("JDN out of range") ); |
1ef54dcf | 1580 | |
c5a1681b | 1581 | // calculate the century |
479cd5de VZ |
1582 | long temp = (jdn + JDN_OFFSET) * 4 - 1; |
1583 | long century = temp / DAYS_PER_400_YEARS; | |
1ef54dcf | 1584 | |
c5a1681b VZ |
1585 | // then the year and day of year (1 <= dayOfYear <= 366) |
1586 | temp = ((temp % DAYS_PER_400_YEARS) / 4) * 4 + 3; | |
479cd5de VZ |
1587 | long year = (century * 100) + (temp / DAYS_PER_4_YEARS); |
1588 | long dayOfYear = (temp % DAYS_PER_4_YEARS) / 4 + 1; | |
1ef54dcf | 1589 | |
c5a1681b VZ |
1590 | // and finally the month and day of the month |
1591 | temp = dayOfYear * 5 - 3; | |
479cd5de VZ |
1592 | long month = temp / DAYS_PER_5_MONTHS; |
1593 | long day = (temp % DAYS_PER_5_MONTHS) / 5 + 1; | |
c5a1681b VZ |
1594 | |
1595 | // month is counted from March - convert to normal | |
1596 | if ( month < 10 ) | |
1597 | { | |
1598 | month += 3; | |
1599 | } | |
1600 | else | |
1601 | { | |
1602 | year += 1; | |
1603 | month -= 9; | |
1604 | } | |
1ef54dcf | 1605 | |
c5a1681b VZ |
1606 | // year is offset by 4800 |
1607 | year -= 4800; | |
1ef54dcf | 1608 | |
c5a1681b | 1609 | // check that the algorithm gave us something reasonable |
9a83f860 VZ |
1610 | wxASSERT_MSG( (0 < month) && (month <= 12), wxT("invalid month") ); |
1611 | wxASSERT_MSG( (1 <= day) && (day < 32), wxT("invalid day") ); | |
e6ec579c | 1612 | |
c5a1681b VZ |
1613 | // construct Tm from these values |
1614 | Tm tm; | |
1615 | tm.year = (int)year; | |
5ed8879e | 1616 | tm.yday = (wxDateTime_t)(dayOfYear - 1); // use C convention for day number |
c5a1681b VZ |
1617 | tm.mon = (Month)(month - 1); // algorithm yields 1 for January, not 0 |
1618 | tm.mday = (wxDateTime_t)day; | |
479cd5de | 1619 | tm.msec = (wxDateTime_t)(timeOnly % 1000); |
c5a1681b VZ |
1620 | timeOnly -= tm.msec; |
1621 | timeOnly /= 1000; // now we have time in seconds | |
e6ec579c | 1622 | |
d26adb9d | 1623 | tm.sec = (wxDateTime_t)(timeOnly % SEC_PER_MIN); |
c5a1681b | 1624 | timeOnly -= tm.sec; |
d26adb9d | 1625 | timeOnly /= SEC_PER_MIN; // now we have time in minutes |
e6ec579c | 1626 | |
d26adb9d | 1627 | tm.min = (wxDateTime_t)(timeOnly % MIN_PER_HOUR); |
c5a1681b | 1628 | timeOnly -= tm.min; |
e6ec579c | 1629 | |
d26adb9d | 1630 | tm.hour = (wxDateTime_t)(timeOnly / MIN_PER_HOUR); |
b76b015e | 1631 | |
c5a1681b | 1632 | return tm; |
b76b015e VZ |
1633 | } |
1634 | ||
1635 | wxDateTime& wxDateTime::SetYear(int year) | |
1636 | { | |
9a83f860 | 1637 | wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime") ); |
b76b015e VZ |
1638 | |
1639 | Tm tm(GetTm()); | |
1640 | tm.year = year; | |
1641 | Set(tm); | |
1642 | ||
1643 | return *this; | |
1644 | } | |
1645 | ||
1646 | wxDateTime& wxDateTime::SetMonth(Month month) | |
1647 | { | |
9a83f860 | 1648 | wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime") ); |
b76b015e VZ |
1649 | |
1650 | Tm tm(GetTm()); | |
1651 | tm.mon = month; | |
1652 | Set(tm); | |
1653 | ||
1654 | return *this; | |
1655 | } | |
1656 | ||
1657 | wxDateTime& wxDateTime::SetDay(wxDateTime_t mday) | |
1658 | { | |
9a83f860 | 1659 | wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime") ); |
b76b015e VZ |
1660 | |
1661 | Tm tm(GetTm()); | |
1662 | tm.mday = mday; | |
1663 | Set(tm); | |
1664 | ||
1665 | return *this; | |
1666 | } | |
1667 | ||
1668 | wxDateTime& wxDateTime::SetHour(wxDateTime_t hour) | |
1669 | { | |
9a83f860 | 1670 | wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime") ); |
b76b015e VZ |
1671 | |
1672 | Tm tm(GetTm()); | |
1673 | tm.hour = hour; | |
1674 | Set(tm); | |
1675 | ||
1676 | return *this; | |
1677 | } | |
1678 | ||
1679 | wxDateTime& wxDateTime::SetMinute(wxDateTime_t min) | |
1680 | { | |
9a83f860 | 1681 | wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime") ); |
b76b015e VZ |
1682 | |
1683 | Tm tm(GetTm()); | |
1684 | tm.min = min; | |
1685 | Set(tm); | |
1686 | ||
1687 | return *this; | |
1688 | } | |
1689 | ||
1690 | wxDateTime& wxDateTime::SetSecond(wxDateTime_t sec) | |
1691 | { | |
9a83f860 | 1692 | wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime") ); |
b76b015e VZ |
1693 | |
1694 | Tm tm(GetTm()); | |
1695 | tm.sec = sec; | |
1696 | Set(tm); | |
0979c962 VZ |
1697 | |
1698 | return *this; | |
1699 | } | |
b76b015e VZ |
1700 | |
1701 | wxDateTime& wxDateTime::SetMillisecond(wxDateTime_t millisecond) | |
1702 | { | |
9a83f860 | 1703 | wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime") ); |
b76b015e VZ |
1704 | |
1705 | // we don't need to use GetTm() for this one | |
1706 | m_time -= m_time % 1000l; | |
1707 | m_time += millisecond; | |
1708 | ||
1709 | return *this; | |
1710 | } | |
1711 | ||
1712 | // ---------------------------------------------------------------------------- | |
1713 | // wxDateTime arithmetics | |
1714 | // ---------------------------------------------------------------------------- | |
1715 | ||
1716 | wxDateTime& wxDateTime::Add(const wxDateSpan& diff) | |
1717 | { | |
1718 | Tm tm(GetTm()); | |
1719 | ||
1720 | tm.year += diff.GetYears(); | |
fcc3d7cb | 1721 | tm.AddMonths(diff.GetMonths()); |
4f6aed9c VZ |
1722 | |
1723 | // check that the resulting date is valid | |
1724 | if ( tm.mday > GetNumOfDaysInMonth(tm.year, tm.mon) ) | |
1725 | { | |
1726 | // We suppose that when adding one month to Jan 31 we want to get Feb | |
1727 | // 28 (or 29), i.e. adding a month to the last day of the month should | |
1728 | // give the last day of the next month which is quite logical. | |
1729 | // | |
1730 | // Unfortunately, there is no logic way to understand what should | |
1731 | // Jan 30 + 1 month be - Feb 28 too or Feb 27 (assuming non leap year)? | |
1732 | // We make it Feb 28 (last day too), but it is highly questionable. | |
1733 | tm.mday = GetNumOfDaysInMonth(tm.year, tm.mon); | |
1734 | } | |
1735 | ||
fcc3d7cb | 1736 | tm.AddDays(diff.GetTotalDays()); |
b76b015e VZ |
1737 | |
1738 | Set(tm); | |
1739 | ||
9d9b7755 | 1740 | wxASSERT_MSG( IsSameTime(tm), |
9a83f860 | 1741 | wxT("Add(wxDateSpan) shouldn't modify time") ); |
9d9b7755 | 1742 | |
b76b015e VZ |
1743 | return *this; |
1744 | } | |
1745 | ||
2f02cb89 VZ |
1746 | // ---------------------------------------------------------------------------- |
1747 | // Weekday and monthday stuff | |
1748 | // ---------------------------------------------------------------------------- | |
1749 | ||
4c27e2fa VZ |
1750 | // convert Sun, Mon, ..., Sat into 6, 0, ..., 5 |
1751 | static inline int ConvertWeekDayToMondayBase(int wd) | |
1752 | { | |
1753 | return wd == wxDateTime::Sun ? 6 : wd - 1; | |
1754 | } | |
1755 | ||
1756 | /* static */ | |
1757 | wxDateTime | |
1758 | wxDateTime::SetToWeekOfYear(int year, wxDateTime_t numWeek, WeekDay wd) | |
4f6aed9c | 1759 | { |
2b5f62a0 | 1760 | wxASSERT_MSG( numWeek > 0, |
9a83f860 | 1761 | wxT("invalid week number: weeks are counted from 1") ); |
2b5f62a0 | 1762 | |
4c27e2fa VZ |
1763 | // Jan 4 always lies in the 1st week of the year |
1764 | wxDateTime dt(4, Jan, year); | |
1765 | dt.SetToWeekDayInSameWeek(wd); | |
1766 | dt += wxDateSpan::Weeks(numWeek - 1); | |
1767 | ||
1768 | return dt; | |
1769 | } | |
4f6aed9c | 1770 | |
ca3e85cf | 1771 | #if WXWIN_COMPATIBILITY_2_6 |
4c27e2fa VZ |
1772 | // use a separate function to avoid warnings about using deprecated |
1773 | // SetToTheWeek in GetWeek below | |
1774 | static wxDateTime | |
1775 | SetToTheWeek(int year, | |
1776 | wxDateTime::wxDateTime_t numWeek, | |
1777 | wxDateTime::WeekDay weekday, | |
1778 | wxDateTime::WeekFlags flags) | |
1779 | { | |
4f6aed9c | 1780 | // Jan 4 always lies in the 1st week of the year |
4c27e2fa VZ |
1781 | wxDateTime dt(4, wxDateTime::Jan, year); |
1782 | dt.SetToWeekDayInSameWeek(weekday, flags); | |
1783 | dt += wxDateSpan::Weeks(numWeek - 1); | |
4f6aed9c | 1784 | |
4c27e2fa VZ |
1785 | return dt; |
1786 | } | |
1787 | ||
1788 | bool wxDateTime::SetToTheWeek(wxDateTime_t numWeek, | |
1789 | WeekDay weekday, | |
1790 | WeekFlags flags) | |
1791 | { | |
1792 | int year = GetYear(); | |
1793 | *this = ::SetToTheWeek(year, numWeek, weekday, flags); | |
4f6aed9c VZ |
1794 | if ( GetYear() != year ) |
1795 | { | |
1796 | // oops... numWeek was too big | |
68379eaf | 1797 | return false; |
4f6aed9c VZ |
1798 | } |
1799 | ||
68379eaf | 1800 | return true; |
4f6aed9c VZ |
1801 | } |
1802 | ||
4c27e2fa VZ |
1803 | wxDateTime wxDateTime::GetWeek(wxDateTime_t numWeek, |
1804 | WeekDay weekday, | |
1805 | WeekFlags flags) const | |
1806 | { | |
1807 | return ::SetToTheWeek(GetYear(), numWeek, weekday, flags); | |
1808 | } | |
ca3e85cf | 1809 | #endif // WXWIN_COMPATIBILITY_2_6 |
4c27e2fa | 1810 | |
2f02cb89 VZ |
1811 | wxDateTime& wxDateTime::SetToLastMonthDay(Month month, |
1812 | int year) | |
1813 | { | |
1814 | // take the current month/year if none specified | |
1a8557b1 VZ |
1815 | if ( year == Inv_Year ) |
1816 | year = GetYear(); | |
1817 | if ( month == Inv_Month ) | |
1818 | month = GetMonth(); | |
2f02cb89 | 1819 | |
fcc3d7cb | 1820 | return Set(GetNumOfDaysInMonth(year, month), month, year); |
2f02cb89 VZ |
1821 | } |
1822 | ||
2b5f62a0 | 1823 | wxDateTime& wxDateTime::SetToWeekDayInSameWeek(WeekDay weekday, WeekFlags flags) |
cd0b1709 | 1824 | { |
9a83f860 | 1825 | wxDATETIME_CHECK( weekday != Inv_WeekDay, wxT("invalid weekday") ); |
cd0b1709 | 1826 | |
af80bc92 VZ |
1827 | int wdayDst = weekday, |
1828 | wdayThis = GetWeekDay(); | |
1829 | if ( wdayDst == wdayThis ) | |
cd0b1709 VZ |
1830 | { |
1831 | // nothing to do | |
1832 | return *this; | |
1833 | } | |
2b5f62a0 VZ |
1834 | |
1835 | if ( flags == Default_First ) | |
1836 | { | |
1837 | flags = GetCountry() == USA ? Sunday_First : Monday_First; | |
1838 | } | |
1839 | ||
1840 | // the logic below based on comparing weekday and wdayThis works if Sun (0) | |
1841 | // is the first day in the week, but breaks down for Monday_First case so | |
1842 | // we adjust the week days in this case | |
af80bc92 | 1843 | if ( flags == Monday_First ) |
2b5f62a0 VZ |
1844 | { |
1845 | if ( wdayThis == Sun ) | |
1846 | wdayThis += 7; | |
af80bc92 VZ |
1847 | if ( wdayDst == Sun ) |
1848 | wdayDst += 7; | |
2b5f62a0 VZ |
1849 | } |
1850 | //else: Sunday_First, nothing to do | |
1851 | ||
1852 | // go forward or back in time to the day we want | |
af80bc92 | 1853 | if ( wdayDst < wdayThis ) |
cd0b1709 | 1854 | { |
af80bc92 | 1855 | return Subtract(wxDateSpan::Days(wdayThis - wdayDst)); |
cd0b1709 VZ |
1856 | } |
1857 | else // weekday > wdayThis | |
1858 | { | |
af80bc92 | 1859 | return Add(wxDateSpan::Days(wdayDst - wdayThis)); |
cd0b1709 VZ |
1860 | } |
1861 | } | |
1862 | ||
1863 | wxDateTime& wxDateTime::SetToNextWeekDay(WeekDay weekday) | |
1864 | { | |
9a83f860 | 1865 | wxDATETIME_CHECK( weekday != Inv_WeekDay, wxT("invalid weekday") ); |
cd0b1709 VZ |
1866 | |
1867 | int diff; | |
1868 | WeekDay wdayThis = GetWeekDay(); | |
1869 | if ( weekday == wdayThis ) | |
1870 | { | |
1871 | // nothing to do | |
1872 | return *this; | |
1873 | } | |
1874 | else if ( weekday < wdayThis ) | |
1875 | { | |
1876 | // need to advance a week | |
1877 | diff = 7 - (wdayThis - weekday); | |
1878 | } | |
1879 | else // weekday > wdayThis | |
1880 | { | |
1881 | diff = weekday - wdayThis; | |
1882 | } | |
1883 | ||
4f6aed9c | 1884 | return Add(wxDateSpan::Days(diff)); |
cd0b1709 VZ |
1885 | } |
1886 | ||
1887 | wxDateTime& wxDateTime::SetToPrevWeekDay(WeekDay weekday) | |
1888 | { | |
9a83f860 | 1889 | wxDATETIME_CHECK( weekday != Inv_WeekDay, wxT("invalid weekday") ); |
cd0b1709 VZ |
1890 | |
1891 | int diff; | |
1892 | WeekDay wdayThis = GetWeekDay(); | |
1893 | if ( weekday == wdayThis ) | |
1894 | { | |
1895 | // nothing to do | |
1896 | return *this; | |
1897 | } | |
1898 | else if ( weekday > wdayThis ) | |
1899 | { | |
1900 | // need to go to previous week | |
1901 | diff = 7 - (weekday - wdayThis); | |
1902 | } | |
1903 | else // weekday < wdayThis | |
1904 | { | |
1905 | diff = wdayThis - weekday; | |
1906 | } | |
1907 | ||
f6bcfd97 | 1908 | return Subtract(wxDateSpan::Days(diff)); |
cd0b1709 VZ |
1909 | } |
1910 | ||
2f02cb89 VZ |
1911 | bool wxDateTime::SetToWeekDay(WeekDay weekday, |
1912 | int n, | |
1913 | Month month, | |
1914 | int year) | |
1915 | { | |
9a83f860 | 1916 | wxCHECK_MSG( weekday != Inv_WeekDay, false, wxT("invalid weekday") ); |
2f02cb89 | 1917 | |
68379eaf | 1918 | // we don't check explicitly that -5 <= n <= 5 because we will return false |
2f02cb89 VZ |
1919 | // anyhow in such case - but may be should still give an assert for it? |
1920 | ||
1921 | // take the current month/year if none specified | |
1922 | ReplaceDefaultYearMonthWithCurrent(&year, &month); | |
1923 | ||
1924 | wxDateTime dt; | |
1925 | ||
1926 | // TODO this probably could be optimised somehow... | |
1927 | ||
1928 | if ( n > 0 ) | |
1929 | { | |
1930 | // get the first day of the month | |
1931 | dt.Set(1, month, year); | |
1932 | ||
1933 | // get its wday | |
1934 | WeekDay wdayFirst = dt.GetWeekDay(); | |
1935 | ||
1936 | // go to the first weekday of the month | |
1937 | int diff = weekday - wdayFirst; | |
1938 | if ( diff < 0 ) | |
1939 | diff += 7; | |
1940 | ||
1941 | // add advance n-1 weeks more | |
1942 | diff += 7*(n - 1); | |
1943 | ||
239446b4 | 1944 | dt += wxDateSpan::Days(diff); |
2f02cb89 | 1945 | } |
239446b4 | 1946 | else // count from the end of the month |
2f02cb89 VZ |
1947 | { |
1948 | // get the last day of the month | |
1949 | dt.SetToLastMonthDay(month, year); | |
1950 | ||
1951 | // get its wday | |
1952 | WeekDay wdayLast = dt.GetWeekDay(); | |
1953 | ||
1954 | // go to the last weekday of the month | |
1955 | int diff = wdayLast - weekday; | |
1956 | if ( diff < 0 ) | |
1957 | diff += 7; | |
1958 | ||
1959 | // and rewind n-1 weeks from there | |
239446b4 | 1960 | diff += 7*(-n - 1); |
2f02cb89 VZ |
1961 | |
1962 | dt -= wxDateSpan::Days(diff); | |
1963 | } | |
1964 | ||
1965 | // check that it is still in the same month | |
1966 | if ( dt.GetMonth() == month ) | |
1967 | { | |
1968 | *this = dt; | |
1969 | ||
68379eaf | 1970 | return true; |
2f02cb89 VZ |
1971 | } |
1972 | else | |
1973 | { | |
1974 | // no such day in this month | |
68379eaf | 1975 | return false; |
2f02cb89 VZ |
1976 | } |
1977 | } | |
1978 | ||
1c5d27e2 VZ |
1979 | static inline |
1980 | wxDateTime::wxDateTime_t GetDayOfYearFromTm(const wxDateTime::Tm& tm) | |
1981 | { | |
907173e5 | 1982 | return (wxDateTime::wxDateTime_t)(gs_cumulatedDays[wxDateTime::IsLeapYear(tm.year)][tm.mon] + tm.mday); |
1c5d27e2 VZ |
1983 | } |
1984 | ||
239446b4 VZ |
1985 | wxDateTime::wxDateTime_t wxDateTime::GetDayOfYear(const TimeZone& tz) const |
1986 | { | |
1c5d27e2 VZ |
1987 | return GetDayOfYearFromTm(GetTm(tz)); |
1988 | } | |
239446b4 | 1989 | |
1c5d27e2 VZ |
1990 | wxDateTime::wxDateTime_t |
1991 | wxDateTime::GetWeekOfYear(wxDateTime::WeekFlags flags, const TimeZone& tz) const | |
239446b4 | 1992 | { |
9d9b7755 VZ |
1993 | if ( flags == Default_First ) |
1994 | { | |
1995 | flags = GetCountry() == USA ? Sunday_First : Monday_First; | |
1996 | } | |
239446b4 | 1997 | |
1c5d27e2 VZ |
1998 | Tm tm(GetTm(tz)); |
1999 | wxDateTime_t nDayInYear = GetDayOfYearFromTm(tm); | |
239446b4 | 2000 | |
1c5d27e2 VZ |
2001 | int wdTarget = GetWeekDay(tz); |
2002 | int wdYearStart = wxDateTime(1, Jan, GetYear()).GetWeekDay(); | |
2003 | int week; | |
9d9b7755 VZ |
2004 | if ( flags == Sunday_First ) |
2005 | { | |
1c5d27e2 VZ |
2006 | // FIXME: First week is not calculated correctly. |
2007 | week = (nDayInYear - wdTarget + 7) / 7; | |
2008 | if ( wdYearStart == Wed || wdYearStart == Thu ) | |
2009 | week++; | |
9d9b7755 | 2010 | } |
1c5d27e2 | 2011 | else // week starts with monday |
9d9b7755 | 2012 | { |
1c5d27e2 VZ |
2013 | // adjust the weekdays to non-US style. |
2014 | wdYearStart = ConvertWeekDayToMondayBase(wdYearStart); | |
2015 | wdTarget = ConvertWeekDayToMondayBase(wdTarget); | |
239446b4 | 2016 | |
1c5d27e2 VZ |
2017 | // quoting from http://www.cl.cam.ac.uk/~mgk25/iso-time.html: |
2018 | // | |
2019 | // Week 01 of a year is per definition the first week that has the | |
2020 | // Thursday in this year, which is equivalent to the week that | |
2021 | // contains the fourth day of January. In other words, the first | |
2022 | // week of a new year is the week that has the majority of its | |
2023 | // days in the new year. Week 01 might also contain days from the | |
2024 | // previous year and the week before week 01 of a year is the last | |
2025 | // week (52 or 53) of the previous year even if it contains days | |
2026 | // from the new year. A week starts with Monday (day 1) and ends | |
2027 | // with Sunday (day 7). | |
2028 | // | |
2029 | ||
2030 | // if Jan 1 is Thursday or less, it is in the first week of this year | |
2031 | if ( wdYearStart < 4 ) | |
2032 | { | |
2033 | // count the number of entire weeks between Jan 1 and this date | |
2034 | week = (nDayInYear + wdYearStart + 6 - wdTarget)/7; | |
2035 | ||
2036 | // be careful to check for overflow in the next year | |
2037 | if ( week == 53 && tm.mday - wdTarget > 28 ) | |
2038 | week = 1; | |
2039 | } | |
2040 | else // Jan 1 is in the last week of the previous year | |
2041 | { | |
2042 | // check if we happen to be at the last week of previous year: | |
2043 | if ( tm.mon == Jan && tm.mday < 8 - wdYearStart ) | |
2044 | week = wxDateTime(31, Dec, GetYear()-1).GetWeekOfYear(); | |
2045 | else | |
2046 | week = (nDayInYear + wdYearStart - 1 - wdTarget)/7; | |
2047 | } | |
239446b4 VZ |
2048 | } |
2049 | ||
907173e5 | 2050 | return (wxDateTime::wxDateTime_t)week; |
68ee7c47 VZ |
2051 | } |
2052 | ||
9d9b7755 VZ |
2053 | wxDateTime::wxDateTime_t wxDateTime::GetWeekOfMonth(wxDateTime::WeekFlags flags, |
2054 | const TimeZone& tz) const | |
68ee7c47 | 2055 | { |
9d9b7755 | 2056 | Tm tm = GetTm(tz); |
404013f8 VZ |
2057 | const wxDateTime dateFirst = wxDateTime(1, tm.mon, tm.year); |
2058 | const wxDateTime::WeekDay wdFirst = dateFirst.GetWeekDay(); | |
2059 | ||
2060 | if ( flags == Default_First ) | |
68ee7c47 | 2061 | { |
404013f8 | 2062 | flags = GetCountry() == USA ? Sunday_First : Monday_First; |
68ee7c47 | 2063 | } |
68ee7c47 | 2064 | |
404013f8 VZ |
2065 | // compute offset of dateFirst from the beginning of the week |
2066 | int firstOffset; | |
2067 | if ( flags == Sunday_First ) | |
2068 | firstOffset = wdFirst - Sun; | |
2069 | else | |
2070 | firstOffset = wdFirst == Sun ? DAYS_PER_WEEK - 1 : wdFirst - Mon; | |
2071 | ||
2072 | return (wxDateTime::wxDateTime_t)((tm.mday - 1 + firstOffset)/7 + 1); | |
239446b4 VZ |
2073 | } |
2074 | ||
f0f951fa VZ |
2075 | wxDateTime& wxDateTime::SetToYearDay(wxDateTime::wxDateTime_t yday) |
2076 | { | |
2077 | int year = GetYear(); | |
384223b3 | 2078 | wxDATETIME_CHECK( (0 < yday) && (yday <= GetNumberOfDays(year)), |
9a83f860 | 2079 | wxT("invalid year day") ); |
f0f951fa VZ |
2080 | |
2081 | bool isLeap = IsLeapYear(year); | |
2082 | for ( Month mon = Jan; mon < Inv_Month; wxNextMonth(mon) ) | |
2083 | { | |
2084 | // for Dec, we can't compare with gs_cumulatedDays[mon + 1], but we | |
2085 | // don't need it neither - because of the CHECK above we know that | |
2086 | // yday lies in December then | |
f49d731f | 2087 | if ( (mon == Dec) || (yday <= gs_cumulatedDays[isLeap][mon + 1]) ) |
f0f951fa | 2088 | { |
42841dfc | 2089 | Set((wxDateTime::wxDateTime_t)(yday - gs_cumulatedDays[isLeap][mon]), mon, year); |
f0f951fa VZ |
2090 | |
2091 | break; | |
2092 | } | |
2093 | } | |
2094 | ||
2095 | return *this; | |
2096 | } | |
2097 | ||
e6ec579c VZ |
2098 | // ---------------------------------------------------------------------------- |
2099 | // Julian day number conversion and related stuff | |
2100 | // ---------------------------------------------------------------------------- | |
2101 | ||
2102 | double wxDateTime::GetJulianDayNumber() const | |
2103 | { | |
d26adb9d | 2104 | return m_time.ToDouble() / MILLISECONDS_PER_DAY + EPOCH_JDN + 0.5; |
e6ec579c VZ |
2105 | } |
2106 | ||
2107 | double wxDateTime::GetRataDie() const | |
2108 | { | |
2109 | // March 1 of the year 0 is Rata Die day -306 and JDN 1721119.5 | |
2110 | return GetJulianDayNumber() - 1721119.5 - 306; | |
2111 | } | |
2112 | ||
fcc3d7cb | 2113 | // ---------------------------------------------------------------------------- |
299fcbfe | 2114 | // timezone and DST stuff |
fcc3d7cb VZ |
2115 | // ---------------------------------------------------------------------------- |
2116 | ||
299fcbfe | 2117 | int wxDateTime::IsDST(wxDateTime::Country country) const |
fcc3d7cb | 2118 | { |
299fcbfe | 2119 | wxCHECK_MSG( country == Country_Default, -1, |
9a83f860 | 2120 | wxT("country support not implemented") ); |
299fcbfe VZ |
2121 | |
2122 | // use the C RTL for the dates in the standard range | |
2123 | time_t timet = GetTicks(); | |
2124 | if ( timet != (time_t)-1 ) | |
2125 | { | |
a452689b SN |
2126 | struct tm tmstruct; |
2127 | tm *tm = wxLocaltime_r(&timet, &tmstruct); | |
299fcbfe | 2128 | |
9a83f860 | 2129 | wxCHECK_MSG( tm, -1, wxT("wxLocaltime_r() failed") ); |
299fcbfe VZ |
2130 | |
2131 | return tm->tm_isdst; | |
2132 | } | |
2133 | else | |
2134 | { | |
239446b4 VZ |
2135 | int year = GetYear(); |
2136 | ||
2137 | if ( !IsDSTApplicable(year, country) ) | |
2138 | { | |
2139 | // no DST time in this year in this country | |
2140 | return -1; | |
2141 | } | |
299fcbfe | 2142 | |
239446b4 | 2143 | return IsBetween(GetBeginDST(year, country), GetEndDST(year, country)); |
299fcbfe | 2144 | } |
fcc3d7cb VZ |
2145 | } |
2146 | ||
41acf5c0 | 2147 | wxDateTime& wxDateTime::MakeTimezone(const TimeZone& tz, bool noDST) |
fcc3d7cb | 2148 | { |
479cd5de | 2149 | long secDiff = GetTimeZone() + tz.GetOffset(); |
fcc3d7cb | 2150 | |
41acf5c0 VZ |
2151 | // we need to know whether DST is or not in effect for this date unless |
2152 | // the test disabled by the caller | |
2153 | if ( !noDST && (IsDST() == 1) ) | |
299fcbfe VZ |
2154 | { |
2155 | // FIXME we assume that the DST is always shifted by 1 hour | |
2156 | secDiff -= 3600; | |
2157 | } | |
2158 | ||
d26adb9d VZ |
2159 | return Add(wxTimeSpan::Seconds(secDiff)); |
2160 | } | |
2161 | ||
2162 | wxDateTime& wxDateTime::MakeFromTimezone(const TimeZone& tz, bool noDST) | |
2163 | { | |
2164 | long secDiff = GetTimeZone() + tz.GetOffset(); | |
2165 | ||
2166 | // we need to know whether DST is or not in effect for this date unless | |
2167 | // the test disabled by the caller | |
2168 | if ( !noDST && (IsDST() == 1) ) | |
2169 | { | |
2170 | // FIXME we assume that the DST is always shifted by 1 hour | |
2171 | secDiff -= 3600; | |
2172 | } | |
2173 | ||
f6bcfd97 | 2174 | return Subtract(wxTimeSpan::Seconds(secDiff)); |
fcc3d7cb VZ |
2175 | } |
2176 | ||
4f6aed9c VZ |
2177 | // ============================================================================ |
2178 | // wxDateTimeHolidayAuthority and related classes | |
2179 | // ============================================================================ | |
2180 | ||
2181 | #include "wx/arrimpl.cpp" | |
2182 | ||
4115960d | 2183 | WX_DEFINE_OBJARRAY(wxDateTimeArray) |
4f6aed9c VZ |
2184 | |
2185 | static int wxCMPFUNC_CONV | |
2186 | wxDateTimeCompareFunc(wxDateTime **first, wxDateTime **second) | |
2187 | { | |
2188 | wxDateTime dt1 = **first, | |
2189 | dt2 = **second; | |
2190 | ||
2191 | return dt1 == dt2 ? 0 : dt1 < dt2 ? -1 : +1; | |
2192 | } | |
2193 | ||
2194 | // ---------------------------------------------------------------------------- | |
2195 | // wxDateTimeHolidayAuthority | |
2196 | // ---------------------------------------------------------------------------- | |
2197 | ||
2198 | wxHolidayAuthoritiesArray wxDateTimeHolidayAuthority::ms_authorities; | |
2199 | ||
2200 | /* static */ | |
2201 | bool wxDateTimeHolidayAuthority::IsHoliday(const wxDateTime& dt) | |
2202 | { | |
df5168c4 | 2203 | size_t count = ms_authorities.size(); |
4f6aed9c VZ |
2204 | for ( size_t n = 0; n < count; n++ ) |
2205 | { | |
2206 | if ( ms_authorities[n]->DoIsHoliday(dt) ) | |
2207 | { | |
68379eaf | 2208 | return true; |
4f6aed9c VZ |
2209 | } |
2210 | } | |
2211 | ||
68379eaf | 2212 | return false; |
4f6aed9c VZ |
2213 | } |
2214 | ||
2215 | /* static */ | |
2216 | size_t | |
2217 | wxDateTimeHolidayAuthority::GetHolidaysInRange(const wxDateTime& dtStart, | |
2218 | const wxDateTime& dtEnd, | |
2219 | wxDateTimeArray& holidays) | |
2220 | { | |
2221 | wxDateTimeArray hol; | |
2222 | ||
df5168c4 | 2223 | holidays.Clear(); |
4f6aed9c | 2224 | |
7a7bd38a VZ |
2225 | const size_t countAuth = ms_authorities.size(); |
2226 | for ( size_t nAuth = 0; nAuth < countAuth; nAuth++ ) | |
4f6aed9c | 2227 | { |
6dc6fda6 | 2228 | ms_authorities[nAuth]->DoGetHolidaysInRange(dtStart, dtEnd, hol); |
4f6aed9c VZ |
2229 | |
2230 | WX_APPEND_ARRAY(holidays, hol); | |
2231 | } | |
2232 | ||
2233 | holidays.Sort(wxDateTimeCompareFunc); | |
2234 | ||
df5168c4 | 2235 | return holidays.size(); |
4f6aed9c VZ |
2236 | } |
2237 | ||
2238 | /* static */ | |
2239 | void wxDateTimeHolidayAuthority::ClearAllAuthorities() | |
2240 | { | |
2241 | WX_CLEAR_ARRAY(ms_authorities); | |
2242 | } | |
2243 | ||
2244 | /* static */ | |
2245 | void wxDateTimeHolidayAuthority::AddAuthority(wxDateTimeHolidayAuthority *auth) | |
2246 | { | |
df5168c4 | 2247 | ms_authorities.push_back(auth); |
4f6aed9c VZ |
2248 | } |
2249 | ||
5e233068 WS |
2250 | wxDateTimeHolidayAuthority::~wxDateTimeHolidayAuthority() |
2251 | { | |
2252 | // required here for Darwin | |
2253 | } | |
2254 | ||
4f6aed9c VZ |
2255 | // ---------------------------------------------------------------------------- |
2256 | // wxDateTimeWorkDays | |
2257 | // ---------------------------------------------------------------------------- | |
2258 | ||
2259 | bool wxDateTimeWorkDays::DoIsHoliday(const wxDateTime& dt) const | |
2260 | { | |
2261 | wxDateTime::WeekDay wd = dt.GetWeekDay(); | |
2262 | ||
2263 | return (wd == wxDateTime::Sun) || (wd == wxDateTime::Sat); | |
2264 | } | |
2265 | ||
2266 | size_t wxDateTimeWorkDays::DoGetHolidaysInRange(const wxDateTime& dtStart, | |
2267 | const wxDateTime& dtEnd, | |
2268 | wxDateTimeArray& holidays) const | |
2269 | { | |
2270 | if ( dtStart > dtEnd ) | |
2271 | { | |
9a83f860 | 2272 | wxFAIL_MSG( wxT("invalid date range in GetHolidaysInRange") ); |
4f6aed9c VZ |
2273 | |
2274 | return 0u; | |
2275 | } | |
2276 | ||
2277 | holidays.Empty(); | |
2278 | ||
2279 | // instead of checking all days, start with the first Sat after dtStart and | |
2280 | // end with the last Sun before dtEnd | |
2281 | wxDateTime dtSatFirst = dtStart.GetNextWeekDay(wxDateTime::Sat), | |
2282 | dtSatLast = dtEnd.GetPrevWeekDay(wxDateTime::Sat), | |
2283 | dtSunFirst = dtStart.GetNextWeekDay(wxDateTime::Sun), | |
2284 | dtSunLast = dtEnd.GetPrevWeekDay(wxDateTime::Sun), | |
2285 | dt; | |
2286 | ||
2287 | for ( dt = dtSatFirst; dt <= dtSatLast; dt += wxDateSpan::Week() ) | |
2288 | { | |
2289 | holidays.Add(dt); | |
2290 | } | |
2291 | ||
2292 | for ( dt = dtSunFirst; dt <= dtSunLast; dt += wxDateSpan::Week() ) | |
2293 | { | |
2294 | holidays.Add(dt); | |
2295 | } | |
2296 | ||
2297 | return holidays.GetCount(); | |
2298 | } | |
3e0b743f | 2299 | |
26364344 WS |
2300 | // ============================================================================ |
2301 | // other helper functions | |
2302 | // ============================================================================ | |
2303 | ||
2304 | // ---------------------------------------------------------------------------- | |
2305 | // iteration helpers: can be used to write a for loop over enum variable like | |
2306 | // this: | |
2307 | // for ( m = wxDateTime::Jan; m < wxDateTime::Inv_Month; wxNextMonth(m) ) | |
2308 | // ---------------------------------------------------------------------------- | |
2309 | ||
2310 | WXDLLIMPEXP_BASE void wxNextMonth(wxDateTime::Month& m) | |
2311 | { | |
9a83f860 | 2312 | wxASSERT_MSG( m < wxDateTime::Inv_Month, wxT("invalid month") ); |
26364344 WS |
2313 | |
2314 | // no wrapping or the for loop above would never end! | |
2315 | m = (wxDateTime::Month)(m + 1); | |
2316 | } | |
2317 | ||
2318 | WXDLLIMPEXP_BASE void wxPrevMonth(wxDateTime::Month& m) | |
2319 | { | |
9a83f860 | 2320 | wxASSERT_MSG( m < wxDateTime::Inv_Month, wxT("invalid month") ); |
26364344 WS |
2321 | |
2322 | m = m == wxDateTime::Jan ? wxDateTime::Inv_Month | |
2323 | : (wxDateTime::Month)(m - 1); | |
2324 | } | |
2325 | ||
2326 | WXDLLIMPEXP_BASE void wxNextWDay(wxDateTime::WeekDay& wd) | |
2327 | { | |
9a83f860 | 2328 | wxASSERT_MSG( wd < wxDateTime::Inv_WeekDay, wxT("invalid week day") ); |
26364344 WS |
2329 | |
2330 | // no wrapping or the for loop above would never end! | |
2331 | wd = (wxDateTime::WeekDay)(wd + 1); | |
2332 | } | |
2333 | ||
2334 | WXDLLIMPEXP_BASE void wxPrevWDay(wxDateTime::WeekDay& wd) | |
2335 | { | |
9a83f860 | 2336 | wxASSERT_MSG( wd < wxDateTime::Inv_WeekDay, wxT("invalid week day") ); |
26364344 WS |
2337 | |
2338 | wd = wd == wxDateTime::Sun ? wxDateTime::Inv_WeekDay | |
2339 | : (wxDateTime::WeekDay)(wd - 1); | |
2340 | } | |
2341 | ||
154014d6 VZ |
2342 | #ifdef __WXMSW__ |
2343 | ||
2344 | wxDateTime& wxDateTime::SetFromMSWSysTime(const SYSTEMTIME& st) | |
2345 | { | |
2346 | return Set(st.wDay, | |
5c33522f | 2347 | static_cast<wxDateTime::Month>(wxDateTime::Jan + st.wMonth - 1), |
154014d6 | 2348 | st.wYear, |
582c3a19 | 2349 | st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); |
154014d6 VZ |
2350 | } |
2351 | ||
10acc3ef VZ |
2352 | wxDateTime& wxDateTime::SetFromMSWSysDate(const SYSTEMTIME& st) |
2353 | { | |
2354 | return Set(st.wDay, | |
2355 | static_cast<wxDateTime::Month>(wxDateTime::Jan + st.wMonth - 1), | |
2356 | st.wYear, | |
2357 | 0, 0, 0, 0); | |
2358 | } | |
2359 | ||
154014d6 VZ |
2360 | void wxDateTime::GetAsMSWSysTime(SYSTEMTIME* st) const |
2361 | { | |
2362 | const wxDateTime::Tm tm(GetTm()); | |
2363 | ||
2364 | st->wYear = (WXWORD)tm.year; | |
2365 | st->wMonth = (WXWORD)(tm.mon - wxDateTime::Jan + 1); | |
2366 | st->wDay = tm.mday; | |
2367 | ||
582c3a19 VZ |
2368 | st->wDayOfWeek = 0; |
2369 | st->wHour = tm.hour; | |
2370 | st->wMinute = tm.min; | |
2371 | st->wSecond = tm.sec; | |
2372 | st->wMilliseconds = tm.msec; | |
154014d6 | 2373 | } |
10acc3ef VZ |
2374 | |
2375 | void wxDateTime::GetAsMSWSysDate(SYSTEMTIME* st) const | |
2376 | { | |
2377 | const wxDateTime::Tm tm(GetTm()); | |
2378 | ||
2379 | st->wYear = (WXWORD)tm.year; | |
2380 | st->wMonth = (WXWORD)(tm.mon - wxDateTime::Jan + 1); | |
2381 | st->wDay = tm.mday; | |
2382 | ||
2383 | st->wDayOfWeek = | |
2384 | st->wHour = | |
2385 | st->wMinute = | |
2386 | st->wSecond = | |
2387 | st->wMilliseconds = 0; | |
2388 | } | |
2389 | ||
154014d6 VZ |
2390 | #endif // __WXMSW__ |
2391 | ||
1e6feb95 | 2392 | #endif // wxUSE_DATETIME |