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