Modern Apps git

← Modern-Apps.git

contacts: a year-less Feb 29 birthday no longer opens as 28 Feb 1900

Commit
2f97545549468e311225767e97e2b5e8ef7751c3
Author
Vayun Mathur <[email protected]>
When
2026-12-01 11:39 UTC (5 days ago)
Parents
48fd89d
The event date picker initialised its year from the raw
year-less sentinel (LocalDate(1604, 2, 29)): the year wheel
(1900..2100) has no 1604, the index coerced to page 0 ->
year 1900, and since 1900 is not a leap year the day-clamp
fired on the February days-in-month change, rewriting 29 ->
28 before any user input. Toggling 'include year' and
confirming saved LocalDate(1900, 2, 28): wrong day, in a
year never picked.

Year-less dates now anchor the picker on 2000 — a leap year
inside the wheel (the same anchor calendar sync uses for
year-less Feb 29); day and month survive whether or not the
year is switched on, and confirming untouched still saves
the year-less --02-29 form. Five pure tests pin the anchor
and the Feb-29 arithmetic; calendar-sync anchoring, photo
handling, account defaults and detail lookup were audited
and cleared. Verified: :contacts:testDevUnitTest 593 tests
with only 2 pre-existing Compose UI failures (unrelated,
flagged, not chased); :contacts:lint BUILD SUCCESSFUL.

Diff

contacts/src/main/java/com/vayunmathur/contacts/ui/dialogs/EventDatePickerDialog.kt
diff --git a/contacts/src/main/java/com/vayunmathur/contacts/ui/dialogs/EventDatePickerDialog.kt b/contacts/src/main/java/com/vayunmathur/contacts/ui/dialogs/EventDatePickerDialog.ktindex f2049690c9..8281888658 100644--- a/contacts/src/main/java/com/vayunmathur/contacts/ui/dialogs/EventDatePickerDialog.kt+++ b/contacts/src/main/java/com/vayunmathur/contacts/ui/dialogs/EventDatePickerDialog.kt@@ -60,6 +60,33 @@ import kotlin.math.abs import kotlin.time.Clock import java.util.Locale +/** First year on the picker wheel; kept named so the anchor below can be checked against it. */+internal const val PICKER_FIRST_YEAR = 1900++/** Last year on the picker wheel. */+internal const val PICKER_LAST_YEAR = 2100++/**+ * Leap year inside the wheel range used as the starting year for a year-less date.+ *+ * A year-less date arrives on the 1604 sentinel year, which is not on the wheel+ * (1900..2100): the wheel then starts on its first page instead, silently moving+ * the picker's selected year to 1900. 1900 is not a leap year (divisible by 100+ * but not by 400), so the day wheel for February shrank from 29 to 28 days and+ * a 29 February birthday was clamped to 28 February before the user touched+ * anything; confirming with the year switched on saved 1900-02-28. Starting a+ * year-less date on a leap year in range keeps month and day intact whether or+ * not the user switches the year on (and matches the calendar sync anchor).+ */+internal const val NO_YEAR_PICKER_ANCHOR_YEAR = 2000++/**+ * Year the picker wheels start on for [date]: its own year when it has one, or+ * [NO_YEAR_PICKER_ANCHOR_YEAR] for a year-less (1604 sentinel) date.+ */+internal fun pickerInitialYear(date: LocalDate): Int =+    if (date.hasYear) date.year else NO_YEAR_PICKER_ANCHOR_YEAR+ @Composable fun EventDatePickerDialog(id: String, initialDate: LocalDate?, onDismiss: () -> Unit) {     val registry = LocalNavResultRegistry.current@@ -69,10 +96,10 @@ fun EventDatePickerDialog(id: String, initialDate: LocalDate?, onDismiss: () ->          var includeYear by remember { mutableStateOf(initialDate?.hasYear ?: true) }     -    val years = remember { (1900..2100).toList() }+    val years = remember { (PICKER_FIRST_YEAR..PICKER_LAST_YEAR).toList() }     val months = remember { (1..12).toList() }-    -    var selectedYear by remember { mutableIntStateOf(baseDate.year) }++    var selectedYear by remember { mutableIntStateOf(pickerInitialYear(baseDate)) }     var selectedMonth by remember { mutableIntStateOf(baseDate.monthNumber) }     var selectedDay by remember { mutableIntStateOf(baseDate.day) } 
contacts/src/test/java/com/vayunmathur/contacts/pure/EventPickerInitialYearTest.kt
diff --git a/contacts/src/test/java/com/vayunmathur/contacts/pure/EventPickerInitialYearTest.kt b/contacts/src/test/java/com/vayunmathur/contacts/pure/EventPickerInitialYearTest.ktnew file mode 100644index 0000000000..333de5b928--- /dev/null+++ b/contacts/src/test/java/com/vayunmathur/contacts/pure/EventPickerInitialYearTest.kt@@ -0,0 +1,58 @@+package com.vayunmathur.contacts.pure++import com.vayunmathur.contacts.ui.dialogs.NO_YEAR_PICKER_ANCHOR_YEAR+import com.vayunmathur.contacts.ui.dialogs.PICKER_FIRST_YEAR+import com.vayunmathur.contacts.ui.dialogs.PICKER_LAST_YEAR+import com.vayunmathur.contacts.ui.dialogs.pickerInitialYear+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFailsWith+import kotlin.test.assertTrue+import kotlinx.datetime.LocalDate++/**+ * Regression for a year-less 29 February birthday landing on the wrong date.+ *+ * The picker wheel only offers 1900..2100, so a year-less birthday (stored on+ * the 1604 sentinel year, e.g. "--02-29") used to start the year wheel on its+ * first page, 1900. 1900 is not a leap year, so February's day wheel shrank to+ * 28 days and clamped the selected day before the user touched anything; a+ * contact whose birthday is 29 February (no year) came back as 28 February+ * 1900 when the user switched "include year" on and confirmed.+ */+class EventPickerInitialYearTest {++    @Test+    fun noYearLeapDayStartsOnALeapYearInWheelRange() {+        val year = pickerInitialYear(LocalDate(1604, 2, 29))+        assertTrue(year in PICKER_FIRST_YEAR..PICKER_LAST_YEAR)+        // The whole point: February of the starting year must have a 29th,+        // or the day wheel clamps the birthday to the 28th.+        assertEquals(29, LocalDate(year, 2, 29).day)+    }++    @Test+    fun wheelFirstYearIsNotALeapYear() {+        // Documents why starting on the wheel's first page was wrong for a+        // 29 February date: 1900 has no 29 February to select.+        assertEquals(1900, PICKER_FIRST_YEAR)+        assertFailsWith<IllegalArgumentException> { LocalDate(PICKER_FIRST_YEAR, 2, 29) }+    }++    @Test+    fun noYearOrdinaryDateKeepsMonthAndDayOnAnchorYear() {+        val anchor = pickerInitialYear(LocalDate(1604, 5, 4))+        assertEquals(NO_YEAR_PICKER_ANCHOR_YEAR, anchor)+        assertEquals(LocalDate(anchor, 5, 4), LocalDate(2000, 5, 4))+    }++    @Test+    fun datedLeapDayKeepsItsOwnYear() {+        assertEquals(2024, pickerInitialYear(LocalDate(2024, 2, 29)))+    }++    @Test+    fun datedOrdinaryDateKeepsItsOwnYear() {+        assertEquals(1990, pickerInitialYear(LocalDate(1990, 5, 4)))+    }+}