From 378851a283af92e2e041a5fdf22e20c92f49629e Mon Sep 17 00:00:00 2001 From: Lauri Nurmi Date: Sat, 2 Sep 2017 16:53:31 +0300 Subject: [PATCH] Refactor WeekFlags processing into UseEffectiveWeekDayFlags() Use the new GetFirstWeekDay() function rather than GetCountry() == USA to determine the first weekday. --- include/wx/datetime.h | 3 +++ src/common/datetime.cpp | 25 +++++++++++++------------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/include/wx/datetime.h b/include/wx/datetime.h index e1d392a9bd..e6b271c60c 100644 --- a/include/wx/datetime.h +++ b/include/wx/datetime.h @@ -1145,6 +1145,9 @@ private: // functions inline bool IsInStdRange() const; + // assign the preferred first day of a week to flags, if necessary + void UseEffectiveWeekDayFlags(WeekFlags &flags) const; + // the internal representation of the time is the amount of milliseconds // elapsed since the origin which is set by convention to the UNIX/C epoch // value: the midnight of January 1, 1970 (UTC) diff --git a/src/common/datetime.cpp b/src/common/datetime.cpp index 46c27b8eb7..38ddd2980d 100644 --- a/src/common/datetime.cpp +++ b/src/common/datetime.cpp @@ -1770,10 +1770,7 @@ wxDateTime& wxDateTime::SetToWeekDayInSameWeek(WeekDay weekday, WeekFlags flags) return *this; } - if ( flags == Default_First ) - { - flags = GetCountry() == USA ? Sunday_First : Monday_First; - } + UseEffectiveWeekDayFlags(flags); // the logic below based on comparing weekday and wdayThis works if Sun (0) // is the first day in the week, but breaks down for Monday_First case so @@ -1928,10 +1925,7 @@ wxDateTime::wxDateTime_t wxDateTime::GetDayOfYear(const TimeZone& tz) const wxDateTime::wxDateTime_t wxDateTime::GetWeekOfYear(wxDateTime::WeekFlags flags, const TimeZone& tz) const { - if ( flags == Default_First ) - { - flags = GetCountry() == USA ? Sunday_First : Monday_First; - } + UseEffectiveWeekDayFlags(flags); Tm tm(GetTm(tz)); wxDateTime_t nDayInYear = GetDayOfYearFromTm(tm); @@ -2018,10 +2012,7 @@ wxDateTime::wxDateTime_t wxDateTime::GetWeekOfMonth(wxDateTime::WeekFlags flags, const wxDateTime dateFirst = wxDateTime(1, tm.mon, tm.year); const wxDateTime::WeekDay wdFirst = dateFirst.GetWeekDay(); - if ( flags == Default_First ) - { - flags = GetCountry() == USA ? Sunday_First : Monday_First; - } + UseEffectiveWeekDayFlags(flags); // compute offset of dateFirst from the beginning of the week int firstOffset; @@ -2134,6 +2125,16 @@ wxDateTime& wxDateTime::MakeFromTimezone(const TimeZone& tz, bool noDST) return Subtract(wxTimeSpan::Seconds(secDiff)); } +void wxDateTime::UseEffectiveWeekDayFlags(WeekFlags &flags) const +{ + if ( flags == Default_First ) + { + WeekDay firstDay; + GetFirstWeekDay(&firstDay); + flags = firstDay == Sun ? Sunday_First : Monday_First; + } +} + // ============================================================================ // wxDateTimeHolidayAuthority and related classes // ============================================================================