Modern Apps git

← Modern-Apps.git

todo: yearly Feb-29 tasks return to Feb 29 in leap years

Commit
eaca1eba2589f533dba2017ad219cc11ac6adfd0
Author
Vayun Mathur <[email protected]>
When
2026-12-01 03:04 UTC (5 days ago)
Parents
bebefb4
A yearly task due 2028-02-29 rolled correctly to
2029-02-28, but the rolled row kept the bare FREQ=YEARLY
rule and the clamped date, and Yearly carried no anchor —
so candidates were generated from the clamped date forever:
2030-02-28, 2031-02-28, 2032-02-28, never back to the leap
day. The monthly anchor fix stopped one level short, and
the RRULE export's Feb-29 clamp detection died with it since
it keyed off the (now Feb-28) due date.

Yearly gains an optional pinned anchor in the existing
BYMONTH/BYMONTHDAY representation (bare FREQ=YEARLY parses
and serializes exactly as before; sync envelopes and VTODO
export already handle it), candidates are built from the
anchor each step and clamped per-year, Completion pins the
anchor on the first roll of a bare Feb-29 yearly task, and
the recurrence sheet preserves a pinned anchor on re-save —
the same trap the monthly fix had to close there. Five new
tests pin the 2028->2032 sequence, round-trips, the mirror
from a clamped due date and an ordinary-date control. Weekly
expansion, reminders, subtask reopen and Wear ops were
audited and cleared. Verified: :todo:testDevUnitTest 413
tests pass; lint errors are pre-existing on untouched files.

Diff

