[california/wip/725785-create-recurring] Better week-of-month calculation, better place for code



commit 5747f0bf695102e62dd64b49b62c37b8bfb9df34
Author: Jim Nelson <jim yorba org>
Date:   Wed Jun 25 12:42:03 2014 -0700

    Better week-of-month calculation, better place for code

 src/calendar/calendar-date.vala             |   26 --------------------------
 src/calendar/calendar-day-of-month.vala     |    7 +++++++
 src/component/component-details-parser.vala |    2 +-
 src/tests/tests-calendar-date.vala          |    2 +-
 4 files changed, 9 insertions(+), 28 deletions(-)
---
diff --git a/src/calendar/calendar-date.vala b/src/calendar/calendar-date.vala
index 7c800b4..9f0e71f 100644
--- a/src/calendar/calendar-date.vala
+++ b/src/calendar/calendar-date.vala
@@ -196,32 +196,6 @@ public class Date : Unit<Date>, Gee.Comparable<Date>, Gee.Hashable<Date> {
     }
     
     /**
-     * Returns the 1-based position of the current { link day_of_week} in the { link month}.
-     *
-     * For example, if this is the first Monday of the month, returns 1.
-     */
-    public int day_of_week_position_in_month() {
-        MonthOfYear moy = month_of_year();
-        
-        // Walk every week of this month ... first day of week doesn't matter here, first Monday
-        // is first Monday whether or not the week starts with Sunday or Monday
-        // TODO: Would be better to find a non-iterative algorithm for this
-        int position = 1;
-        foreach (Week week in moy.to_week_span(FirstOfWeek.SUNDAY)) {
-            // if this date is in the week, found it
-            if (week.contains(this))
-                break;
-            
-            // only increase position counter if this day of week is present in the week that is
-            // also in this month (weeks can span months)
-            if (week.date_at(day_of_week).month_of_year().equal_to(moy))
-                position++;
-        }
-        
-        return position;
-    }
-    
-    /**
      * Returns the { link MonthOfYear} the { link Date} falls in.
      */
     public MonthOfYear month_of_year() {
diff --git a/src/calendar/calendar-day-of-month.vala b/src/calendar/calendar-day-of-month.vala
index 5732405..d616c38 100644
--- a/src/calendar/calendar-day-of-month.vala
+++ b/src/calendar/calendar-day-of-month.vala
@@ -26,6 +26,13 @@ public class DayOfMonth : BaseObject, Gee.Comparable<DayOfMonth>, Gee.Hashable<D
     public int value { get; private set; }
     
     /**
+     * Returns the 1-based week of the month this day resides in.
+     *
+     * For example, if this is the first Monday of the month, returns 1.
+     */
+    public int week_of_month { get { return ((value - 1) / DayOfWeek.COUNT) + 1; } }
+    
+    /**
      * Returns the day number as an informal (no leading zero) string.
      */
     public string informal_number { get; private set; }
diff --git a/src/component/component-details-parser.vala b/src/component/component-details-parser.vala
index 4be57a2..d7481eb 100644
--- a/src/component/component-details-parser.vala
+++ b/src/component/component-details-parser.vala
@@ -600,7 +600,7 @@ public class DetailsParser : BaseObject {
         if (by_days != null) {
             Gee.Set<Calendar.DayOfWeek> dows = from_array<Calendar.DayOfWeek>(by_days).to_hash_set();
              Calendar.Date earliest = Calendar.System.today.upcoming(true, (date) => {
-                if (position != 0 && date.day_of_week_position_in_month() != position)
+                if (position != 0 && date.day_of_month.week_of_month != position)
                     return false;
                 
                 return dows.contains(date.day_of_week);
diff --git a/src/tests/tests-calendar-date.vala b/src/tests/tests-calendar-date.vala
index 22c5205..0276c3e 100644
--- a/src/tests/tests-calendar-date.vala
+++ b/src/tests/tests-calendar-date.vala
@@ -168,7 +168,7 @@ private class CalendarDate : UnitTest.Harness {
     }
     
     private bool test_dow_position(Calendar.Date date, int expected, out string? dump) throws Error {
-        int position = date.day_of_week_position_in_month();
+        int position = date.day_of_month.week_of_month;
         
         dump = "%s position=%d, expected=%d".printf(date.to_string(), position, expected);
         


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]