+wxDateTime& wxDateTime::SetToWeekDayInSameWeek(WeekDay weekday, WeekFlags flags)
+{
+ wxDATETIME_CHECK( weekday != Inv_WeekDay, wxT("invalid weekday") );
+
+ int wdayDst = weekday,
+ wdayThis = GetWeekDay();
+ if ( wdayDst == wdayThis )
+ {
+ // nothing to do
+ return *this;
+ }
+
+ if ( flags == Default_First )
+ {
+ flags = GetCountry() == USA ? Sunday_First : Monday_First;
+ }
+
+ // 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
+ // we adjust the week days in this case
+ if ( flags == Monday_First )
+ {
+ if ( wdayThis == Sun )
+ wdayThis += 7;
+ if ( wdayDst == Sun )
+ wdayDst += 7;
+ }
+ //else: Sunday_First, nothing to do
+
+ // go forward or back in time to the day we want
+ if ( wdayDst < wdayThis )
+ {
+ return Subtract(wxDateSpan::Days(wdayThis - wdayDst));
+ }
+ else // weekday > wdayThis
+ {
+ return Add(wxDateSpan::Days(wdayDst - wdayThis));
+ }
+}
+
+wxDateTime& wxDateTime::SetToNextWeekDay(WeekDay weekday)
+{
+ wxDATETIME_CHECK( weekday != Inv_WeekDay, wxT("invalid weekday") );
+
+ int diff;
+ WeekDay wdayThis = GetWeekDay();
+ if ( weekday == wdayThis )
+ {
+ // nothing to do
+ return *this;
+ }
+ else if ( weekday < wdayThis )
+ {
+ // need to advance a week
+ diff = 7 - (wdayThis - weekday);
+ }
+ else // weekday > wdayThis
+ {
+ diff = weekday - wdayThis;
+ }
+
+ return Add(wxDateSpan::Days(diff));
+}
+
+wxDateTime& wxDateTime::SetToPrevWeekDay(WeekDay weekday)
+{
+ wxDATETIME_CHECK( weekday != Inv_WeekDay, wxT("invalid weekday") );
+
+ int diff;
+ WeekDay wdayThis = GetWeekDay();
+ if ( weekday == wdayThis )
+ {
+ // nothing to do
+ return *this;
+ }
+ else if ( weekday > wdayThis )
+ {
+ // need to go to previous week
+ diff = 7 - (weekday - wdayThis);
+ }
+ else // weekday < wdayThis
+ {
+ diff = wdayThis - weekday;
+ }
+
+ return Subtract(wxDateSpan::Days(diff));
+}
+