diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarDataSource.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarDataSource.kt index e879e690..3790ba81 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarDataSource.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarDataSource.kt @@ -1349,6 +1349,8 @@ class AndroidCalendarDataSource @Inject constructor( // Reminders, guests and colour come along like any new event, and so does // a fresh UID — the detached row is a separate event now, and sharing the // parent's would collide with it in .ics restore dedup. + // Fail before inserting: the EXDATE write below needs a usable length. + row.writeDuration() val detachedId = insertEvent(form.toDetachedOccurrence(), allDayReminderTimeMinutes) // Rows touched, not occurrences excluded — 1 whenever the series row still // exists. It catches the row disappearing under us, not an EXDATE the @@ -1392,6 +1394,8 @@ class AndroidCalendarDataSource @Inject constructor( return eventId } // Insert the new series first: if it fails, the original is untouched. + // Fail before inserting: truncating the original needs a usable length. + row.writeDuration() val newEventId = insertEvent(updated, allDayReminderTimeMinutes) carrySplitExdate(newEventId, row, beginMillis, original, updated) truncateSeries(eventId, row, beginMillis) @@ -1795,7 +1799,7 @@ class AndroidCalendarDataSource @Inject constructor( val values = buildOccurrenceCancelValues( originalInstanceMillis = beginMillis, dtStartMillis = beginMillis, - duration = row.duration, + duration = row.writeDuration(), timezone = row.timezone, allDay = row.allDay, ) diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapper.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapper.kt index c4bf21f7..980a38ad 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapper.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapper.kt @@ -370,7 +370,7 @@ internal fun buildMovedMasterValues( /** The recurring copy's DURATION: its own if present, else derived from DTEND. */ private fun MasterEventSnapshot.movedDuration(): String = duration?.takeIf { it.isNotBlank() } ?: seriesDuration(dtStartMillis, dtEndMillis ?: dtStartMillis, occurrenceSpanMillis = null, isAllDay = isAllDay) - ?: throw IllegalStateException("series has no usable length") + ?: throw WriteFailedException("move series with no usable length") /** * Raw provider snapshot of one exception row of a recurring series (a modified @@ -472,7 +472,10 @@ internal fun seriesDuration( val span = occurrenceSpanMillis?.takeIf { it >= 0 } ?: dtEndMillis?.minus(dtStartMillis)?.takeIf { it >= 0 } ?: return null - return rfc2445Duration(if (isAllDay) span.coerceAtLeast(MILLIS_PER_DAY) else span, isAllDay) + if (!isAllDay) return rfc2445Duration(span, isAllDay = false) + // Whole days, rounded: a span read across a DST change or zone offset is a few hours off. + val days = ((span + MILLIS_PER_DAY / 2) / MILLIS_PER_DAY).coerceAtLeast(1) + return rfc2445Duration(days * MILLIS_PER_DAY, isAllDay = true) } /** [buildOccurrenceExdateValues] excluding every one of [occurrences] in one write. */ diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapperTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapperTest.kt index 845b3588..bbfe7355 100644 --- a/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapperTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapperTest.kt @@ -525,6 +525,12 @@ class EventWriteMapperTest { assertThat(seriesDuration(1_000L, null, null, isAllDay = true)).isNull() } + @Test + fun `all-day series duration rounds to whole days`() { + // 47 h: a two-day occurrence read across a DST change. + assertThat(seriesDuration(0L, null, 47L * 3_600_000L, isAllDay = true)).isEqualTo("P2D") + } + @Test fun `series duration keeps a zero length the provider already shows`() { assertThat(seriesDuration(1_000L, 0L, 0L, isAllDay = false)).isEqualTo("P0S") @@ -1244,7 +1250,7 @@ class EventWriteMapperTest { @Test fun `moved series with a zero dtend and no duration is refused, not copied broken`() { val snapshot = masterSnapshot(dtEndMillis = 0L, rrule = "FREQ=WEEKLY;BYDAY=TH") - assertThrows { + assertThrows { buildMovedMasterValues(snapshot, targetCalendarId = 9L, uid = "u@calendula") } }