todo/src/main/java/com/vayunmathur/todo/domain/Completion.kt
diff --git a/todo/src/main/java/com/vayunmathur/todo/domain/Completion.kt b/todo/src/main/java/com/vayunmathur/todo/domain/Completion.ktindex 037174d868..86e479b9b8 100644--- a/todo/src/main/java/com/vayunmathur/todo/domain/Completion.kt+++ b/todo/src/main/java/com/vayunmathur/todo/domain/Completion.kt@@ -1,12 +1,14 @@ package com.vayunmathur.todo.domain  import com.vayunmathur.todo.data.Todo+import kotlinx.datetime.Month import kotlinx.datetime.TimeZone  /** Checking a task off. A repeating task rolls forward to its next occurrence instead. */ object Completion {     /** Last day of February in a non-leap year: month days up to this never clamp. */     private const val LAST_UNCLAMPED_DAY = 28+    private const val LEAP_DAY = 29      fun complete(todo: Todo, now: Long, zone: TimeZone): Todo {         val recurrence = Recurrence.parse(todo.recurrence)@@ -29,12 +31,18 @@ object Completion {         // occurrence (Feb 28 -> Mar 28, not Mar 31), so pin the original         // anchor day into the stored rule on the first such roll. Days that         // exist in every month never clamp, so they keep the implicit form.-        val rolledRecurrence = if (-            recurrence is Recurrence.Monthly && recurrence.dayOfMonth == null && dueDate.day > LAST_UNCLAMPED_DAY-        ) {-            recurrence.copy(dayOfMonth = dueDate.day).serialize()-        } else {-            todo.recurrence+        // The yearly leap-day case is the same loss one level up: Feb 29+        // clamps to Feb 28 in non-leap years, and a bare FREQ=YEARLY stored+        // rule has nowhere to keep the 29th, so pin it (BYMONTH/BYMONTHDAY)+        // on the first roll or it would stay on Feb 28 through later leap+        // years. Every other yearly date exists in every year.+        val rolledRecurrence = when {+            recurrence is Recurrence.Monthly && recurrence.dayOfMonth == null && dueDate.day > LAST_UNCLAMPED_DAY ->+                recurrence.copy(dayOfMonth = dueDate.day).serialize()+            recurrence is Recurrence.Yearly && recurrence.dayOfMonth == null &&+                dueDate.month == Month.FEBRUARY && dueDate.day == LEAP_DAY ->+                recurrence.copy(month = Month.FEBRUARY, dayOfMonth = LEAP_DAY).serialize()+            else -> todo.recurrence         }         return todo.copy(             done = false,
todo/src/main/java/com/vayunmathur/todo/domain/Recurrence.kt
diff --git a/todo/src/main/java/com/vayunmathur/todo/domain/Recurrence.kt b/todo/src/main/java/com/vayunmathur/todo/domain/Recurrence.ktindex 48429a4af4..60e09ae9cc 100644--- a/todo/src/main/java/com/vayunmathur/todo/domain/Recurrence.kt+++ b/todo/src/main/java/com/vayunmathur/todo/domain/Recurrence.kt@@ -3,6 +3,7 @@ package com.vayunmathur.todo.domain import kotlinx.datetime.DateTimeUnit import kotlinx.datetime.DayOfWeek import kotlinx.datetime.LocalDate+import kotlinx.datetime.Month import kotlinx.datetime.daysUntil import kotlinx.datetime.isoDayNumber import kotlinx.datetime.minus@@ -26,8 +27,15 @@ sealed interface Recurrence {     /** [dayOfMonth] null means "the due date's day", [LAST_DAY] the last day of the month. */     data class Monthly(override val interval: Int = 1, val dayOfMonth: Int? = null) : Recurrence -    /** Repeats every interval years on the due date. */-    data class Yearly(override val interval: Int = 1) : Recurrence+    /**+     * Repeats every interval years on the due date. [month]/[dayOfMonth] pin the anchor+     * when it can clamp (Feb 29); null means "the due date's month and day".+     */+    data class Yearly(+        override val interval: Int = 1,+        val month: Month? = null,+        val dayOfMonth: Int? = null,+    ) : Recurrence      /** Due [interval] days after the task is completed, whenever that is. */     data class AfterCompletion(override val interval: Int = 1) : Recurrence@@ -42,9 +50,7 @@ sealed interface Recurrence {             is Daily -> due.plus(stepsPast(due.daysUntil(base), interval) * interval, DateTimeUnit.DAY)             is Weekly -> nextWeekly(due, base)             is Monthly -> nextMonthly(due, base)-            is Yearly -> generateSequence(1) { it + 1 }-                .map { due.plus(it * interval, DateTimeUnit.YEAR) }-                .first { it > base }+            is Yearly -> nextYearly(due, base)             is AfterCompletion -> today.plus(interval, DateTimeUnit.DAY)         }     }@@ -58,6 +64,18 @@ sealed interface Recurrence {                 LAST_DAY -> today.day == lengthOfMonth(LocalDate(today.year, today.month.number, 1))                 else -> today.day == dayOfMonth             }+            is Yearly -> {+                val anchorMonth = month+                val anchorDay = dayOfMonth+                if (anchorMonth == null || anchorDay == null) {+                    true+                } else {+                    today.month == anchorMonth &&+                        today.day == anchorDay.coerceAtMost(+                            lengthOfMonth(LocalDate(today.year, anchorMonth.number, 1)),+                        )+                }+            }             else -> true         }         return if (matches) today else nextDate(today, today)@@ -76,6 +94,10 @@ sealed interface Recurrence {             parts += "BYDAY=" + days.sortedBy { it.isoDayNumber }.joinToString(",") { DAY_CODES.getValue(it) }         }         if (this is Monthly && dayOfMonth != null) parts += "BYMONTHDAY=$dayOfMonth"+        if (this is Yearly && month != null && dayOfMonth != null) {+            parts += "BYMONTH=${month.number}"+            parts += "BYMONTHDAY=$dayOfMonth"+        }         if (this is AfterCompletion) parts += "$X_FROM=COMPLETION"         return parts.joinToString(";")     }@@ -99,7 +121,8 @@ sealed interface Recurrence {      *  - yearly Feb-29 anchor: `BYMONTH=2;BYMONTHDAY=29,-1` lands on Feb 28 in      *    non-leap years.      * [due] is the task's due date; it supplies the anchor day for a [Monthly]-     * with no explicit [Monthly.dayOfMonth] and detects a Feb-29 [Yearly].+     * with no explicit [Monthly.dayOfMonth] and for a bare [Yearly] (a pinned+     * [Yearly] anchor supplies itself, so it survives a clamped due date).      */     fun toRRule(due: LocalDate? = null): String? {         if (this is AfterCompletion) return null@@ -113,9 +136,16 @@ sealed interface Recurrence {                     if (anchor < LAST_POSSIBLE_DAY) parts += "BYSETPOS=1"                 }             }-            is Yearly -> if (due != null && due.month.number == 2 && due.day == 29) {-                parts += "BYMONTH=2"-                parts += "BYMONTHDAY=29,-1"+            is Yearly -> {+                // The stored anchor survives a clamped roll (due Feb 28); a bare+                // rule still detects Feb 29 from the due date itself.+                val anchorMonth = month ?: due?.month+                val anchorDay = dayOfMonth ?: due?.day+                if (anchorMonth == Month.FEBRUARY && anchorDay == FEB_29) {+                    parts.removeAll { it.startsWith("BYMONTH=") || it.startsWith("BYMONTHDAY=") }+                    parts += "BYMONTH=2"+                    parts += "BYMONTHDAY=29,-1"+                }             }             else -> Unit         }@@ -131,6 +161,7 @@ sealed interface Recurrence {         private const val X_FROM = "X-FROM"         private const val MAX_INTERVAL = 999         private const val MAX_DAY = 31+        private const val FEB_29 = 29          private val DAY_CODES = mapOf(             DayOfWeek.MONDAY to "MO", DayOfWeek.TUESDAY to "TU", DayOfWeek.WEDNESDAY to "WE",@@ -150,7 +181,19 @@ sealed interface Recurrence {                 "DAILY" -> if (parts[X_FROM].equals("COMPLETION", true)) AfterCompletion(interval) else Daily(interval)                 "WEEKLY" -> Weekly(interval, parseDays(parts["BYDAY"]))                 "MONTHLY" -> Monthly(interval, parseMonthDay(parts["BYMONTHDAY"]))-                "YEARLY" -> Yearly(interval)+                "YEARLY" -> {+                    val anchorMonth = parts["BYMONTH"]?.toIntOrNull()+                        ?.let { n -> Month.entries.firstOrNull { it.number == n } }+                    // A mirror-style "29,-1" list still pins day 29; its clamp is+                    // re-encoded by toRRule, never stored.+                    val anchorDay = parseMonthDay(parts["BYMONTHDAY"]?.substringBefore(','))+                        ?.takeIf { it != LAST_DAY }+                    if (anchorMonth != null && anchorDay != null) {+                        Yearly(interval, anchorMonth, anchorDay)+                    } else {+                        Yearly(interval)+                    }+                }                 else -> null             }         }@@ -178,6 +221,22 @@ private fun Recurrence.Weekly.nextWeekly(due: LocalDate, base: LocalDate): Local     } } +private fun Recurrence.Yearly.nextYearly(due: LocalDate, base: LocalDate): LocalDate {+    // A pinned anchor (Feb 29) outlives a clamped occurrence (Feb 28): each+    // candidate is built from the anchor, clamped to that year's month, so a+    // leap day returns in the next leap year instead of drifting to Feb 28.+    val anchorMonth = month ?: due.month+    val anchorDay = dayOfMonth ?: due.day+    var year = due.year+    while (true) {+        val last = lengthOfMonth(LocalDate(year, anchorMonth.number, 1))+        val day = if (anchorDay == Recurrence.LAST_DAY) last else anchorDay.coerceAtMost(last)+        val candidate = LocalDate(year, anchorMonth.number, day)+        if (candidate > base) return candidate+        year += interval+    }+}+ private fun Recurrence.Monthly.nextMonthly(due: LocalDate, base: LocalDate): LocalDate {     val wantedDay = dayOfMonth ?: due.day     var k = 0
todo/src/main/java/com/vayunmathur/todo/ui/RecurrenceSheet.kt
diff --git a/todo/src/main/java/com/vayunmathur/todo/ui/RecurrenceSheet.kt b/todo/src/main/java/com/vayunmathur/todo/ui/RecurrenceSheet.ktindex 852838466a..3ea6fd6074 100644--- a/todo/src/main/java/com/vayunmathur/todo/ui/RecurrenceSheet.kt+++ b/todo/src/main/java/com/vayunmathur/todo/ui/RecurrenceSheet.kt@@ -143,7 +143,10 @@ private fun CustomRecurrence(current: Recurrence?, onSet: (Recurrence) -> Unit)                         n,                         if (lastDay) Recurrence.LAST_DAY else (current as? Recurrence.Monthly)?.dayOfMonth,                     )-                    else -> Recurrence.Yearly(n)+                    // Likewise preserve a pinned yearly anchor (Feb 29): this+                    // form has no month/day picker, so re-saving must not drop it.+                    else -> (current as? Recurrence.Yearly)?.let { Recurrence.Yearly(n, it.month, it.dayOfMonth) }+                        ?: Recurrence.Yearly(n)                 },             )         }) { Text(stringResource(R.string.done_action)) }
todo/src/test/java/com/vayunmathur/todo/domain/CompletionTest.kt
diff --git a/todo/src/test/java/com/vayunmathur/todo/domain/CompletionTest.kt b/todo/src/test/java/com/vayunmathur/todo/domain/CompletionTest.ktindex fe5b7d7b9c..22206a42cf 100644--- a/todo/src/test/java/com/vayunmathur/todo/domain/CompletionTest.kt+++ b/todo/src/test/java/com/vayunmathur/todo/domain/CompletionTest.kt@@ -71,6 +71,32 @@ class CompletionTest {         assertEquals(day(2026, 5, 31), todo.dueAt)     } +    @Test+    fun `yearly leap-day task keeps its anchor day across sequential completions`() {+        // Regression: rolling Feb 29 forward clamps to Feb 28; completing that+        // occurrence must eventually return to Feb 29, not stay on Feb 28+        // through the next leap year (2032).+        var todo = Todo(title = "x", dueAt = day(2028, 2, 29), recurrence = "FREQ=YEARLY")+        todo = Completion.complete(todo, now = day(2028, 2, 29), zone = zone)+        assertEquals(day(2029, 2, 28), todo.dueAt)+        assertEquals("FREQ=YEARLY;BYMONTH=2;BYMONTHDAY=29", todo.recurrence)+        todo = Completion.complete(todo, now = day(2029, 2, 28), zone = zone)+        assertEquals(day(2030, 2, 28), todo.dueAt)+        assertEquals("FREQ=YEARLY;BYMONTH=2;BYMONTHDAY=29", todo.recurrence)+        todo = Completion.complete(todo, now = day(2030, 2, 28), zone = zone)+        assertEquals(day(2031, 2, 28), todo.dueAt)+        todo = Completion.complete(todo, now = day(2031, 2, 28), zone = zone)+        assertEquals(day(2032, 2, 29), todo.dueAt)+    }++    @Test+    fun `yearly task on an ordinary date keeps its implicit recurrence`() {+        val todo = Todo(title = "x", dueAt = day(2026, 3, 15), recurrence = "FREQ=YEARLY")+        val next = Completion.complete(todo, now = day(2026, 3, 15), zone = zone)+        assertEquals(day(2027, 3, 15), next.dueAt)+        assertEquals("FREQ=YEARLY", next.recurrence)+    }+     @Test     fun `monthly task on a day present in every month keeps its implicit recurrence`() {         val todo = Todo(title = "x", dueAt = day(2026, 1, 15), recurrence = "FREQ=MONTHLY")
todo/src/test/java/com/vayunmathur/todo/domain/RecurrenceTest.kt
diff --git a/todo/src/test/java/com/vayunmathur/todo/domain/RecurrenceTest.kt b/todo/src/test/java/com/vayunmathur/todo/domain/RecurrenceTest.ktindex bf74731059..5cf09be734 100644--- a/todo/src/test/java/com/vayunmathur/todo/domain/RecurrenceTest.kt+++ b/todo/src/test/java/com/vayunmathur/todo/domain/RecurrenceTest.kt@@ -3,6 +3,7 @@ package com.vayunmathur.todo.domain import kotlinx.datetime.DateTimeUnit import kotlinx.datetime.DayOfWeek import kotlinx.datetime.LocalDate+import kotlinx.datetime.Month import kotlinx.datetime.minus import kotlinx.datetime.number import kotlinx.datetime.plus@@ -68,6 +69,34 @@ class RecurrenceTest {         assertEquals(d(2029, 2, 28), Recurrence.Yearly().nextDate(due = d(2028, 2, 29), today = d(2028, 2, 29)))     } +    @Test+    fun `pinned yearly leap day returns in the next leap year instead of drifting to Feb 28`() {+        // Regression: a bare yearly rule rolled from Feb 29 lands on Feb 28;+        // without a pinned anchor every later occurrence stayed on Feb 28.+        val r = Recurrence.Yearly(1, Month.FEBRUARY, 29)+        assertEquals(d(2029, 2, 28), r.nextDate(due = d(2028, 2, 29), today = d(2028, 2, 29)))+        assertEquals(d(2030, 2, 28), r.nextDate(due = d(2029, 2, 28), today = d(2029, 2, 28)))+        assertEquals(d(2032, 2, 29), r.nextDate(due = d(2031, 2, 28), today = d(2031, 2, 28)))+        // Long overdue: the anchor, not the clamped due date, drives the jump.+        assertEquals(d(2032, 2, 29), r.nextDate(due = d(2028, 2, 29), today = d(2031, 3, 1)))+        // Interval cadence is kept from the anchor year.+        assertEquals(d(2030, 2, 28), Recurrence.Yearly(2, Month.FEBRUARY, 29).nextDate(d(2028, 2, 29), d(2028, 2, 29)))+        assertEquals(d(2032, 2, 29), Recurrence.Yearly(2, Month.FEBRUARY, 29).nextDate(d(2030, 2, 28), d(2030, 2, 28)))+    }++    @Test+    fun `pinned yearly anchor round trips and the mirror rule still clamps from a clamped due date`() {+        val r = Recurrence.Yearly(1, Month.FEBRUARY, 29)+        assertEquals("FREQ=YEARLY;BYMONTH=2;BYMONTHDAY=29", r.serialize())+        assertEquals(r, Recurrence.parse(r.serialize()))+        assertEquals(r, Recurrence.parse("FREQ=YEARLY;BYMONTH=2;BYMONTHDAY=29,-1"))+        assertEquals(Recurrence.Yearly(), Recurrence.parse("FREQ=YEARLY"))+        // Due date is the clamped Feb 28 here; the stored anchor must still encode the clamp.+        assertEquals("FREQ=YEARLY;BYMONTH=2;BYMONTHDAY=29,-1", r.toRRule(d(2029, 2, 28)))+        assertEquals(d(2029, 2, 28), r.firstOnOrAfter(d(2029, 2, 28)))+        assertEquals(d(2030, 2, 28), r.firstOnOrAfter(d(2029, 3, 1)))+    }+     @Test     fun `after completion counts from today`() {         val r = Recurrence.AfterCompletion(10)@@ -83,6 +112,7 @@ class RecurrenceTest {             Recurrence.Monthly(1, 15),             Recurrence.Monthly(3, Recurrence.LAST_DAY),             Recurrence.Yearly(),+            Recurrence.Yearly(2, Month.FEBRUARY, 29),             Recurrence.AfterCompletion(7),         )         for (r in all) assertEquals(r, Recurrence.parse(r.serialize()), r.serialize())
todo/src/test/java/com/vayunmathur/todo/domain/RemindersTest.kt
diff --git a/todo/src/test/java/com/vayunmathur/todo/domain/RemindersTest.kt b/todo/src/test/java/com/vayunmathur/todo/domain/RemindersTest.ktindex ae1a998eac..a3c690c506 100644--- a/todo/src/test/java/com/vayunmathur/todo/domain/RemindersTest.kt+++ b/todo/src/test/java/com/vayunmathur/todo/domain/RemindersTest.kt@@ -74,6 +74,23 @@ class RemindersTest {         assertEquals(at(16, 9), Reminders.nextFire(todo, now = at(9, 10), zone = zone))     } +    @Test+    fun `an unchecked yearly leap-day task reminds on Feb 29 again in the next leap year`() {+        // The stored due date is a clamped occurrence (Feb 28); only the+        // pinned anchor in the rule can bring the reminder back to Feb 29.+        val due = DueDates.at(LocalDate(2029, 2, 28), LocalTime(9, 0), zone)+        val todo = Todo(+            dueAt = due,+            dueAllDay = false,+            reminderAt = due - 3_600_000L,+            recurrence = "FREQ=YEARLY;BYMONTH=2;BYMONTHDAY=29",+        )+        assertEquals(+            DueDates.at(LocalDate(2032, 2, 29), LocalTime(8, 0), zone),+            Reminders.nextFire(todo, now = DueDates.at(LocalDate(2031, 3, 1), LocalTime(10, 0), zone), zone = zone),+        )+    }+     @Test     fun `done and after-completion tasks have no next reminder`() {         val base = Todo(dueAt = at(10, 8), dueAllDay = false, reminderAt = at(10, 8), recurrence = "FREQ=DAILY")