[PATCH 17/23] kcalendarsystem: Simplify the dayOfWeek() implementation.
Jon Severinsson
jon at severinsson.net
Fri Oct 12 15:13:52 UTC 2012
Day of week is the same for a given julian day number regardless of calendar system used.
---
kdecore/date/kcalendarsystem.cpp | 29 +++--------------
kdecore/date/kcalendarsystem.h | 5 ++-
kdecore/date/kcalendarsystemhebrew.cpp | 10 ------
kdecore/date/kcalendarsystemhebrew_p.h | 2 --
kdecore/date/kcalendarsystemprivate_p.h | 1 -
kdecore/date/kcalendarsystemqdate.cpp | 5 ---
kdecore/date/kcalendarsystemqdate_p.h | 1 -
kdecore/tests/kcalendartest.cpp | 54 -------------------------------
kdecore/tests/kcalendartest.h | 1 -
9 filer ändrade, 9 tillägg(+), 99 borttagningar(-)
diff --git a/kdecore/date/kcalendarsystem.cpp b/kdecore/date/kcalendarsystem.cpp
index f8bc3e6..653cea9 100644
--- a/kdecore/date/kcalendarsystem.cpp
+++ b/kdecore/date/kcalendarsystem.cpp
@@ -182,7 +182,7 @@ int KCalendarSystemPrivate::isoWeekNumber(const QDate &date, int *yearNum) const
// let's guess 1st day of 1st week
firstDayWeek1 = firstDayOfYear(y);
- weekDay1 = dayOfWeek(firstDayWeek1);
+ weekDay1 = q->dayOfWeek(firstDayWeek1);
// iso 8601: week 1 is the first containing thursday and week starts on monday
if (weekDay1 > 4 /*Thursday*/) {
@@ -203,8 +203,8 @@ int KCalendarSystemPrivate::isoWeekNumber(const QDate &date, int *yearNum) const
lastDay = lastDayOfYear(y);
// if our date is in last week && 1st week in next year has thursday
- if ((dayOfYear(date) >= daysInYear(y) - dayOfWeek(lastDay) + 1)
- && dayOfWeek(lastDay) < 4) {
+ if ((dayOfYear(date) >= daysInYear(y) - q->dayOfWeek(lastDay) + 1)
+ && q->dayOfWeek(lastDay) < 4) {
if (yearNum) {
* yearNum = addYears(y, 1);
}
@@ -231,11 +231,11 @@ int KCalendarSystemPrivate::regularWeekNumber(const QDate &date, int weekStartDa
int y, m, d;
q->julianDayToDate(date.toJulianDay(), y, m, d);
- int firstWeekDayOffset = (dayOfWeek(date) - weekStartDay + 7) % 7;
+ int firstWeekDayOffset = (q->dayOfWeek(date) - weekStartDay + 7) % 7;
int dayInYear = date.toJulianDay() - firstDayOfYear(y).toJulianDay(); // 0 indexed
int week = ((dayInYear - firstWeekDayOffset + 7) / 7);
- if (dayOfWeek(firstDayOfYear(y)) != weekStartDay) {
+ if (q->dayOfWeek(firstDayOfYear(y)) != weekStartDay) {
week = week + firstWeekNumber;
}
@@ -599,15 +599,6 @@ int KCalendarSystemPrivate::dayOfYear(const QDate &date) const
return (date.toJulianDay() - jdFirstDayOfYear + 1);
}
-int KCalendarSystemPrivate::dayOfWeek(const QDate &date) const
-{
- // Makes assumption that Julian Day 0 was day 1 of week
- // This is true for Julian/Gregorian calendar with jd 0 being Monday
- // We add 1 for ISO compliant numbering for 7 day week
- // Assumes we've never skipped weekdays
- return ((date.toJulianDay() % 7) + 1);
-}
-
QDate KCalendarSystemPrivate::firstDayOfYear(int year) const
{
qint64 jd;
@@ -1315,16 +1306,6 @@ int KCalendarSystem::dayOfYear(const QDate &date) const
return -1;
}
-int KCalendarSystem::dayOfWeek(const QDate &date) const
-{
- Q_D(const KCalendarSystem);
-
- if (isValid(date)) {
- return d->dayOfWeek(date);
- }
-
- return -1;
-}
int KCalendarSystem::weekNumber(const QDate &date, int *yearNum) const
{
diff --git a/kdecore/date/kcalendarsystem.h b/kdecore/date/kcalendarsystem.h
index d31fc0a..2e13c8e 100644
--- a/kdecore/date/kcalendarsystem.h
+++ b/kdecore/date/kcalendarsystem.h
@@ -640,7 +640,10 @@ public:
* @param date the date to obtain day from
* @return day of week number, -1 if input date not valid
*/
- virtual int dayOfWeek(const QDate &date) const;
+ inline int dayOfWeek(const QDate &date) const
+ {
+ return isValid(date) ? date.dayOfWeek() : -1;
+ }
/**
* @deprecated use week() instead
diff --git a/kdecore/date/kcalendarsystemhebrew.cpp b/kdecore/date/kcalendarsystemhebrew.cpp
index f5a9e94..0c0c5c4 100644
--- a/kdecore/date/kcalendarsystemhebrew.cpp
+++ b/kdecore/date/kcalendarsystemhebrew.cpp
@@ -905,16 +905,6 @@ QDate KCalendarSystemHebrew::latestValidDate() const
return QDate::fromJulianDay(3313431);
}
-int KCalendarSystemHebrew::dayOfWeek(const QDate &date) const
-{
- class h_date * sd = toHebrew(date);
- if (sd->hd_dw == 0) {
- return 7;
- } else {
- return (sd->hd_dw);
- }
-}
-
bool KCalendarSystemHebrew::isLeapYear(int year) const
{
return KCalendarSystem::isLeapYear(year);
diff --git a/kdecore/date/kcalendarsystemhebrew_p.h b/kdecore/date/kcalendarsystemhebrew_p.h
index b202e80..6362388 100644
--- a/kdecore/date/kcalendarsystemhebrew_p.h
+++ b/kdecore/date/kcalendarsystemhebrew_p.h
@@ -51,8 +51,6 @@ public:
virtual QDate earliestValidDate() const;
virtual QDate latestValidDate() const;
- virtual int dayOfWeek(const QDate &date) const;
-
virtual bool isLeapYear(int year) const;
virtual bool isLeapYear(const QDate &date) const;
diff --git a/kdecore/date/kcalendarsystemprivate_p.h b/kdecore/date/kcalendarsystemprivate_p.h
index b77597a..46abc14 100644
--- a/kdecore/date/kcalendarsystemprivate_p.h
+++ b/kdecore/date/kcalendarsystemprivate_p.h
@@ -77,7 +77,6 @@ public:
int differenceYearNumbers(int fromYear, int toYear) const;
QString simpleDateString(const QString &str) const;
int dayOfYear(const QDate &date) const;
- int dayOfWeek(const QDate &date) const;
QDate firstDayOfYear(int year) const;
QDate lastDayOfYear(int year) const;
QDate firstDayOfMonth(int year, int month) const;
diff --git a/kdecore/date/kcalendarsystemqdate.cpp b/kdecore/date/kcalendarsystemqdate.cpp
index 44677b7..215e538 100644
--- a/kdecore/date/kcalendarsystemqdate.cpp
+++ b/kdecore/date/kcalendarsystemqdate.cpp
@@ -446,11 +446,6 @@ int KCalendarSystemQDate::dayOfYear(const QDate &date) const
return date.dayOfYear();
}
-int KCalendarSystemQDate::dayOfWeek(const QDate &date) const
-{
- return date.dayOfWeek();
-}
-
bool KCalendarSystemQDate::isLeapYear(int year) const
{
return QDate::isLeapYear(year);
diff --git a/kdecore/date/kcalendarsystemqdate_p.h b/kdecore/date/kcalendarsystemqdate_p.h
index dbe52f9..fb10a0a 100644
--- a/kdecore/date/kcalendarsystemqdate_p.h
+++ b/kdecore/date/kcalendarsystemqdate_p.h
@@ -59,7 +59,6 @@ public:
virtual int daysInMonth(const QDate &date) const;
virtual int dayOfYear(const QDate &date) const;
- virtual int dayOfWeek(const QDate &date) const;
virtual bool isLeapYear(int year) const;
virtual bool isLeapYear(const QDate &date) const;
diff --git a/kdecore/tests/kcalendartest.cpp b/kdecore/tests/kcalendartest.cpp
index 1459183..4baae5f 100644
--- a/kdecore/tests/kcalendartest.cpp
+++ b/kdecore/tests/kcalendartest.cpp
@@ -2315,60 +2315,6 @@ void KCalendarTest::testQDateDayOfYear()
delete calendar;
}
-void KCalendarTest::testQDateDayOfWeek()
-{
- const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::QDateCalendar);
-
- QDate testDate;
-
- calendar->setDate(testDate, 2000, 1, 1);
- QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
- calendar->setDate(testDate, 2000, 1, 2);
- QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
- calendar->setDate(testDate, 2000, 1, 3);
- QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
- calendar->setDate(testDate, 2000, 1, 4);
- QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
- calendar->setDate(testDate, 2000, 1, 5);
- QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
- calendar->setDate(testDate, 2000, 1, 6);
- QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
- calendar->setDate(testDate, 2000, 1, 7);
- QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
-
- calendar->setDate(testDate, -4712, 1, 2);
- QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
- calendar->setDate(testDate, -4712, 1, 3);
- QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
- calendar->setDate(testDate, -4712, 1, 4);
- QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
- calendar->setDate(testDate, -4712, 1, 5);
- QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
- calendar->setDate(testDate, -4712, 1, 6);
- QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
- calendar->setDate(testDate, -4712, 1, 7);
- QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
- calendar->setDate(testDate, -4712, 1, 8);
- QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
-
- calendar->setDate(testDate, 9999, 1, 1);
- QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
- calendar->setDate(testDate, 9999, 1, 2);
- QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
- calendar->setDate(testDate, 9999, 1, 3);
- QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
- calendar->setDate(testDate, 9999, 1, 4);
- QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
- calendar->setDate(testDate, 9999, 1, 5);
- QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
- calendar->setDate(testDate, 9999, 1, 6);
- QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
- calendar->setDate(testDate, 9999, 1, 7);
- QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
-
- delete calendar;
-}
-
// Don't really need this as Gregorian currently uses QDate directly
void KCalendarTest::testQDateIsLeapYear()
{
diff --git a/kdecore/tests/kcalendartest.h b/kdecore/tests/kcalendartest.h
index 505c137..ba0c7bb 100644
--- a/kdecore/tests/kcalendartest.h
+++ b/kdecore/tests/kcalendartest.h
@@ -117,7 +117,6 @@ private Q_SLOTS:
void testQDateDaysInYear();
void testQDateDaysInMonth();
void testQDateDayOfYear();
- void testQDateDayOfWeek();
void testQDateIsLeapYear();
void testKLocalizedDate();
void testWeekNumberSystem();
--
1.7.10.4
More information about the Kde-frameworks-devel
mailing list