[PATCH 18/23] kcalendarsystem: Simplify week() and weeksInYear() implementations

Jon Severinsson jon at severinsson.net
Fri Oct 12 15:13:53 UTC 2012


---
 kdecore/date/kcalendarsystem.cpp        |  118 ++++++++-----------------------
 kdecore/date/kcalendarsystem.h          |   51 +++++--------
 kdecore/date/kcalendarsystemprivate_p.h |   18 +++--
 3 filer ändrade, 54 tillägg(+), 133 borttagningar(-)

diff --git a/kdecore/date/kcalendarsystem.cpp b/kdecore/date/kcalendarsystem.cpp
index 653cea9..92db979 100644
--- a/kdecore/date/kcalendarsystem.cpp
+++ b/kdecore/date/kcalendarsystem.cpp
@@ -149,28 +149,6 @@ KCalendarSystemPrivate::~KCalendarSystemPrivate()
     delete m_eraList;
 }
 
-// Reimplement if special maths handling required, e.g. Hebrew.
-int KCalendarSystemPrivate::week(const QDate &date, KLocale::WeekNumberSystem weekNumberSystem, int *yearNum) const
-{
-    int y, m, d;
-    q->julianDayToDate(date.toJulianDay(), y, m, d);
-
-    switch (weekNumberSystem) {
-    case KLocale::IsoWeekNumber:
-        return isoWeekNumber(date, yearNum);
-    case KLocale::FirstFullWeek:
-        return regularWeekNumber(date, locale()->weekStartDay(), 0, yearNum);
-    case KLocale::FirstPartialWeek:
-        return regularWeekNumber(date, locale()->weekStartDay(), 1, yearNum);
-    case KLocale::SimpleWeek:
-        return simpleWeekNumber(date, yearNum);
-    case KLocale::DefaultWeekNumber:
-    default:
-        return week(date, locale()->weekNumberSystem(), yearNum);
-    }
-}
-
-// Reimplement if special maths handling required, e.g. Hebrew.
 int KCalendarSystemPrivate::isoWeekNumber(const QDate &date, int *yearNum) const
 {
     int y, m, d;
@@ -225,7 +203,6 @@ int KCalendarSystemPrivate::isoWeekNumber(const QDate &date, int *yearNum) const
     return week;
 }
 
