| Summary: | KCalendarSystem::addMonths() does not always work for Hijri and Jalali calendars | ||
|---|---|---|---|
| Product: | [Unmaintained] kdelibs | Reporter: | nq |
| Component: | kdecore | Assignee: | John Layt <jlayt> |
| Status: | RESOLVED INTENTIONAL | ||
| Severity: | normal | CC: | jlayt |
| Priority: | NOR | ||
| Version First Reported In: | unspecified | ||
| Target Milestone: | --- | ||
| Platform: | Gentoo Packages | ||
| OS: | Linux | ||
| Latest Commit: | Version Fixed/Implemented In: | ||
| Sentry Crash Report: | |||
|
Description
nq
2006-07-25 11:32:11 UTC
KCalendarSystemHebrew::addMonths() does not work correctly either as it adds the number of days in the current month. Example: QDate date; KGlobal::locale()->calendar()->setYMD(date, 5766, 7, 30); date = KGlobal::locale()->calendar()->addMonths(date, 1); // date is now at month 9, day 1 Same problem with addYears().
Here is my generic functions for working solving the problems:
QDate KCalendarSystem::addMonths(const QDate &date, int nmonths) {
QDate result = date;
int m = month(date);
int d = day(date);
if(nmonths > 0) {
int nMonthsInYear = monthsInYear(result);
while(nmonths > 0 && m + nmonths > nMonthsInYear) {
setYMD(result, year(result) + 1, 1, 1);
nmonths -= nMonthsInYear - m + 1;
m = 1;
nMonthsInYear = monthsInYear(result);
}
if(nmonths > 0) {
setYMD(result, year(result), m + nmonths, 1);
}
if(d != 1) {
int nDaysInMonth = daysInMonth(result);
if(d > nDaysInMonth) result = addDays(result, nDaysInMonth - 1);
else result = addDays(result, d - 1);
}
} else if(nmonths < 0) {
while(nmonths < 0 && m + nmonths < 1) {
setYMD(result, year(result) - 1, 1, 1);
int nMonthsInYear = monthsInYear(result);
setYMD(result, year(result), nMonthsInYear, 1);
nmonths += m;
m = nMonthsInYear;
}
if(nmonths < 0) {
setYMD(result, year(result), m + nmonths, 1);
}
if(d != 1) {
int nDaysInMonth = daysInMonth(result);
if(d > nDaysInMonth) result = addDays(result, nDaysInMonth - 1);
else result = addDays(result, d - 1);
}
}
return result;
}
QDate KCalendarSystem::addYears(const QDate &date, int nyears) {
QDate result = date;
int y = year(date) + nyears;
int d = day(date);
int m = month(date);
setYMD(result, y, 1, 1);
if(m > monthsInYear(result)) {
setYMD(result, y + 1, 1, 1);
addDays(result, -1);
}
setYMD(result, y, m, 1);
if(d > daysInMonth(result)) {
d = daysInMonth(result);
}
setYMD(result, y, m, d);
return result;
}
Assign to me to work on. Check KDE4 which should be correct and back-port if possible. Works OK in KDE4, will not be back-ported to KDE3 as there will probably not be anymore KDE3 releases. |