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