-// Reimplement if special maths handling required, e.g. Hebrew.
 int KCalendarSystemPrivate::regularWeekNumber(const QDate &date, int weekStartDay, int firstWeekNumber, int *weekYear) const
 {
     int y, m, d;
@@ -251,7 +228,6 @@ int KCalendarSystemPrivate::regularWeekNumber(const QDate &date, int weekStartDa
     return week;
 }
 
-// Reimplement if special maths handling required, e.g. Hebrew.
 int KCalendarSystemPrivate::simpleWeekNumber(const QDate &date, int *yearNum) const
 {
     int y, m, d;
@@ -262,25 +238,6 @@ int KCalendarSystemPrivate::simpleWeekNumber(const QDate &date, int *yearNum) co
     return ((date.toJulianDay() - firstDayOfYear(y).toJulianDay()) / 7) + 1;
 }
 
-// Reimplement if special maths handling required, e.g. Hebrew.
-int KCalendarSystemPrivate::weeksInYear(int year, KLocale::WeekNumberSystem weekNumberSystem) const
-{
-    switch (weekNumberSystem) {
-    case KLocale::IsoWeekNumber:
-        return isoWeeksInYear(year);
-    case KLocale::FirstFullWeek:
-        return regularWeeksInYear(year, locale()->weekStartDay(), 0);
-    case KLocale::FirstPartialWeek:
-        return regularWeeksInYear(year, locale()->weekStartDay(), 1);
-    case KLocale::SimpleWeek:
-        return simpleWeeksInYear(year);
-    case KLocale::DefaultWeekNumber:
-    default:
-        return weeksInYear(year, locale()->weekNumberSystem());
-    }
-}
-
-// Reimplement if special maths handling required, e.g. Hebrew.
 int KCalendarSystemPrivate::isoWeeksInYear(int year) const
 {
     QDate lastDayOfThisYear = lastDayOfYear(year);
@@ -296,13 +253,11 @@ int KCalendarSystemPrivate::isoWeeksInYear(int year) const
     return lastWeekInThisYear;
 }
 
-// Reimplement if special maths handling required, e.g. Hebrew.
 int KCalendarSystemPrivate::regularWeeksInYear(int year, int weekStartDay, int firstWeekNumber) const
 {
     return regularWeekNumber(lastDayOfYear(year), weekStartDay, firstWeekNumber, 0);
 }
 
-// Reimplement if special maths handling required, e.g. Hebrew.
 int KCalendarSystemPrivate::simpleWeeksInYear(int year) const
 {
     return simpleWeekNumber(lastDayOfYear(year), 0);
@@ -1213,38 +1168,26 @@ int KCalendarSystem::monthsInYear(int year) const
     return -1;
 }
 
-int KCalendarSystem::weeksInYear(const QDate &date) const
-{
-    return weeksInYear(date, KLocale::DefaultWeekNumber);
-}
-
-int KCalendarSystem::weeksInYear(int year) const
-{
-    return weeksInYear(year, KLocale::DefaultWeekNumber);
-}
-
-// NOT VIRTUAL - Uses shared-d instead
-int KCalendarSystem::weeksInYear(const QDate &date, KLocale::WeekNumberSystem weekNumberSystem) const
-{
-    Q_D(const KCalendarSystem);
-
-    if (isValid(date)) {
-        return d->weeksInYear(year(date), weekNumberSystem);
-    }
-
-    return -1;
-}
-
-// NOT VIRTUAL - Uses shared-d instead
 int KCalendarSystem::weeksInYear(int year, KLocale::WeekNumberSystem weekNumberSystem) const
 {
     Q_D(const KCalendarSystem);
 
-    if (isValid(year, 1, 1)) {
-        return d->weeksInYear(year, weekNumberSystem);
-    }
+    if (!isValid(year, 1, 1))
+        return -1;
 
-    return -1;
+    switch (weekNumberSystem) {
+    case KLocale::IsoWeekNumber:
+        return d->isoWeeksInYear(year);
+    case KLocale::FirstFullWeek:
+        return d->regularWeeksInYear(year, locale()->weekStartDay(), 0);
+    case KLocale::FirstPartialWeek:
+        return d->regularWeeksInYear(year, locale()->weekStartDay(), 1);
+    case KLocale::SimpleWeek:
+        return d->simpleWeeksInYear(year);
+    case KLocale::DefaultWeekNumber:
+    default:
+        return weeksInYear(year, locale()->weekNumberSystem());
+    }
 }
 
 int KCalendarSystem::daysInYear(const QDate &date) const
@@ -1307,27 +1250,26 @@ int KCalendarSystem::dayOfYear(const QDate &date) const
 }
 
 
-int KCalendarSystem::weekNumber(const QDate &date, int *yearNum) const
-{
-    return week(date, KLocale::IsoWeekNumber, yearNum);
-}
-
-// NOT VIRTUAL - Uses shared-d instead
-int KCalendarSystem::week(const QDate &date, int *yearNum) const
-{
-    return week(date, KLocale::DefaultWeekNumber, yearNum);
-}
-
-// NOT VIRTUAL - Uses shared-d instead
 int KCalendarSystem::week(const QDate &date, KLocale::WeekNumberSystem weekNumberSystem, int *yearNum) const
 {
     Q_D(const KCalendarSystem);
 
-    if (isValid(date)) {
-        return d->week(date, weekNumberSystem, yearNum);
-    }
+    if (!isValid(date))
+        return -1;
 
-    return -1;
+    switch (weekNumberSystem) {
+    case KLocale::IsoWeekNumber:
+        return d->isoWeekNumber(date, yearNum);
+    case KLocale::FirstFullWeek:
+        return d->regularWeekNumber(date, locale()->weekStartDay(), 0, yearNum);
+    case KLocale::FirstPartialWeek:
+        return d->regularWeekNumber(date, locale()->weekStartDay(), 1, yearNum);
+    case KLocale::SimpleWeek:
+        return d->simpleWeekNumber(date, yearNum);
+    case KLocale::DefaultWeekNumber:
+    default:
+        return week(date, locale()->weekNumberSystem(), yearNum);
+    }
 }
 
 bool KCalendarSystem::isLeapYear(int year) const
