X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/fb7ce3e8cc980192d8ef0f7775a55206eb4c514a..0f5378d414cb68caaa535199c1af12dec1720c3d:/src/common/datetime.cpp diff --git a/src/common/datetime.cpp b/src/common/datetime.cpp index 90038a27c3..6ea7a0a37a 100644 --- a/src/common/datetime.cpp +++ b/src/common/datetime.cpp @@ -63,7 +63,7 @@ #if !defined(wxUSE_DATETIME) || wxUSE_DATETIME #ifndef WX_PRECOMP - #ifdef __WXMSW__ + #ifdef __WINDOWS__ #include "wx/msw/wrapwin.h" #endif #include "wx/string.h" @@ -1102,7 +1102,7 @@ wxDateTime wxDateTime::GetEndDST(int year, Country country) // return the current time with ms precision /* static */ wxDateTime wxDateTime::UNow() { - return wxDateTime(wxGetLocalTimeMillis()); + return wxDateTime(wxGetUTCTimeMillis()); } // the values in the tm structure contain the local time @@ -1602,6 +1602,56 @@ wxDateTime& wxDateTime::Add(const wxDateSpan& diff) return *this; } +wxDateSpan wxDateTime::DiffAsDateSpan(const wxDateTime& dt) const +{ + wxASSERT_MSG( IsValid() && dt.IsValid(), wxT("invalid wxDateTime")); + + // If dt is larger than this, calculations below needs to be inverted. + int inv = 1; + if ( dt > *this ) + inv = -1; + + int y = GetYear() - dt.GetYear(); + int m = GetMonth() - dt.GetMonth(); + int d = GetDay() - dt.GetDay(); + + // If month diff is negative, dt is the year before, so decrease year + // and set month diff to its inverse, e.g. January - December should be 1, + // not -11. + if ( m * inv < 0 || (m == 0 && d * inv < 0)) + { + m += inv * MONTHS_IN_YEAR; + y -= inv; + } + + // Same logic for days as for months above. + if ( d * inv < 0 ) + { + // Use number of days in month from the month which end date we're + // crossing. That is month before this for positive diff, and this + // month for negative diff. + // If we're on january and using previous month, we get december + // previous year, but don't care, december has same amount of days + // every year. + wxDateTime::Month monthfordays = GetMonth(); + if (inv > 0 && monthfordays == wxDateTime::Jan) + monthfordays = wxDateTime::Dec; + else if (inv > 0) + monthfordays = static_cast(monthfordays - 1); + + d += inv * wxDateTime::GetNumberOfDays(monthfordays, GetYear()); + m -= inv; + } + + int w = d / DAYS_PER_WEEK; + + // Remove weeks from d, since wxDateSpan only keep days as the ones + // not in complete weeks + d -= w * DAYS_PER_WEEK; + + return wxDateSpan(y, m, w, d); +} + // ---------------------------------------------------------------------------- // Weekday and monthday stuff // ---------------------------------------------------------------------------- @@ -1871,7 +1921,6 @@ wxDateTime::GetWeekOfYear(wxDateTime::WeekFlags flags, const TimeZone& tz) const { // adjust the weekdays to non-US style. wdYearStart = ConvertWeekDayToMondayBase(wdYearStart); - wdTarget = ConvertWeekDayToMondayBase(wdTarget); // quoting from http://www.cl.cam.ac.uk/~mgk25/iso-time.html: // @@ -1887,22 +1936,24 @@ wxDateTime::GetWeekOfYear(wxDateTime::WeekFlags flags, const TimeZone& tz) const // // if Jan 1 is Thursday or less, it is in the first week of this year - if ( wdYearStart < 4 ) - { - // count the number of entire weeks between Jan 1 and this date - week = (nDayInYear + wdYearStart + 6 - wdTarget)/7; + int dayCountFix = wdYearStart < 4 ? 6 : -1; - // be careful to check for overflow in the next year - if ( week == 53 && tm.mday - wdTarget > 28 ) - week = 1; + // count the number of week + week = (nDayInYear + wdYearStart + dayCountFix) / DAYS_PER_WEEK; + + // check if we happen to be at the last week of previous year: + if ( week == 0 ) + { + week = wxDateTime(31, Dec, GetYear() - 1).GetWeekOfYear(); } - else // Jan 1 is in the last week of the previous year + else if ( week == 53 ) { - // check if we happen to be at the last week of previous year: - if ( tm.mon == Jan && tm.mday < 8 - wdYearStart ) - week = wxDateTime(31, Dec, GetYear()-1).GetWeekOfYear(); - else - week = (nDayInYear + wdYearStart - 1 - wdTarget)/7; + int wdYearEnd = (wdYearStart + 364 + IsLeapYear(GetYear())) + % DAYS_PER_WEEK; + + // Week 53 only if last day of year is Thursday or later. + if ( wdYearEnd < 3 ) + week = 1; } } @@ -2198,7 +2249,7 @@ WXDLLIMPEXP_BASE void wxPrevWDay(wxDateTime::WeekDay& wd) : (wxDateTime::WeekDay)(wd - 1); } -#ifdef __WXMSW__ +#ifdef __WINDOWS__ wxDateTime& wxDateTime::SetFromMSWSysTime(const SYSTEMTIME& st) { @@ -2246,6 +2297,6 @@ void wxDateTime::GetAsMSWSysDate(SYSTEMTIME* st) const st->wMilliseconds = 0; } -#endif // __WXMSW__ +#endif // __WINDOWS__ #endif // wxUSE_DATETIME