diff --git a/src/preferences/time/CalendarView.cpp b/src/preferences/time/CalendarView.cpp index 840325e8dc..b6a4ee912e 100644 --- a/src/preferences/time/CalendarView.cpp +++ b/src/preferences/time/CalendarView.cpp @@ -393,7 +393,7 @@ TCalendarView::InitDates() int32 label = 0; int idx = 0; - f_firstday = getFirstDay(f_month+1, f_year); + f_firstday = getFirstDay(f_month, f_year); int daycnt = getDaysInMonth(f_month, f_year); bool cday = false; for (int row = 0; row < 6; row++) { @@ -403,7 +403,8 @@ TCalendarView::InitDates() if (idx < f_firstday || idx > daycnt +f_firstday +1) label = 0; else - label = idx -(f_firstday +1); + label = idx - (f_firstday - 1); + idx++; cday = label == f_day; day->SetTo(label, cday); @@ -441,7 +442,7 @@ TCalendarView::SetTo(int32 year, int32 month, int32 day) InitDates(); } else if (f_day != day) { f_day = day; - int idx = ((f_day/7)*7) + (f_day%7 +f_firstday +1); + int idx = ((f_day/7)*7) + (f_day%7 +f_firstday - 1); TDay *day = dynamic_cast(ChildAt(idx)); f_cday->SetValue(B_CONTROL_OFF); f_cday = day; diff --git a/src/preferences/time/DateUtils.cpp b/src/preferences/time/DateUtils.cpp index 19ca670fa3..661a25bc6d 100644 --- a/src/preferences/time/DateUtils.cpp +++ b/src/preferences/time/DateUtils.cpp @@ -1,6 +1,8 @@ #include "DateUtils.h" #include "math.h" +#include + bool isLeapYear(const int year) { @@ -28,26 +30,19 @@ getDaysInMonth(const int month, const int year) int getFirstDay(const int month, const int year) { - int l_century = year/100; - int l_decade = year%100; - int l_month = (month +10)%12; - int l_day = 1; + struct tm tm; + time_t currentTime = time(NULL); + localtime_r(¤tTime, &tm); - if (l_month == 1 || l_month == 2) { - if (l_decade == 0) { - l_decade = 99; - l_century -= 1; - } else - l_decade-= 1; - } + // TODO: review this. + // We rely on the fact that tm.tm_wday is calculated + // starting from the following parameters + tm.tm_mon = month; + tm.tm_year = year; + tm.tm_mday = 1; - float tmp = (l_day +(floor(((13 *l_month) -1)/5)) - +l_decade +(floor(l_decade /4)) - +(floor(l_century/4)) -(2 *l_century)); - int result = static_cast(tmp)%7; - - if (result < 0) - result += 7; + currentTime = mktime(&tm); + localtime_r(¤tTime, &tm); - return result; + return tm.tm_wday; }