diff --git a/kdecore/date/kcalendarsystem.h b/kdecore/date/kcalendarsystem.h
index 2e13c8e..742713a 100644
--- a/kdecore/date/kcalendarsystem.h
+++ b/kdecore/date/kcalendarsystem.h
@@ -225,7 +225,6 @@ public:
      */
     bool isValid(const QString &eraName, int yearInEra, int month, int day) const;
 
-    //KDE5 make virtual?
     /**
      * @since 4.4
      *
@@ -291,7 +290,6 @@ public:
      */
     bool setDate(QDate &date, QString eraName, int yearInEra, int month, int day) const;
 
-    //KDE5 make virtual?
     /**
      * @since 4.4
      *
@@ -512,21 +510,9 @@ public:
     int monthsInYear(int year) const;
 
     /**
-     * Returns the number of localized weeks in the given year.
-     *
-     * @param date the date to obtain year from
-     * @return number of weeks in the year, -1 if input date invalid
-     */
-    virtual int weeksInYear(const QDate &date) const;
-
-    //KDE5 Merge with virtual weeksInYear with default
-    /**
      * @since 4.7
      *
-     * Returns the number of Weeks in a year using the required Week Number System.
-     *
-     * Unless you specifically want a particular Week Number System (e.g. ISO Weeks)
-     * you should use the localized number of weeks provided by weeksInYear().
+     * Returns the number of Weeks in a year using the specified Week Number System.
      *
      * @see week()
      * @see formatDate()
@@ -534,24 +520,15 @@ public:
      * @param weekNumberSystem the week number system to use
      * @return number of weeks in the year, -1 if  date invalid
      */
-    int weeksInYear(const QDate &date, KLocale::WeekNumberSystem weekNumberSystem) const;
-
-    /**
-     * Returns the number of localized weeks in the given year.
-     *
-     * @param year the year
-     * @return number of weeks in the year, -1 if input date invalid
-     */
-    virtual int weeksInYear(int year) const;
+    inline int weeksInYear(const QDate &date, KLocale::WeekNumberSystem weekNumberSystem = KLocale::DefaultWeekNumber) const
+    {
+        return isValid(date) ? weeksInYear(year(date), weekNumberSystem) : -1;
+    }
 
-    //KDE5 Merge with virtual weeksInYear with default
     /**
      * @since 4.7
      *
-     * Returns the number of Weeks in a year using the required Week Number System.
-     *
-     * Unless you specifically want a particular Week Number System (e.g. ISO Weeks)
-     * you should use the localized number of weeks provided by weeksInYear().
+     * Returns the number of Weeks in a year using the specified Week Number System.
      *
      * @see week()
      * @see formatDate()
@@ -559,7 +536,7 @@ public:
      * @param weekNumberSystem the week number system to use
      * @return number of weeks in the year, -1 if  date invalid
      */
-    int weeksInYear(int year, KLocale::WeekNumberSystem weekNumberSystem) const;
+    int weeksInYear(int year, KLocale::WeekNumberSystem weekNumberSystem = KLocale::DefaultWeekNumber) const;
 
     /**
      * Returns the number of days in the given year.
@@ -660,9 +637,11 @@ public:
      * @param yearNum returns the year the date belongs to
      * @return ISO week number, -1 if input date invalid
      */
