1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/datetime.cpp
3 // Purpose: implementation of time/date related classes
4 // (for formatting&parsing see datetimefmt.cpp)
5 // Author: Vadim Zeitlin
9 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // parts of code taken from sndcal library by Scott E. Lee:
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.
17 // Licence: wxWindows licence
18 ///////////////////////////////////////////////////////////////////////////////
21 * Implementation notes:
23 * 1. the time is stored as a 64bit integer containing the signed number of
24 * milliseconds since Jan 1. 1970 (the Unix Epoch) - so it is always
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
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
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
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
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
48 // ============================================================================
50 // ============================================================================
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 // For compilers that support precompilation, includes "wx.h".
57 #include "wx/wxprec.h"
63 #if !defined(wxUSE_DATETIME) || wxUSE_DATETIME
67 #include "wx/msw/wrapwin.h"
69 #include "wx/string.h"
72 #include "wx/stopwatch.h" // for wxGetLocalTimeMillis()
73 #include "wx/module.h"
77 #include "wx/thread.h"
78 #include "wx/tokenzr.h"
89 #include "wx/datetime.h"
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
95 #if wxUSE_EXTENDED_RTTI
97 template<> void wxStringReadValue(const wxString
&s
, wxDateTime
&data
)
99 data
.ParseFormat(s
,"%Y-%m-%d %H:%M:%S", NULL
);
102 template<> void wxStringWriteValue(wxString
&s
, const wxDateTime
&data
)
104 s
= data
.Format("%Y-%m-%d %H:%M:%S");
107 wxCUSTOM_TYPE_INFO(wxDateTime
, wxToStringConverter
<wxDateTime
> , wxFromStringConverter
<wxDateTime
>)
109 #endif // wxUSE_EXTENDED_RTTI
112 // ----------------------------------------------------------------------------
113 // conditional compilation
114 // ----------------------------------------------------------------------------
116 #if defined(__MWERKS__) && wxUSE_UNICODE
120 #if defined(__DJGPP__) || defined(__WINE__)
121 #include <sys/timeb.h>
125 #ifndef WX_GMTOFF_IN_TM
126 // Define it for some systems which don't (always) use configure but are
127 // known to have tm_gmtoff field.
128 #if defined(__WXPALMOS__) || defined(__DARWIN__)
129 #define WX_GMTOFF_IN_TM
133 // NB: VC8 safe time functions could/should be used for wxMSW as well probably
134 #if defined(__WXWINCE__) && defined(__VISUALC8__)
136 struct tm
*wxLocaltime_r(const time_t *t
, struct tm
* tm
)
139 return _localtime64_s(tm
, &t64
) == 0 ? tm
: NULL
;
142 struct tm
*wxGmtime_r(const time_t* t
, struct tm
* tm
)
145 return _gmtime64_s(tm
, &t64
) == 0 ? tm
: NULL
;
148 #else // !wxWinCE with VC8
150 #if (!defined(HAVE_LOCALTIME_R) || !defined(HAVE_GMTIME_R)) && wxUSE_THREADS && !defined(__WINDOWS__)
151 static wxMutex timeLock
;
154 #ifndef HAVE_LOCALTIME_R
155 struct tm
*wxLocaltime_r(const time_t* ticks
, struct tm
* temp
)
157 #if wxUSE_THREADS && !defined(__WINDOWS__)
158 // No need to waste time with a mutex on windows since it's using
159 // thread local storage for localtime anyway.
160 wxMutexLocker
locker(timeLock
);
163 // Borland CRT crashes when passed 0 ticks for some reason, see SF bug 1704438
169 const tm
* const t
= localtime(ticks
);
173 memcpy(temp
, t
, sizeof(struct tm
));
176 #endif // !HAVE_LOCALTIME_R
178 #ifndef HAVE_GMTIME_R
179 struct tm
*wxGmtime_r(const time_t* ticks
, struct tm
* temp
)
181 #if wxUSE_THREADS && !defined(__WINDOWS__)
182 // No need to waste time with a mutex on windows since it's
183 // using thread local storage for gmtime anyway.
184 wxMutexLocker
locker(timeLock
);
192 const tm
* const t
= gmtime(ticks
);
196 memcpy(temp
, gmtime(ticks
), sizeof(struct tm
));
199 #endif // !HAVE_GMTIME_R
201 #endif // wxWinCE with VC8/other platforms
203 // ----------------------------------------------------------------------------
205 // ----------------------------------------------------------------------------
207 // debugging helper: just a convenient replacement of wxCHECK()
208 #define wxDATETIME_CHECK(expr, msg) \
209 wxCHECK2_MSG(expr, *this = wxInvalidDateTime; return *this, msg)
211 // ----------------------------------------------------------------------------
213 // ----------------------------------------------------------------------------
215 class wxDateTimeHolidaysModule
: public wxModule
218 virtual bool OnInit()
220 wxDateTimeHolidayAuthority::AddAuthority(new wxDateTimeWorkDays
);
225 virtual void OnExit()
227 wxDateTimeHolidayAuthority::ClearAllAuthorities();
228 wxDateTimeHolidayAuthority::ms_authorities
.clear();
232 DECLARE_DYNAMIC_CLASS(wxDateTimeHolidaysModule
)
235 IMPLEMENT_DYNAMIC_CLASS(wxDateTimeHolidaysModule
, wxModule
)
237 // ----------------------------------------------------------------------------
239 // ----------------------------------------------------------------------------
242 static const int MONTHS_IN_YEAR
= 12;
244 static const int SEC_PER_MIN
= 60;
246 static const int MIN_PER_HOUR
= 60;
248 static const long SECONDS_PER_DAY
= 86400l;
250 static const int DAYS_PER_WEEK
= 7;
252 static const long MILLISECONDS_PER_DAY
= 86400000l;
254 // this is the integral part of JDN of the midnight of Jan 1, 1970
255 // (i.e. JDN(Jan 1, 1970) = 2440587.5)
256 static const long EPOCH_JDN
= 2440587l;
258 // these values are only used in asserts so don't define them if asserts are
259 // disabled to avoid warnings about unused static variables
261 // the date of JDN -0.5 (as we don't work with fractional parts, this is the
262 // reference date for us) is Nov 24, 4714BC
263 static const int JDN_0_YEAR
= -4713;
264 static const int JDN_0_MONTH
= wxDateTime::Nov
;
265 static const int JDN_0_DAY
= 24;
266 #endif // wxDEBUG_LEVEL
268 // the constants used for JDN calculations
269 static const long JDN_OFFSET
= 32046l;
270 static const long DAYS_PER_5_MONTHS
= 153l;
271 static const long DAYS_PER_4_YEARS
= 1461l;
272 static const long DAYS_PER_400_YEARS
= 146097l;
274 // this array contains the cumulated number of days in all previous months for
275 // normal and leap years
276 static const wxDateTime::wxDateTime_t gs_cumulatedDays
[2][MONTHS_IN_YEAR
] =
278 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 },
279 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 }
282 const long wxDateTime::TIME_T_FACTOR
= 1000l;
284 // ----------------------------------------------------------------------------
286 // ----------------------------------------------------------------------------
288 const char wxDefaultDateTimeFormat
[] = "%c";
289 const char wxDefaultTimeSpanFormat
[] = "%H:%M:%S";
291 // in the fine tradition of ANSI C we use our equivalent of (time_t)-1 to
292 // indicate an invalid wxDateTime object
293 const wxDateTime wxDefaultDateTime
;
295 wxDateTime::Country
wxDateTime::ms_country
= wxDateTime::Country_Unknown
;
297 // ----------------------------------------------------------------------------
299 // ----------------------------------------------------------------------------
301 // debugger helper: this function can be called from a debugger to show what
302 // the date really is
303 extern const char *wxDumpDate(const wxDateTime
* dt
)
305 static char buf
[128];
307 wxString
fmt(dt
->Format("%Y-%m-%d (%a) %H:%M:%S"));
309 (fmt
+ " (" + dt
->GetValue().ToString() + " ticks)").ToAscii(),
315 // get the number of days in the given month of the given year
317 wxDateTime::wxDateTime_t
GetNumOfDaysInMonth(int year
, wxDateTime::Month month
)
319 // the number of days in month in Julian/Gregorian calendar: the first line
320 // is for normal years, the second one is for the leap ones
321 static const wxDateTime::wxDateTime_t daysInMonth
[2][MONTHS_IN_YEAR
] =
323 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
324 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
327 return daysInMonth
[wxDateTime::IsLeapYear(year
)][month
];
330 // returns the time zone in the C sense, i.e. the difference UTC - local
332 // NOTE: not static because used by datetimefmt.cpp
335 #ifdef WX_GMTOFF_IN_TM
336 // set to true when the timezone is set
337 static bool s_timezoneSet
= false;
338 static long gmtoffset
= LONG_MAX
; // invalid timezone
340 // ensure that the timezone variable is set by calling wxLocaltime_r
341 if ( !s_timezoneSet
)
343 // just call wxLocaltime_r() instead of figuring out whether this
344 // system supports tzset(), _tzset() or something else
345 time_t t
= time(NULL
);
348 wxLocaltime_r(&t
, &tm
);
349 s_timezoneSet
= true;
351 // note that GMT offset is the opposite of time zone and so to return
352 // consistent results in both WX_GMTOFF_IN_TM and !WX_GMTOFF_IN_TM
353 // cases we have to negate it
354 gmtoffset
= -tm
.tm_gmtoff
;
356 // this function is supposed to return the same value whether DST is
357 // enabled or not, so we need to use an additional offset if DST is on
358 // as tm_gmtoff already does include it
362 return (int)gmtoffset
;
363 #elif defined(__DJGPP__) || defined(__WINE__)
366 return tb
.timezone
*60;
367 #elif defined(__VISUALC__)
368 // We must initialize the time zone information before using it (this will
369 // be done only once internally).
372 // Starting with VC++ 8 timezone variable is deprecated and is not even
373 // available in some standard library version so use the new function for
374 // accessing it instead.
375 #if wxCHECK_VISUALC_VERSION(8)
382 #elif defined(WX_TIMEZONE) // If WX_TIMEZONE was defined by configure, use it.
384 #elif defined(__BORLANDC__) || defined(__MINGW32__) || defined(__VISAGECPP__)
386 #elif defined(__MWERKS__)
388 #else // unknown platform -- assume it has timezone
390 #endif // WX_GMTOFF_IN_TM/!WX_GMTOFF_IN_TM
393 // return the integral part of the JDN for the midnight of the given date (to
394 // get the real JDN you need to add 0.5, this is, in fact, JDN of the
395 // noon of the previous day)
396 static long GetTruncatedJDN(wxDateTime::wxDateTime_t day
,
397 wxDateTime::Month mon
,
400 // CREDIT: code below is by Scott E. Lee (but bugs are mine)
402 // check the date validity
404 (year
> JDN_0_YEAR
) ||
405 ((year
== JDN_0_YEAR
) && (mon
> JDN_0_MONTH
)) ||
406 ((year
== JDN_0_YEAR
) && (mon
== JDN_0_MONTH
) && (day
>= JDN_0_DAY
)),
407 wxT("date out of range - can't convert to JDN")
410 // make the year positive to avoid problems with negative numbers division
413 // months are counted from March here
415 if ( mon
>= wxDateTime::Mar
)
425 // now we can simply add all the contributions together
426 return ((year
/ 100) * DAYS_PER_400_YEARS
) / 4
427 + ((year
% 100) * DAYS_PER_4_YEARS
) / 4
428 + (month
* DAYS_PER_5_MONTHS
+ 2) / 5
433 #ifdef wxHAS_STRFTIME
435 // this function is a wrapper around strftime(3) adding error checking
436 // NOTE: not static because used by datetimefmt.cpp
437 wxString
CallStrftime(const wxString
& format
, const tm
* tm
)
440 // Create temp wxString here to work around mingw/cygwin bug 1046059
441 // http://sourceforge.net/tracker/?func=detail&atid=102435&aid=1046059&group_id=2435
444 if ( !wxStrftime(buf
, WXSIZEOF(buf
), format
, tm
) )
446 // There is one special case in which strftime() can return 0 without
447 // indicating an error: "%p" may give empty string depending on the
448 // locale, so check for it explicitly. Apparently it's really the only
450 if ( format
!= wxS("%p") )
452 // if the format is valid, buffer must be too small?
453 wxFAIL_MSG(wxT("strftime() failed"));
463 #endif // wxHAS_STRFTIME
465 // if year and/or month have invalid values, replace them with the current ones
466 static void ReplaceDefaultYearMonthWithCurrent(int *year
,
467 wxDateTime::Month
*month
)
469 struct tm
*tmNow
= NULL
;
472 if ( *year
== wxDateTime::Inv_Year
)
474 tmNow
= wxDateTime::GetTmNow(&tmstruct
);
476 *year
= 1900 + tmNow
->tm_year
;
479 if ( *month
== wxDateTime::Inv_Month
)
482 tmNow
= wxDateTime::GetTmNow(&tmstruct
);
484 *month
= (wxDateTime::Month
)tmNow
->tm_mon
;
488 // fill the struct tm with default values
489 // NOTE: not static because used by datetimefmt.cpp
490 void InitTm(struct tm
& tm
)
492 // struct tm may have etxra fields (undocumented and with unportable
493 // names) which, nevertheless, must be set to 0
494 memset(&tm
, 0, sizeof(struct tm
));
496 tm
.tm_mday
= 1; // mday 0 is invalid
497 tm
.tm_year
= 76; // any valid year
498 tm
.tm_isdst
= -1; // auto determine
501 // ============================================================================
502 // implementation of wxDateTime
503 // ============================================================================
505 // ----------------------------------------------------------------------------
507 // ----------------------------------------------------------------------------
511 year
= (wxDateTime_t
)wxDateTime::Inv_Year
;
512 mon
= wxDateTime::Inv_Month
;
519 wday
= wxDateTime::Inv_WeekDay
;
522 wxDateTime::Tm::Tm(const struct tm
& tm
, const TimeZone
& tz
)
526 sec
= (wxDateTime::wxDateTime_t
)tm
.tm_sec
;
527 min
= (wxDateTime::wxDateTime_t
)tm
.tm_min
;
528 hour
= (wxDateTime::wxDateTime_t
)tm
.tm_hour
;
529 mday
= (wxDateTime::wxDateTime_t
)tm
.tm_mday
;
530 mon
= (wxDateTime::Month
)tm
.tm_mon
;
531 year
= 1900 + tm
.tm_year
;
532 wday
= (wxDateTime::wxDateTime_t
)tm
.tm_wday
;
533 yday
= (wxDateTime::wxDateTime_t
)tm
.tm_yday
;
536 bool wxDateTime::Tm::IsValid() const
538 if ( mon
== wxDateTime::Inv_Month
)
541 // We need to check this here to avoid crashing in GetNumOfDaysInMonth() if
542 // somebody passed us "(wxDateTime::Month)1000".
543 wxCHECK_MSG( mon
>= wxDateTime::Jan
&& mon
< wxDateTime::Inv_Month
, false,
544 wxS("Invalid month value") );
546 // we allow for the leap seconds, although we don't use them (yet)
547 return (year
!= wxDateTime::Inv_Year
) && (mon
!= wxDateTime::Inv_Month
) &&
548 (mday
> 0 && mday
<= GetNumOfDaysInMonth(year
, mon
)) &&
549 (hour
< 24) && (min
< 60) && (sec
< 62) && (msec
< 1000);
552 void wxDateTime::Tm::ComputeWeekDay()
554 // compute the week day from day/month/year: we use the dumbest algorithm
555 // possible: just compute our JDN and then use the (simple to derive)
556 // formula: weekday = (JDN + 1.5) % 7
557 wday
= (wxDateTime::wxDateTime_t
)((GetTruncatedJDN(mday
, mon
, year
) + 2) % 7);
560 void wxDateTime::Tm::AddMonths(int monDiff
)
562 // normalize the months field
563 while ( monDiff
< -mon
)
567 monDiff
+= MONTHS_IN_YEAR
;
570 while ( monDiff
+ mon
>= MONTHS_IN_YEAR
)
574 monDiff
-= MONTHS_IN_YEAR
;
577 mon
= (wxDateTime::Month
)(mon
+ monDiff
);
579 wxASSERT_MSG( mon
>= 0 && mon
< MONTHS_IN_YEAR
, wxT("logic error") );
581 // NB: we don't check here that the resulting date is valid, this function
582 // is private and the caller must check it if needed
585 void wxDateTime::Tm::AddDays(int dayDiff
)
587 // normalize the days field
588 while ( dayDiff
+ mday
< 1 )
592 dayDiff
+= GetNumOfDaysInMonth(year
, mon
);
595 mday
= (wxDateTime::wxDateTime_t
)( mday
+ dayDiff
);
596 while ( mday
> GetNumOfDaysInMonth(year
, mon
) )
598 mday
-= GetNumOfDaysInMonth(year
, mon
);
603 wxASSERT_MSG( mday
> 0 && mday
<= GetNumOfDaysInMonth(year
, mon
),
604 wxT("logic error") );
607 // ----------------------------------------------------------------------------
609 // ----------------------------------------------------------------------------
611 wxDateTime::TimeZone::TimeZone(wxDateTime::TZ tz
)
615 case wxDateTime::Local
:
616 // get the offset from C RTL: it returns the difference GMT-local
617 // while we want to have the offset _from_ GMT, hence the '-'
618 m_offset
= -GetTimeZone();
621 case wxDateTime::GMT_12
:
622 case wxDateTime::GMT_11
:
623 case wxDateTime::GMT_10
:
624 case wxDateTime::GMT_9
:
625 case wxDateTime::GMT_8
:
626 case wxDateTime::GMT_7
:
627 case wxDateTime::GMT_6
:
628 case wxDateTime::GMT_5
:
629 case wxDateTime::GMT_4
:
630 case wxDateTime::GMT_3
:
631 case wxDateTime::GMT_2
:
632 case wxDateTime::GMT_1
:
633 m_offset
= -3600*(wxDateTime::GMT0
- tz
);
636 case wxDateTime::GMT0
:
637 case wxDateTime::GMT1
:
638 case wxDateTime::GMT2
:
639 case wxDateTime::GMT3
:
640 case wxDateTime::GMT4
:
641 case wxDateTime::GMT5
:
642 case wxDateTime::GMT6
:
643 case wxDateTime::GMT7
:
644 case wxDateTime::GMT8
:
645 case wxDateTime::GMT9
:
646 case wxDateTime::GMT10
:
647 case wxDateTime::GMT11
:
648 case wxDateTime::GMT12
:
649 case wxDateTime::GMT13
:
650 m_offset
= 3600*(tz
- wxDateTime::GMT0
);
653 case wxDateTime::A_CST
:
654 // Central Standard Time in use in Australia = UTC + 9.5
655 m_offset
= 60l*(9*MIN_PER_HOUR
+ MIN_PER_HOUR
/2);
659 wxFAIL_MSG( wxT("unknown time zone") );
663 // ----------------------------------------------------------------------------
665 // ----------------------------------------------------------------------------
668 struct tm
*wxDateTime::GetTmNow(struct tm
*tmstruct
)
670 time_t t
= GetTimeNow();
671 return wxLocaltime_r(&t
, tmstruct
);
675 bool wxDateTime::IsLeapYear(int year
, wxDateTime::Calendar cal
)
677 if ( year
== Inv_Year
)
678 year
= GetCurrentYear();
680 if ( cal
== Gregorian
)
682 // in Gregorian calendar leap years are those divisible by 4 except
683 // those divisible by 100 unless they're also divisible by 400
684 // (in some countries, like Russia and Greece, additional corrections
685 // exist, but they won't manifest themselves until 2700)
686 return (year
% 4 == 0) && ((year
% 100 != 0) || (year
% 400 == 0));
688 else if ( cal
== Julian
)
690 // in Julian calendar the rule is simpler
691 return year
% 4 == 0;
695 wxFAIL_MSG(wxT("unknown calendar"));
702 int wxDateTime::GetCentury(int year
)
704 return year
> 0 ? year
/ 100 : year
/ 100 - 1;
708 int wxDateTime::ConvertYearToBC(int year
)
711 return year
> 0 ? year
: year
- 1;
715 int wxDateTime::GetCurrentYear(wxDateTime::Calendar cal
)
720 return Now().GetYear();
723 wxFAIL_MSG(wxT("TODO"));
727 wxFAIL_MSG(wxT("unsupported calendar"));
735 wxDateTime::Month
wxDateTime::GetCurrentMonth(wxDateTime::Calendar cal
)
740 return Now().GetMonth();
743 wxFAIL_MSG(wxT("TODO"));
747 wxFAIL_MSG(wxT("unsupported calendar"));
755 wxDateTime::wxDateTime_t
wxDateTime::GetNumberOfDays(int year
, Calendar cal
)
757 if ( year
== Inv_Year
)
759 // take the current year if none given
760 year
= GetCurrentYear();
767 return IsLeapYear(year
) ? 366 : 365;
770 wxFAIL_MSG(wxT("unsupported calendar"));
778 wxDateTime::wxDateTime_t
wxDateTime::GetNumberOfDays(wxDateTime::Month month
,
780 wxDateTime::Calendar cal
)
782 wxCHECK_MSG( month
< MONTHS_IN_YEAR
, 0, wxT("invalid month") );
784 if ( cal
== Gregorian
|| cal
== Julian
)
786 if ( year
== Inv_Year
)
788 // take the current year if none given
789 year
= GetCurrentYear();
792 return GetNumOfDaysInMonth(year
, month
);
796 wxFAIL_MSG(wxT("unsupported calendar"));
805 // helper function used by GetEnglish/WeekDayName(): returns 0 if flags is
806 // Name_Full and 1 if it is Name_Abbr or -1 if the flags is incorrect (and
807 // asserts in this case)
809 // the return value of this function is used as an index into 2D array
810 // containing full names in its first row and abbreviated ones in the 2nd one
811 int NameArrayIndexFromFlag(wxDateTime::NameFlags flags
)
815 case wxDateTime::Name_Full
:
818 case wxDateTime::Name_Abbr
:
822 wxFAIL_MSG( "unknown wxDateTime::NameFlags value" );
828 } // anonymous namespace
831 wxString
wxDateTime::GetEnglishMonthName(Month month
, NameFlags flags
)
833 wxCHECK_MSG( month
!= Inv_Month
, wxEmptyString
, "invalid month" );
835 static const char *const monthNames
[2][MONTHS_IN_YEAR
] =
837 { "January", "February", "March", "April", "May", "June",
838 "July", "August", "September", "October", "November", "December" },
839 { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
840 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }
843 const int idx
= NameArrayIndexFromFlag(flags
);
847 return monthNames
[idx
][month
];
851 wxString
wxDateTime::GetMonthName(wxDateTime::Month month
,
852 wxDateTime::NameFlags flags
)
854 #ifdef wxHAS_STRFTIME
855 wxCHECK_MSG( month
!= Inv_Month
, wxEmptyString
, wxT("invalid month") );
857 // notice that we must set all the fields to avoid confusing libc (GNU one
858 // gets confused to a crash if we don't do this)
863 return CallStrftime(flags
== Name_Abbr
? wxT("%b") : wxT("%B"), &tm
);
864 #else // !wxHAS_STRFTIME
865 return GetEnglishMonthName(month
, flags
);
866 #endif // wxHAS_STRFTIME/!wxHAS_STRFTIME
870 wxString
wxDateTime::GetEnglishWeekDayName(WeekDay wday
, NameFlags flags
)
872 wxCHECK_MSG( wday
!= Inv_WeekDay
, wxEmptyString
, wxT("invalid weekday") );
874 static const char *const weekdayNames
[2][DAYS_PER_WEEK
] =
876 { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
878 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" },
881 const int idx
= NameArrayIndexFromFlag(flags
);
885 return weekdayNames
[idx
][wday
];
889 wxString
wxDateTime::GetWeekDayName(wxDateTime::WeekDay wday
,
890 wxDateTime::NameFlags flags
)
892 #ifdef wxHAS_STRFTIME
893 wxCHECK_MSG( wday
!= Inv_WeekDay
, wxEmptyString
, wxT("invalid weekday") );
895 // take some arbitrary Sunday (but notice that the day should be such that
896 // after adding wday to it below we still have a valid date, e.g. don't
904 // and offset it by the number of days needed to get the correct wday
907 // call mktime() to normalize it...
910 // ... and call strftime()
911 return CallStrftime(flags
== Name_Abbr
? wxT("%a") : wxT("%A"), &tm
);
912 #else // !wxHAS_STRFTIME
913 return GetEnglishWeekDayName(wday
, flags
);
914 #endif // wxHAS_STRFTIME/!wxHAS_STRFTIME
918 void wxDateTime::GetAmPmStrings(wxString
*am
, wxString
*pm
)
923 // @Note: Do not call 'CallStrftime' here! CallStrftime checks the return code
924 // and causes an assertion failed if the buffer is to small (which is good) - OR -
925 // if strftime does not return anything because the format string is invalid - OR -
926 // if there are no 'am' / 'pm' tokens defined for the current locale (which is not good).
927 // wxDateTime::ParseTime will try several different formats to parse the time.
928 // As a result, GetAmPmStrings might get called, even if the current locale
929 // does not define any 'am' / 'pm' tokens. In this case, wxStrftime would
930 // assert, even though it is a perfectly legal use.
933 if (wxStrftime(buffer
, WXSIZEOF(buffer
), wxT("%p"), &tm
) > 0)
934 *am
= wxString(buffer
);
941 if (wxStrftime(buffer
, WXSIZEOF(buffer
), wxT("%p"), &tm
) > 0)
942 *pm
= wxString(buffer
);
949 // ----------------------------------------------------------------------------
950 // Country stuff: date calculations depend on the country (DST, work days,
951 // ...), so we need to know which rules to follow.
952 // ----------------------------------------------------------------------------
955 wxDateTime::Country
wxDateTime::GetCountry()
957 // TODO use LOCALE_ICOUNTRY setting under Win32
959 if ( ms_country
== Country_Unknown
)
961 // try to guess from the time zone name
962 time_t t
= time(NULL
);
964 struct tm
*tm
= wxLocaltime_r(&t
, &tmstruct
);
966 wxString tz
= CallStrftime(wxT("%Z"), tm
);
967 if ( tz
== wxT("WET") || tz
== wxT("WEST") )
971 else if ( tz
== wxT("CET") || tz
== wxT("CEST") )
973 ms_country
= Country_EEC
;
975 else if ( tz
== wxT("MSK") || tz
== wxT("MSD") )
979 else if ( tz
== wxT("AST") || tz
== wxT("ADT") ||
980 tz
== wxT("EST") || tz
== wxT("EDT") ||
981 tz
== wxT("CST") || tz
== wxT("CDT") ||
982 tz
== wxT("MST") || tz
== wxT("MDT") ||
983 tz
== wxT("PST") || tz
== wxT("PDT") )
989 // well, choose a default one
995 #endif // !__WXWINCE__/__WXWINCE__
1001 void wxDateTime::SetCountry(wxDateTime::Country country
)
1003 ms_country
= country
;
1007 bool wxDateTime::IsWestEuropeanCountry(Country country
)
1009 if ( country
== Country_Default
)
1011 country
= GetCountry();
1014 return (Country_WesternEurope_Start
<= country
) &&
1015 (country
<= Country_WesternEurope_End
);
1018 // ----------------------------------------------------------------------------
1019 // DST calculations: we use 3 different rules for the West European countries,
1020 // USA and for the rest of the world. This is undoubtedly false for many
1021 // countries, but I lack the necessary info (and the time to gather it),
1022 // please add the other rules here!
1023 // ----------------------------------------------------------------------------
1026 bool wxDateTime::IsDSTApplicable(int year
, Country country
)
1028 if ( year
== Inv_Year
)
1030 // take the current year if none given
1031 year
= GetCurrentYear();
1034 if ( country
== Country_Default
)
1036 country
= GetCountry();
1043 // DST was first observed in the US and UK during WWI, reused
1044 // during WWII and used again since 1966
1045 return year
>= 1966 ||
1046 (year
>= 1942 && year
<= 1945) ||
1047 (year
== 1918 || year
== 1919);
1050 // assume that it started after WWII
1056 wxDateTime
wxDateTime::GetBeginDST(int year
, Country country
)
1058 if ( year
== Inv_Year
)
1060 // take the current year if none given
1061 year
= GetCurrentYear();
1064 if ( country
== Country_Default
)
1066 country
= GetCountry();
1069 if ( !IsDSTApplicable(year
, country
) )
1071 return wxInvalidDateTime
;
1076 if ( IsWestEuropeanCountry(country
) || (country
== Russia
) )
1078 // DST begins at 1 a.m. GMT on the last Sunday of March
1079 if ( !dt
.SetToLastWeekDay(Sun
, Mar
, year
) )
1082 wxFAIL_MSG( wxT("no last Sunday in March?") );
1085 dt
+= wxTimeSpan::Hours(1);
1087 else switch ( country
)
1094 // don't know for sure - assume it was in effect all year
1099 dt
.Set(1, Jan
, year
);
1103 // DST was installed Feb 2, 1942 by the Congress
1104 dt
.Set(2, Feb
, year
);
1107 // Oil embargo changed the DST period in the US
1109 dt
.Set(6, Jan
, 1974);
1113 dt
.Set(23, Feb
, 1975);
1117 // before 1986, DST begun on the last Sunday of April, but
1118 // in 1986 Reagan changed it to begin at 2 a.m. of the
1119 // first Sunday in April
1122 if ( !dt
.SetToLastWeekDay(Sun
, Apr
, year
) )
1125 wxFAIL_MSG( wxT("no first Sunday in April?") );
1128 else if ( year
> 2006 )
1129 // Energy Policy Act of 2005, Pub. L. no. 109-58, 119 Stat 594 (2005).
1130 // Starting in 2007, daylight time begins in the United States on the
1131 // second Sunday in March and ends on the first Sunday in November
1133 if ( !dt
.SetToWeekDay(Sun
, 2, Mar
, year
) )
1136 wxFAIL_MSG( wxT("no second Sunday in March?") );
1141 if ( !dt
.SetToWeekDay(Sun
, 1, Apr
, year
) )
1144 wxFAIL_MSG( wxT("no first Sunday in April?") );
1148 dt
+= wxTimeSpan::Hours(2);
1150 // TODO what about timezone??
1156 // assume Mar 30 as the start of the DST for the rest of the world
1157 // - totally bogus, of course
1158 dt
.Set(30, Mar
, year
);
1165 wxDateTime
wxDateTime::GetEndDST(int year
, Country country
)
1167 if ( year
== Inv_Year
)
1169 // take the current year if none given
1170 year
= GetCurrentYear();
1173 if ( country
== Country_Default
)
1175 country
= GetCountry();
1178 if ( !IsDSTApplicable(year
, country
) )
1180 return wxInvalidDateTime
;
1185 if ( IsWestEuropeanCountry(country
) || (country
== Russia
) )
1187 // DST ends at 1 a.m. GMT on the last Sunday of October
1188 if ( !dt
.SetToLastWeekDay(Sun
, Oct
, year
) )
1190 // weirder and weirder...
1191 wxFAIL_MSG( wxT("no last Sunday in October?") );
1194 dt
+= wxTimeSpan::Hours(1);
1196 else switch ( country
)
1203 // don't know for sure - assume it was in effect all year
1207 dt
.Set(31, Dec
, year
);
1211 // the time was reset after the end of the WWII
1212 dt
.Set(30, Sep
, year
);
1215 default: // default for switch (year)
1217 // Energy Policy Act of 2005, Pub. L. no. 109-58, 119 Stat 594 (2005).
1218 // Starting in 2007, daylight time begins in the United States on the
1219 // second Sunday in March and ends on the first Sunday in November
1221 if ( !dt
.SetToWeekDay(Sun
, 1, Nov
, year
) )
1224 wxFAIL_MSG( wxT("no first Sunday in November?") );
1229 // DST ends at 2 a.m. on the last Sunday of October
1231 if ( !dt
.SetToLastWeekDay(Sun
, Oct
, year
) )
1233 // weirder and weirder...
1234 wxFAIL_MSG( wxT("no last Sunday in October?") );
1238 dt
+= wxTimeSpan::Hours(2);
1240 // TODO: what about timezone??
1244 default: // default for switch (country)
1245 // assume October 26th as the end of the DST - totally bogus too
1246 dt
.Set(26, Oct
, year
);
1252 // ----------------------------------------------------------------------------
1253 // constructors and assignment operators
1254 // ----------------------------------------------------------------------------
1256 // return the current time with ms precision
1257 /* static */ wxDateTime
wxDateTime::UNow()
1259 return wxDateTime(wxGetLocalTimeMillis());
1262 // the values in the tm structure contain the local time
1263 wxDateTime
& wxDateTime::Set(const struct tm
& tm
)
1266 time_t timet
= mktime(&tm2
);
1268 if ( timet
== (time_t)-1 )
1270 // mktime() rather unintuitively fails for Jan 1, 1970 if the hour is
1271 // less than timezone - try to make it work for this case
1272 if ( tm2
.tm_year
== 70 && tm2
.tm_mon
== 0 && tm2
.tm_mday
== 1 )
1274 return Set((time_t)(
1276 tm2
.tm_hour
* MIN_PER_HOUR
* SEC_PER_MIN
+
1277 tm2
.tm_min
* SEC_PER_MIN
+
1281 wxFAIL_MSG( wxT("mktime() failed") );
1283 *this = wxInvalidDateTime
;
1293 wxDateTime
& wxDateTime::Set(wxDateTime_t hour
,
1294 wxDateTime_t minute
,
1295 wxDateTime_t second
,
1296 wxDateTime_t millisec
)
1298 // we allow seconds to be 61 to account for the leap seconds, even if we
1299 // don't use them really
1300 wxDATETIME_CHECK( hour
< 24 &&
1304 wxT("Invalid time in wxDateTime::Set()") );
1306 // get the current date from system
1308 struct tm
*tm
= GetTmNow(&tmstruct
);
1310 wxDATETIME_CHECK( tm
, wxT("wxLocaltime_r() failed") );
1312 // make a copy so it isn't clobbered by the call to mktime() below
1317 tm1
.tm_min
= minute
;
1318 tm1
.tm_sec
= second
;
1320 // and the DST in case it changes on this date
1323 if ( tm2
.tm_isdst
!= tm1
.tm_isdst
)
1324 tm1
.tm_isdst
= tm2
.tm_isdst
;
1328 // and finally adjust milliseconds
1329 return SetMillisecond(millisec
);
1332 wxDateTime
& wxDateTime::Set(wxDateTime_t day
,
1336 wxDateTime_t minute
,
1337 wxDateTime_t second
,
1338 wxDateTime_t millisec
)
1340 wxDATETIME_CHECK( hour
< 24 &&
1344 wxT("Invalid time in wxDateTime::Set()") );
1346 ReplaceDefaultYearMonthWithCurrent(&year
, &month
);
1348 wxDATETIME_CHECK( (0 < day
) && (day
<= GetNumberOfDays(month
, year
)),
1349 wxT("Invalid date in wxDateTime::Set()") );
1351 // the range of time_t type (inclusive)
1352 static const int yearMinInRange
= 1970;
1353 static const int yearMaxInRange
= 2037;
1355 // test only the year instead of testing for the exact end of the Unix
1356 // time_t range - it doesn't bring anything to do more precise checks
1357 if ( year
>= yearMinInRange
&& year
<= yearMaxInRange
)
1359 // use the standard library version if the date is in range - this is
1360 // probably more efficient than our code
1362 tm
.tm_year
= year
- 1900;
1368 tm
.tm_isdst
= -1; // mktime() will guess it
1372 // and finally adjust milliseconds
1374 SetMillisecond(millisec
);
1380 // do time calculations ourselves: we want to calculate the number of
1381 // milliseconds between the given date and the epoch
1383 // get the JDN for the midnight of this day
1384 m_time
= GetTruncatedJDN(day
, month
, year
);
1385 m_time
-= EPOCH_JDN
;
1386 m_time
*= SECONDS_PER_DAY
* TIME_T_FACTOR
;
1388 // JDN corresponds to GMT, we take localtime
1389 Add(wxTimeSpan(hour
, minute
, second
+ GetTimeZone(), millisec
));
1395 wxDateTime
& wxDateTime::Set(double jdn
)
1397 // so that m_time will be 0 for the midnight of Jan 1, 1970 which is jdn
1399 jdn
-= EPOCH_JDN
+ 0.5;
1401 m_time
.Assign(jdn
*MILLISECONDS_PER_DAY
);
1403 // JDNs always are in UTC, so we don't need any adjustments for time zone
1408 wxDateTime
& wxDateTime::ResetTime()
1412 if ( tm
.hour
|| tm
.min
|| tm
.sec
|| tm
.msec
)
1425 wxDateTime
wxDateTime::GetDateOnly() const
1432 return wxDateTime(tm
);
1435 // ----------------------------------------------------------------------------
1436 // DOS Date and Time Format functions
1437 // ----------------------------------------------------------------------------
1438 // the dos date and time value is an unsigned 32 bit value in the format:
1439 // YYYYYYYMMMMDDDDDhhhhhmmmmmmsssss
1441 // Y = year offset from 1980 (0-127)
1443 // D = day of month (1-31)
1445 // m = minute (0-59)
1446 // s = bisecond (0-29) each bisecond indicates two seconds
1447 // ----------------------------------------------------------------------------
1449 wxDateTime
& wxDateTime::SetFromDOS(unsigned long ddt
)
1454 long year
= ddt
& 0xFE000000;
1459 long month
= ddt
& 0x1E00000;
1464 long day
= ddt
& 0x1F0000;
1468 long hour
= ddt
& 0xF800;
1472 long minute
= ddt
& 0x7E0;
1476 long second
= ddt
& 0x1F;
1477 tm
.tm_sec
= second
* 2;
1479 return Set(mktime(&tm
));
1482 unsigned long wxDateTime::GetAsDOS() const
1485 time_t ticks
= GetTicks();
1487 struct tm
*tm
= wxLocaltime_r(&ticks
, &tmstruct
);
1488 wxCHECK_MSG( tm
, ULONG_MAX
, wxT("time can't be represented in DOS format") );
1490 long year
= tm
->tm_year
;
1494 long month
= tm
->tm_mon
;
1498 long day
= tm
->tm_mday
;
1501 long hour
= tm
->tm_hour
;
1504 long minute
= tm
->tm_min
;
1507 long second
= tm
->tm_sec
;
1510 ddt
= year
| month
| day
| hour
| minute
| second
;
1514 // ----------------------------------------------------------------------------
1515 // time_t <-> broken down time conversions
1516 // ----------------------------------------------------------------------------
1518 wxDateTime::Tm
wxDateTime::GetTm(const TimeZone
& tz
) const
1520 wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime") );
1522 time_t time
= GetTicks();
1523 if ( time
!= (time_t)-1 )
1525 // use C RTL functions
1528 if ( tz
.GetOffset() == -GetTimeZone() )
1530 // we are working with local time
1531 tm
= wxLocaltime_r(&time
, &tmstruct
);
1533 // should never happen
1534 wxCHECK_MSG( tm
, Tm(), wxT("wxLocaltime_r() failed") );
1538 time
+= (time_t)tz
.GetOffset();
1539 #if defined(__VMS__) || defined(__WATCOMC__) // time is unsigned so avoid warning
1540 int time2
= (int) time
;
1546 tm
= wxGmtime_r(&time
, &tmstruct
);
1548 // should never happen
1549 wxCHECK_MSG( tm
, Tm(), wxT("wxGmtime_r() failed") );
1553 tm
= (struct tm
*)NULL
;
1559 // adjust the milliseconds
1561 long timeOnly
= (m_time
% MILLISECONDS_PER_DAY
).ToLong();
1562 tm2
.msec
= (wxDateTime_t
)(timeOnly
% 1000);
1565 //else: use generic code below
1568 // remember the time and do the calculations with the date only - this
1569 // eliminates rounding errors of the floating point arithmetics
1571 wxLongLong timeMidnight
= m_time
+ tz
.GetOffset() * 1000;
1573 long timeOnly
= (timeMidnight
% MILLISECONDS_PER_DAY
).ToLong();
1575 // we want to always have positive time and timeMidnight to be really
1576 // the midnight before it
1579 timeOnly
= MILLISECONDS_PER_DAY
+ timeOnly
;
1582 timeMidnight
-= timeOnly
;
1584 // calculate the Gregorian date from JDN for the midnight of our date:
1585 // this will yield day, month (in 1..12 range) and year
1587 // actually, this is the JDN for the noon of the previous day
1588 long jdn
= (timeMidnight
/ MILLISECONDS_PER_DAY
).ToLong() + EPOCH_JDN
;
1590 // CREDIT: code below is by Scott E. Lee (but bugs are mine)
1592 wxASSERT_MSG( jdn
> -2, wxT("JDN out of range") );
1594 // calculate the century
1595 long temp
= (jdn
+ JDN_OFFSET
) * 4 - 1;
1596 long century
= temp
/ DAYS_PER_400_YEARS
;
1598 // then the year and day of year (1 <= dayOfYear <= 366)
1599 temp
= ((temp
% DAYS_PER_400_YEARS
) / 4) * 4 + 3;
1600 long year
= (century
* 100) + (temp
/ DAYS_PER_4_YEARS
);
1601 long dayOfYear
= (temp
% DAYS_PER_4_YEARS
) / 4 + 1;
1603 // and finally the month and day of the month
1604 temp
= dayOfYear
* 5 - 3;
1605 long month
= temp
/ DAYS_PER_5_MONTHS
;
1606 long day
= (temp
% DAYS_PER_5_MONTHS
) / 5 + 1;
1608 // month is counted from March - convert to normal
1619 // year is offset by 4800
1622 // check that the algorithm gave us something reasonable
1623 wxASSERT_MSG( (0 < month
) && (month
<= 12), wxT("invalid month") );
1624 wxASSERT_MSG( (1 <= day
) && (day
< 32), wxT("invalid day") );
1626 // construct Tm from these values
1628 tm
.year
= (int)year
;
1629 tm
.yday
= (wxDateTime_t
)(dayOfYear
- 1); // use C convention for day number
1630 tm
.mon
= (Month
)(month
- 1); // algorithm yields 1 for January, not 0
1631 tm
.mday
= (wxDateTime_t
)day
;
1632 tm
.msec
= (wxDateTime_t
)(timeOnly
% 1000);
1633 timeOnly
-= tm
.msec
;
1634 timeOnly
/= 1000; // now we have time in seconds
1636 tm
.sec
= (wxDateTime_t
)(timeOnly
% SEC_PER_MIN
);
1638 timeOnly
/= SEC_PER_MIN
; // now we have time in minutes
1640 tm
.min
= (wxDateTime_t
)(timeOnly
% MIN_PER_HOUR
);
1643 tm
.hour
= (wxDateTime_t
)(timeOnly
/ MIN_PER_HOUR
);
1648 wxDateTime
& wxDateTime::SetYear(int year
)
1650 wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime") );
1659 wxDateTime
& wxDateTime::SetMonth(Month month
)
1661 wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime") );
1670 wxDateTime
& wxDateTime::SetDay(wxDateTime_t mday
)
1672 wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime") );
1681 wxDateTime
& wxDateTime::SetHour(wxDateTime_t hour
)
1683 wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime") );
1692 wxDateTime
& wxDateTime::SetMinute(wxDateTime_t min
)
1694 wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime") );
1703 wxDateTime
& wxDateTime::SetSecond(wxDateTime_t sec
)
1705 wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime") );
1714 wxDateTime
& wxDateTime::SetMillisecond(wxDateTime_t millisecond
)
1716 wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime") );
1718 // we don't need to use GetTm() for this one
1719 m_time
-= m_time
% 1000l;
1720 m_time
+= millisecond
;
1725 // ----------------------------------------------------------------------------
1726 // wxDateTime arithmetics
1727 // ----------------------------------------------------------------------------
1729 wxDateTime
& wxDateTime::Add(const wxDateSpan
& diff
)
1733 tm
.year
+= diff
.GetYears();
1734 tm
.AddMonths(diff
.GetMonths());
1736 // check that the resulting date is valid
1737 if ( tm
.mday
> GetNumOfDaysInMonth(tm
.year
, tm
.mon
) )
1739 // We suppose that when adding one month to Jan 31 we want to get Feb
1740 // 28 (or 29), i.e. adding a month to the last day of the month should
1741 // give the last day of the next month which is quite logical.
1743 // Unfortunately, there is no logic way to understand what should
1744 // Jan 30 + 1 month be - Feb 28 too or Feb 27 (assuming non leap year)?
1745 // We make it Feb 28 (last day too), but it is highly questionable.
1746 tm
.mday
= GetNumOfDaysInMonth(tm
.year
, tm
.mon
);
1749 tm
.AddDays(diff
.GetTotalDays());
1753 wxASSERT_MSG( IsSameTime(tm
),
1754 wxT("Add(wxDateSpan) shouldn't modify time") );
1759 // ----------------------------------------------------------------------------
1760 // Weekday and monthday stuff
1761 // ----------------------------------------------------------------------------
1763 // convert Sun, Mon, ..., Sat into 6, 0, ..., 5
1764 static inline int ConvertWeekDayToMondayBase(int wd
)
1766 return wd
== wxDateTime::Sun
? 6 : wd
- 1;
1771 wxDateTime::SetToWeekOfYear(int year
, wxDateTime_t numWeek
, WeekDay wd
)
1773 wxASSERT_MSG( numWeek
> 0,
1774 wxT("invalid week number: weeks are counted from 1") );
1776 // Jan 4 always lies in the 1st week of the year
1777 wxDateTime
dt(4, Jan
, year
);
1778 dt
.SetToWeekDayInSameWeek(wd
);
1779 dt
+= wxDateSpan::Weeks(numWeek
- 1);
1784 #if WXWIN_COMPATIBILITY_2_6
1785 // use a separate function to avoid warnings about using deprecated
1786 // SetToTheWeek in GetWeek below
1788 SetToTheWeek(int year
,
1789 wxDateTime::wxDateTime_t numWeek
,
1790 wxDateTime::WeekDay weekday
,
1791 wxDateTime::WeekFlags flags
)
1793 // Jan 4 always lies in the 1st week of the year
1794 wxDateTime
dt(4, wxDateTime::Jan
, year
);
1795 dt
.SetToWeekDayInSameWeek(weekday
, flags
);
1796 dt
+= wxDateSpan::Weeks(numWeek
- 1);
1801 bool wxDateTime::SetToTheWeek(wxDateTime_t numWeek
,
1805 int year
= GetYear();
1806 *this = ::SetToTheWeek(year
, numWeek
, weekday
, flags
);
1807 if ( GetYear() != year
)
1809 // oops... numWeek was too big
1816 wxDateTime
wxDateTime::GetWeek(wxDateTime_t numWeek
,
1818 WeekFlags flags
) const
1820 return ::SetToTheWeek(GetYear(), numWeek
, weekday
, flags
);
1822 #endif // WXWIN_COMPATIBILITY_2_6
1824 wxDateTime
& wxDateTime::SetToLastMonthDay(Month month
,
1827 // take the current month/year if none specified
1828 if ( year
== Inv_Year
)
1830 if ( month
== Inv_Month
)
1833 return Set(GetNumOfDaysInMonth(year
, month
), month
, year
);
1836 wxDateTime
& wxDateTime::SetToWeekDayInSameWeek(WeekDay weekday
, WeekFlags flags
)
1838 wxDATETIME_CHECK( weekday
!= Inv_WeekDay
, wxT("invalid weekday") );
1840 int wdayDst
= weekday
,
1841 wdayThis
= GetWeekDay();
1842 if ( wdayDst
== wdayThis
)
1848 if ( flags
== Default_First
)
1850 flags
= GetCountry() == USA
? Sunday_First
: Monday_First
;
1853 // the logic below based on comparing weekday and wdayThis works if Sun (0)
1854 // is the first day in the week, but breaks down for Monday_First case so
1855 // we adjust the week days in this case
1856 if ( flags
== Monday_First
)
1858 if ( wdayThis
== Sun
)
1860 if ( wdayDst
== Sun
)
1863 //else: Sunday_First, nothing to do
1865 // go forward or back in time to the day we want
1866 if ( wdayDst
< wdayThis
)
1868 return Subtract(wxDateSpan::Days(wdayThis
- wdayDst
));
1870 else // weekday > wdayThis
1872 return Add(wxDateSpan::Days(wdayDst
- wdayThis
));
1876 wxDateTime
& wxDateTime::SetToNextWeekDay(WeekDay weekday
)
1878 wxDATETIME_CHECK( weekday
!= Inv_WeekDay
, wxT("invalid weekday") );
1881 WeekDay wdayThis
= GetWeekDay();
1882 if ( weekday
== wdayThis
)
1887 else if ( weekday
< wdayThis
)
1889 // need to advance a week
1890 diff
= 7 - (wdayThis
- weekday
);
1892 else // weekday > wdayThis
1894 diff
= weekday
- wdayThis
;
1897 return Add(wxDateSpan::Days(diff
));
1900 wxDateTime
& wxDateTime::SetToPrevWeekDay(WeekDay weekday
)
1902 wxDATETIME_CHECK( weekday
!= Inv_WeekDay
, wxT("invalid weekday") );
1905 WeekDay wdayThis
= GetWeekDay();
1906 if ( weekday
== wdayThis
)
1911 else if ( weekday
> wdayThis
)
1913 // need to go to previous week
1914 diff
= 7 - (weekday
- wdayThis
);
1916 else // weekday < wdayThis
1918 diff
= wdayThis
- weekday
;
1921 return Subtract(wxDateSpan::Days(diff
));
1924 bool wxDateTime::SetToWeekDay(WeekDay weekday
,
1929 wxCHECK_MSG( weekday
!= Inv_WeekDay
, false, wxT("invalid weekday") );
1931 // we don't check explicitly that -5 <= n <= 5 because we will return false
1932 // anyhow in such case - but may be should still give an assert for it?
1934 // take the current month/year if none specified
1935 ReplaceDefaultYearMonthWithCurrent(&year
, &month
);
1939 // TODO this probably could be optimised somehow...
1943 // get the first day of the month
1944 dt
.Set(1, month
, year
);
1947 WeekDay wdayFirst
= dt
.GetWeekDay();
1949 // go to the first weekday of the month
1950 int diff
= weekday
- wdayFirst
;
1954 // add advance n-1 weeks more
1957 dt
+= wxDateSpan::Days(diff
);
1959 else // count from the end of the month
1961 // get the last day of the month
1962 dt
.SetToLastMonthDay(month
, year
);
1965 WeekDay wdayLast
= dt
.GetWeekDay();
1967 // go to the last weekday of the month
1968 int diff
= wdayLast
- weekday
;
1972 // and rewind n-1 weeks from there
1975 dt
-= wxDateSpan::Days(diff
);
1978 // check that it is still in the same month
1979 if ( dt
.GetMonth() == month
)
1987 // no such day in this month
1993 wxDateTime::wxDateTime_t
GetDayOfYearFromTm(const wxDateTime::Tm
& tm
)
1995 return (wxDateTime::wxDateTime_t
)(gs_cumulatedDays
[wxDateTime::IsLeapYear(tm
.year
)][tm
.mon
] + tm
.mday
);
1998 wxDateTime::wxDateTime_t
wxDateTime::GetDayOfYear(const TimeZone
& tz
) const
2000 return GetDayOfYearFromTm(GetTm(tz
));
2003 wxDateTime::wxDateTime_t
2004 wxDateTime::GetWeekOfYear(wxDateTime::WeekFlags flags
, const TimeZone
& tz
) const
2006 if ( flags
== Default_First
)
2008 flags
= GetCountry() == USA
? Sunday_First
: Monday_First
;
2012 wxDateTime_t nDayInYear
= GetDayOfYearFromTm(tm
);
2014 int wdTarget
= GetWeekDay(tz
);
2015 int wdYearStart
= wxDateTime(1, Jan
, GetYear()).GetWeekDay();
2017 if ( flags
== Sunday_First
)
2019 // FIXME: First week is not calculated correctly.
2020 week
= (nDayInYear
- wdTarget
+ 7) / 7;
2021 if ( wdYearStart
== Wed
|| wdYearStart
== Thu
)
2024 else // week starts with monday
2026 // adjust the weekdays to non-US style.
2027 wdYearStart
= ConvertWeekDayToMondayBase(wdYearStart
);
2028 wdTarget
= ConvertWeekDayToMondayBase(wdTarget
);
2030 // quoting from http://www.cl.cam.ac.uk/~mgk25/iso-time.html:
2032 // Week 01 of a year is per definition the first week that has the
2033 // Thursday in this year, which is equivalent to the week that
2034 // contains the fourth day of January. In other words, the first
2035 // week of a new year is the week that has the majority of its
2036 // days in the new year. Week 01 might also contain days from the
2037 // previous year and the week before week 01 of a year is the last
2038 // week (52 or 53) of the previous year even if it contains days
2039 // from the new year. A week starts with Monday (day 1) and ends
2040 // with Sunday (day 7).
2043 // if Jan 1 is Thursday or less, it is in the first week of this year
2044 if ( wdYearStart
< 4 )
2046 // count the number of entire weeks between Jan 1 and this date
2047 week
= (nDayInYear
+ wdYearStart
+ 6 - wdTarget
)/7;
2049 // be careful to check for overflow in the next year
2050 if ( week
== 53 && tm
.mday
- wdTarget
> 28 )
2053 else // Jan 1 is in the last week of the previous year
2055 // check if we happen to be at the last week of previous year:
2056 if ( tm
.mon
== Jan
&& tm
.mday
< 8 - wdYearStart
)
2057 week
= wxDateTime(31, Dec
, GetYear()-1).GetWeekOfYear();
2059 week
= (nDayInYear
+ wdYearStart
- 1 - wdTarget
)/7;
2063 return (wxDateTime::wxDateTime_t
)week
;
2066 wxDateTime::wxDateTime_t
wxDateTime::GetWeekOfMonth(wxDateTime::WeekFlags flags
,
2067 const TimeZone
& tz
) const
2070 const wxDateTime dateFirst
= wxDateTime(1, tm
.mon
, tm
.year
);
2071 const wxDateTime::WeekDay wdFirst
= dateFirst
.GetWeekDay();
2073 if ( flags
== Default_First
)
2075 flags
= GetCountry() == USA
? Sunday_First
: Monday_First
;
2078 // compute offset of dateFirst from the beginning of the week
2080 if ( flags
== Sunday_First
)
2081 firstOffset
= wdFirst
- Sun
;
2083 firstOffset
= wdFirst
== Sun
? DAYS_PER_WEEK
- 1 : wdFirst
- Mon
;
2085 return (wxDateTime::wxDateTime_t
)((tm
.mday
- 1 + firstOffset
)/7 + 1);
2088 wxDateTime
& wxDateTime::SetToYearDay(wxDateTime::wxDateTime_t yday
)
2090 int year
= GetYear();
2091 wxDATETIME_CHECK( (0 < yday
) && (yday
<= GetNumberOfDays(year
)),
2092 wxT("invalid year day") );
2094 bool isLeap
= IsLeapYear(year
);
2095 for ( Month mon
= Jan
; mon
< Inv_Month
; wxNextMonth(mon
) )
2097 // for Dec, we can't compare with gs_cumulatedDays[mon + 1], but we
2098 // don't need it neither - because of the CHECK above we know that
2099 // yday lies in December then
2100 if ( (mon
== Dec
) || (yday
<= gs_cumulatedDays
[isLeap
][mon
+ 1]) )
2102 Set((wxDateTime::wxDateTime_t
)(yday
- gs_cumulatedDays
[isLeap
][mon
]), mon
, year
);
2111 // ----------------------------------------------------------------------------
2112 // Julian day number conversion and related stuff
2113 // ----------------------------------------------------------------------------
2115 double wxDateTime::GetJulianDayNumber() const
2117 return m_time
.ToDouble() / MILLISECONDS_PER_DAY
+ EPOCH_JDN
+ 0.5;
2120 double wxDateTime::GetRataDie() const
2122 // March 1 of the year 0 is Rata Die day -306 and JDN 1721119.5
2123 return GetJulianDayNumber() - 1721119.5 - 306;
2126 // ----------------------------------------------------------------------------
2127 // timezone and DST stuff
2128 // ----------------------------------------------------------------------------
2130 int wxDateTime::IsDST(wxDateTime::Country country
) const
2132 wxCHECK_MSG( country
== Country_Default
, -1,
2133 wxT("country support not implemented") );
2135 // use the C RTL for the dates in the standard range
2136 time_t timet
= GetTicks();
2137 if ( timet
!= (time_t)-1 )
2140 tm
*tm
= wxLocaltime_r(&timet
, &tmstruct
);
2142 wxCHECK_MSG( tm
, -1, wxT("wxLocaltime_r() failed") );
2144 return tm
->tm_isdst
;
2148 int year
= GetYear();
2150 if ( !IsDSTApplicable(year
, country
) )
2152 // no DST time in this year in this country
2156 return IsBetween(GetBeginDST(year
, country
), GetEndDST(year
, country
));
2160 wxDateTime
& wxDateTime::MakeTimezone(const TimeZone
& tz
, bool noDST
)
2162 long secDiff
= GetTimeZone() + tz
.GetOffset();
2164 // we need to know whether DST is or not in effect for this date unless
2165 // the test disabled by the caller
2166 if ( !noDST
&& (IsDST() == 1) )
2168 // FIXME we assume that the DST is always shifted by 1 hour
2172 return Add(wxTimeSpan::Seconds(secDiff
));
2175 wxDateTime
& wxDateTime::MakeFromTimezone(const TimeZone
& tz
, bool noDST
)
2177 long secDiff
= GetTimeZone() + tz
.GetOffset();
2179 // we need to know whether DST is or not in effect for this date unless
2180 // the test disabled by the caller
2181 if ( !noDST
&& (IsDST() == 1) )
2183 // FIXME we assume that the DST is always shifted by 1 hour
2187 return Subtract(wxTimeSpan::Seconds(secDiff
));
2190 // ============================================================================
2191 // wxDateTimeHolidayAuthority and related classes
2192 // ============================================================================
2194 #include "wx/arrimpl.cpp"
2196 WX_DEFINE_OBJARRAY(wxDateTimeArray
)
2198 static int wxCMPFUNC_CONV
2199 wxDateTimeCompareFunc(wxDateTime
**first
, wxDateTime
**second
)
2201 wxDateTime dt1
= **first
,
2204 return dt1
== dt2
? 0 : dt1
< dt2
? -1 : +1;
2207 // ----------------------------------------------------------------------------
2208 // wxDateTimeHolidayAuthority
2209 // ----------------------------------------------------------------------------
2211 wxHolidayAuthoritiesArray
wxDateTimeHolidayAuthority::ms_authorities
;
2214 bool wxDateTimeHolidayAuthority::IsHoliday(const wxDateTime
& dt
)
2216 size_t count
= ms_authorities
.size();
2217 for ( size_t n
= 0; n
< count
; n
++ )
2219 if ( ms_authorities
[n
]->DoIsHoliday(dt
) )
2230 wxDateTimeHolidayAuthority::GetHolidaysInRange(const wxDateTime
& dtStart
,
2231 const wxDateTime
& dtEnd
,
2232 wxDateTimeArray
& holidays
)
2234 wxDateTimeArray hol
;
2238 const size_t countAuth
= ms_authorities
.size();
2239 for ( size_t nAuth
= 0; nAuth
< countAuth
; nAuth
++ )
2241 ms_authorities
[nAuth
]->DoGetHolidaysInRange(dtStart
, dtEnd
, hol
);
2243 WX_APPEND_ARRAY(holidays
, hol
);
2246 holidays
.Sort(wxDateTimeCompareFunc
);
2248 return holidays
.size();
2252 void wxDateTimeHolidayAuthority::ClearAllAuthorities()
2254 WX_CLEAR_ARRAY(ms_authorities
);
2258 void wxDateTimeHolidayAuthority::AddAuthority(wxDateTimeHolidayAuthority
*auth
)
2260 ms_authorities
.push_back(auth
);
2263 wxDateTimeHolidayAuthority::~wxDateTimeHolidayAuthority()
2265 // required here for Darwin
2268 // ----------------------------------------------------------------------------
2269 // wxDateTimeWorkDays
2270 // ----------------------------------------------------------------------------
2272 bool wxDateTimeWorkDays::DoIsHoliday(const wxDateTime
& dt
) const
2274 wxDateTime::WeekDay wd
= dt
.GetWeekDay();
2276 return (wd
== wxDateTime::Sun
) || (wd
== wxDateTime::Sat
);
2279 size_t wxDateTimeWorkDays::DoGetHolidaysInRange(const wxDateTime
& dtStart
,
2280 const wxDateTime
& dtEnd
,
2281 wxDateTimeArray
& holidays
) const
2283 if ( dtStart
> dtEnd
)
2285 wxFAIL_MSG( wxT("invalid date range in GetHolidaysInRange") );
2292 // instead of checking all days, start with the first Sat after dtStart and
2293 // end with the last Sun before dtEnd
2294 wxDateTime dtSatFirst
= dtStart
.GetNextWeekDay(wxDateTime::Sat
),
2295 dtSatLast
= dtEnd
.GetPrevWeekDay(wxDateTime::Sat
),
2296 dtSunFirst
= dtStart
.GetNextWeekDay(wxDateTime::Sun
),
2297 dtSunLast
= dtEnd
.GetPrevWeekDay(wxDateTime::Sun
),
2300 for ( dt
= dtSatFirst
; dt
<= dtSatLast
; dt
+= wxDateSpan::Week() )
2305 for ( dt
= dtSunFirst
; dt
<= dtSunLast
; dt
+= wxDateSpan::Week() )
2310 return holidays
.GetCount();
2313 // ============================================================================
2314 // other helper functions
2315 // ============================================================================
2317 // ----------------------------------------------------------------------------
2318 // iteration helpers: can be used to write a for loop over enum variable like
2320 // for ( m = wxDateTime::Jan; m < wxDateTime::Inv_Month; wxNextMonth(m) )
2321 // ----------------------------------------------------------------------------
2323 WXDLLIMPEXP_BASE
void wxNextMonth(wxDateTime::Month
& m
)
2325 wxASSERT_MSG( m
< wxDateTime::Inv_Month
, wxT("invalid month") );
2327 // no wrapping or the for loop above would never end!
2328 m
= (wxDateTime::Month
)(m
+ 1);
2331 WXDLLIMPEXP_BASE
void wxPrevMonth(wxDateTime::Month
& m
)
2333 wxASSERT_MSG( m
< wxDateTime::Inv_Month
, wxT("invalid month") );
2335 m
= m
== wxDateTime::Jan
? wxDateTime::Inv_Month
2336 : (wxDateTime::Month
)(m
- 1);
2339 WXDLLIMPEXP_BASE
void wxNextWDay(wxDateTime::WeekDay
& wd
)
2341 wxASSERT_MSG( wd
< wxDateTime::Inv_WeekDay
, wxT("invalid week day") );
2343 // no wrapping or the for loop above would never end!
2344 wd
= (wxDateTime::WeekDay
)(wd
+ 1);
2347 WXDLLIMPEXP_BASE
void wxPrevWDay(wxDateTime::WeekDay
& wd
)
2349 wxASSERT_MSG( wd
< wxDateTime::Inv_WeekDay
, wxT("invalid week day") );
2351 wd
= wd
== wxDateTime::Sun
? wxDateTime::Inv_WeekDay
2352 : (wxDateTime::WeekDay
)(wd
- 1);
2357 wxDateTime
& wxDateTime::SetFromMSWSysTime(const SYSTEMTIME
& st
)
2360 static_cast<wxDateTime::Month
>(wxDateTime::Jan
+ st
.wMonth
- 1),
2362 st
.wHour
, st
.wMinute
, st
.wSecond
, st
.wMilliseconds
);
2365 wxDateTime
& wxDateTime::SetFromMSWSysDate(const SYSTEMTIME
& st
)
2368 static_cast<wxDateTime::Month
>(wxDateTime::Jan
+ st
.wMonth
- 1),
2373 void wxDateTime::GetAsMSWSysTime(SYSTEMTIME
* st
) const
2375 const wxDateTime::Tm
tm(GetTm());
2377 st
->wYear
= (WXWORD
)tm
.year
;
2378 st
->wMonth
= (WXWORD
)(tm
.mon
- wxDateTime::Jan
+ 1);
2382 st
->wHour
= tm
.hour
;
2383 st
->wMinute
= tm
.min
;
2384 st
->wSecond
= tm
.sec
;
2385 st
->wMilliseconds
= tm
.msec
;
2388 void wxDateTime::GetAsMSWSysDate(SYSTEMTIME
* st
) const
2390 const wxDateTime::Tm
tm(GetTm());
2392 st
->wYear
= (WXWORD
)tm
.year
;
2393 st
->wMonth
= (WXWORD
)(tm
.mon
- wxDateTime::Jan
+ 1);
2400 st
->wMilliseconds
= 0;
2405 #endif // wxUSE_DATETIME