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