-    KDECORE_DEPRECATED virtual int weekNumber(const QDate &date, int *yearNum = 0) const;
+    KDECORE_DEPRECATED inline int weekNumber(const QDate &date, int *yearNum = 0) const
+    {
+        return week(date, KLocale::IsoWeekNumber, yearNum);
+    }
 
-    //KDE5 Make virtual?
     /**
      * Returns the localized Week Number for the date.
      *
@@ -680,9 +659,11 @@ public:
      * @param yearNum returns the year the date belongs to
      * @return localized week number, -1 if input date invalid
      */
-    int week(const QDate &date, int *yearNum = 0) const;
+    inline int week(const QDate &date, int *yearNum) const
+    {
+        return week(date, KLocale::DefaultWeekNumber, yearNum);
+    }
 
-    //KDE5 Make virtual?
     /**
      * Returns the Week Number for the date in the required Week Number System.
      *
@@ -703,7 +684,7 @@ public:
      * @param yearNum returns the year the date belongs to
      * @return week number, -1 if input date invalid
      */
-    int week(const QDate &date, KLocale::WeekNumberSystem weekNumberSystem, int *yearNum = 0) const;
+    int week(const QDate &date, KLocale::WeekNumberSystem weekNumberSystem = KLocale::DefaultWeekNumber, int *yearNum = 0) const;
 
     /**
      * Returns whether a given year is a leap year.
diff --git a/kdecore/date/kcalendarsystemprivate_p.h b/kdecore/date/kcalendarsystemprivate_p.h
index 46abc14..683817b 100644
--- a/kdecore/date/kcalendarsystemprivate_p.h
+++ b/kdecore/date/kcalendarsystemprivate_p.h
@@ -50,16 +50,6 @@ public:
     virtual QString weekDayName(int weekDay, KLocale::DateTimeComponentFormat format) const = 0;
 
     // Virtual methods to re-implement if special maths needed
-    virtual int week(const QDate &date, KLocale::WeekNumberSystem weekNumberSystem, int *yearNum) const;
-    virtual int isoWeekNumber(const QDate &date, int *yearNum) const;
-    virtual int regularWeekNumber(const QDate &date, int weekStartDay, int firstWeekNumber, int *weekYear) const;
-    virtual int simpleWeekNumber(const QDate &date, int *yearNum) const;
-    virtual int weeksInYear(int year, KLocale::WeekNumberSystem weekNumberSystem) const;
-    virtual int isoWeeksInYear(int year) const;
-    virtual int regularWeeksInYear(int year, int weekStartDay, int firstWeekNumber) const;
-    virtual int simpleWeeksInYear(int year) const;
-
-    // Virtual methods to re-implement if special maths needed
     // Currently only Hebrew may need special conversion, rest should be OK
     virtual int yearsDifference(const QDate &fromDate, const QDate &toDate) const;
     virtual int monthsDifference(const QDate &fromDate, const QDate &toDate) const;
@@ -94,6 +84,14 @@ public:
     KSharedConfig::Ptr config();
     void loadConfig(const QString & calendarType);
 
+    // Week utility functions
+    int isoWeekNumber(const QDate &date, int *yearNum) const;
+    int regularWeekNumber(const QDate &date, int weekStartDay, int firstWeekNumber, int *weekYear) const;
+    int simpleWeekNumber(const QDate &date, int *yearNum) const;
+    int isoWeeksInYear(int year) const;
+    int regularWeeksInYear(int year, int weekStartDay, int firstWeekNumber) const;
+    int simpleWeeksInYear(int year) const;
+
     // Global variables each calendar system must initialise
     const KCalendarSystem *q;
     const KLocale *m_locale;
-- 
1.7.10.4



More information about the Kde-frameworks-devel mailing list