[PATCH 14/23] kcalendarsystem: Simplify the addDays() and daysDifference implementations.

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


These needs no calendar system specific code, but can work directly on the julian day numbers.
---
 kdecore/date/kcalendarsystem.cpp |   24 ------------------------
 kdecore/date/kcalendarsystem.h   |   12 +++++++++---
 2 filer ändrade, 9 tillägg(+), 27 borttagningar(-)

diff --git a/kdecore/date/kcalendarsystem.cpp b/kdecore/date/kcalendarsystem.cpp
index 5872a2c..31a7059 100644
--- a/kdecore/date/kcalendarsystem.cpp
+++ b/kdecore/date/kcalendarsystem.cpp
@@ -1185,20 +1185,6 @@ QDate KCalendarSystem::addMonths(const QDate &date, int numMonths) const
     return QDate();
 }
 
-QDate KCalendarSystem::addDays(const QDate &date, int numDays) const
-{
-    // QDate only holds a uint and has no boundary checking in addDays(), so we need to check
-    if (isValid(date) && (long) date.toJulianDay() + (long) numDays > 0) {
-        // QDate adds straight to jd
-        QDate temp = date.addDays(numDays);
-        if (isValid(temp)) {
-            return temp;
-        }
-    }
-
-    return QDate();
-}
-
 // NOT VIRTUAL - Uses shared-d instead
 void KCalendarSystem::dateDifference(const QDate &fromDate, const QDate &toDate,
                                      int *yearsDiff, int *monthsDiff, int *daysDiff, int *direction) const
@@ -1234,16 +1220,6 @@ int KCalendarSystem::monthsDifference(const QDate &fromDate, const QDate &toDate
     return 0;
 }
 
-// NOT VIRTUAL - Uses shared-d instead
-int KCalendarSystem::daysDifference(const QDate &fromDate, const QDate &toDate) const
-{
-    if (isValid(fromDate) && isValid(toDate)) {
-        return toDate.toJulianDay() - fromDate.toJulianDay();
-    }
-
-    return 0;
-}
-
 int KCalendarSystem::monthsInYear(const QDate &date) const
 {
     Q_D(const KCalendarSystem);
diff --git a/kdecore/date/kcalendarsystem.h b/kdecore/date/kcalendarsystem.h
index 9214847..a2f857d 100644
--- a/kdecore/date/kcalendarsystem.h
+++ b/kdecore/date/kcalendarsystem.h
@@ -422,7 +422,11 @@ public:
      * @param ndays number of days to add
      * @return The new date, null date if any errors
      */
-    virtual QDate addDays(const QDate &date, int ndays) const;
+    inline QDate addDays(const QDate &date, int ndays) const
+    {
+        QDate d = date.addDays(ndays);
+        return isValid(d) ? d : QDate();
+    }
 
     //KDE5 make virtual?
     /**
@@ -474,7 +478,6 @@ public:
      */
     int monthsDifference(const QDate &fromDate, const QDate &toDate) const;
 
-    //KDE5 make virtual?
     /**
      * Returns the difference between two dates in days
      * The returned value will be negative if @p fromDate > @p toDate.
@@ -483,7 +486,10 @@ public:
      * @param toDate The date to end at
      * @return The number of days difference
      */
-    int daysDifference(const QDate &fromDate, const QDate &toDate) const;
+    inline int daysDifference(const QDate &fromDate, const QDate &toDate) const
+    {
+        return isValid(fromDate) && isValid(toDate) ? toDate.toJulianDay() - fromDate.toJulianDay() : 0;
+    }
 
     /**
      * Returns number of months in the given year
-- 
1.7.10.4



More information about the Kde-frameworks-devel mailing list