Fixed wrong day of week shown in Time. Needs review as I used some trick

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@16445 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stefano Ceccherini 2006-02-17 08:53:10 +00:00
parent f282009d15
commit 25a4d8f957
2 changed files with 18 additions and 22 deletions

View File

@ -393,7 +393,7 @@ TCalendarView::InitDates()
int32 label = 0; int32 label = 0;
int idx = 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); int daycnt = getDaysInMonth(f_month, f_year);
bool cday = false; bool cday = false;
for (int row = 0; row < 6; row++) { for (int row = 0; row < 6; row++) {
@ -403,7 +403,8 @@ TCalendarView::InitDates()
if (idx < f_firstday || idx > daycnt +f_firstday +1) if (idx < f_firstday || idx > daycnt +f_firstday +1)
label = 0; label = 0;
else else
label = idx -(f_firstday +1); label = idx - (f_firstday - 1);
idx++; idx++;
cday = label == f_day; cday = label == f_day;
day->SetTo(label, cday); day->SetTo(label, cday);
@ -441,7 +442,7 @@ TCalendarView::SetTo(int32 year, int32 month, int32 day)
InitDates(); InitDates();
} else if (f_day != day) { } else if (f_day != day) {
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<TDay *>(ChildAt(idx)); TDay *day = dynamic_cast<TDay *>(ChildAt(idx));
f_cday->SetValue(B_CONTROL_OFF); f_cday->SetValue(B_CONTROL_OFF);
f_cday = day; f_cday = day;

View File

@ -1,6 +1,8 @@
#include "DateUtils.h" #include "DateUtils.h"
#include "math.h" #include "math.h"
#include <time.h>
bool bool
isLeapYear(const int year) isLeapYear(const int year)
{ {
@ -28,26 +30,19 @@ getDaysInMonth(const int month, const int year)
int int
getFirstDay(const int month, const int year) getFirstDay(const int month, const int year)
{ {
int l_century = year/100; struct tm tm;
int l_decade = year%100; time_t currentTime = time(NULL);
int l_month = (month +10)%12; localtime_r(&currentTime, &tm);
int l_day = 1;
if (l_month == 1 || l_month == 2) { // TODO: review this.
if (l_decade == 0) { // We rely on the fact that tm.tm_wday is calculated
l_decade = 99; // starting from the following parameters
l_century -= 1; tm.tm_mon = month;
} else tm.tm_year = year;
l_decade-= 1; tm.tm_mday = 1;
}
float tmp = (l_day +(floor(((13 *l_month) -1)/5)) currentTime = mktime(&tm);
+l_decade +(floor(l_decade /4)) localtime_r(&currentTime, &tm);
+(floor(l_century/4)) -(2 *l_century));
int result = static_cast<int>(tmp)%7;
if (result < 0)
result += 7;
return result; return tm.tm_wday;
} }