Share the untimed wall clock and zone checks, shift exceptions by days, review fixes (#101)

This commit is contained in:
makiolaj committed 2026-11-17 17:08:08 +01:00
1 parent d04ef3d612
commit d95fb9c5f9
13 files changed
+122 -35

No files matched your search

@@ -37,9 +37,16 @@ internal object StoredReminders {
fun of(form: TaskForm, current: Task?, settings: Settings, now: Long, zone: ZoneId): List<TaskReminder> {
val wanted = form.reminders.map { it.lead }.distinct()
if (form.remindersChosen) return wanted
// In the frame the form holds them in, so an untouched untimed end does not read as moved.
return withDefaults(
form, current?.start, current?.due, current?.isAllDay ?: form.isAllDay, settings, now, zone,
current?.untimed, current?.timeZone,
form,
current?.formValueOf(TaskEnd.START),
current?.formValueOf(TaskEnd.DUE),
current?.isAllDay ?: form.isAllDay,
settings,
now,
zone,
current?.untimed,
)
}
@@ -53,10 +60,9 @@ internal object StoredReminders {
now: Long,
zone: ZoneId,
wasUntimed: TaskEnd? = null,
wasTimeZone: String? = null,
): List<TaskReminder> {
val wanted = form.reminders.map { it.lead }.distinct()
val fresh = freshAnchors(form, start, due, wasAllDay, settings, now, zone, wasUntimed, wasTimeZone)
val fresh = freshAnchors(form, start, due, wasAllDay, settings, now, zone, wasUntimed)
if (fresh.isEmpty()) return wanted
if (anchorsOf(start, due).isEmpty() || wanted.isEmpty()) return wanted.ifEmpty { defaultsFor(form, settings, now, zone) }
val named = wanted.map { it.fromStart }.toSet()
@@ -76,11 +82,10 @@ internal object StoredReminders {
now: Long,
zone: ZoneId,
wasUntimed: TaskEnd? = null,
wasTimeZone: String? = null,
): Set<Boolean> {
val wanted = form.reminders.map { it.lead }.distinct()
val gained = anchorsOf(form.start, form.due) - anchorsOf(start, due)
val was = TaskDates(start, due, wasAllDay, wasUntimed, wasTimeZone)
val was = TaskDates(start, due, wasAllDay, wasUntimed)
return gained + revived(form, was, wanted, settings, now, zone)
}
@@ -4,7 +4,10 @@ import de.jeanlucmakiola.agendula.data.tasks.room.TaskAlarmEntity
import de.jeanlucmakiola.agendula.data.tasks.room.TaskEntity
import de.jeanlucmakiola.agendula.domain.PRIORITY_NONE
import de.jeanlucmakiola.agendula.domain.TaskEnd
import de.jeanlucmakiola.agendula.domain.knownZone
import de.jeanlucmakiola.agendula.domain.untimedDate
import de.jeanlucmakiola.agendula.domain.untimedInstantOf
import de.jeanlucmakiola.agendula.domain.untimedWall
import de.jeanlucmakiola.agendula.domain.zoneOr
import de.jeanlucmakiola.agendula.domain.TaskStatus
import de.jeanlucmakiola.agendula.domain.ical.ICalComponent
@@ -152,7 +155,7 @@ object VTodoMapper {
// write would emit the caller's minted UID alongside the empty one.
val uid = take(uidProperty, uidProperty?.value?.trim()).orEmpty()
val zoneHint = vtodo.property("DTSTART")?.param("TZID") ?: vtodo.property("DUE")?.param("TZID")
val zoneHint = knownZone(vtodo.property("DTSTART")?.param("TZID")) ?: knownZone(vtodo.property("DUE")?.param("TZID"))
val dtstart = readTime(vtodo, "DTSTART", TaskEnd.START, zoneHint)
val due = readTime(vtodo, "DUE", TaskEnd.DUE, zoneHint)
@@ -571,11 +574,9 @@ object VTodoMapper {
noTime = marked && value.tzid != null && onDayBoundary(end!!, value.instant, zoneOr(value.tzid)),
)
is ICalValues.TimeValue.Unrepresentable -> {
val wall = ICalValues.floatingWall(property)?.takeIf { marked }
val zone = zoneOr(zoneHint)
val instant = wall?.let { untimedInstantOf(end!!, it.toLocalDate(), zone) }
if (instant != null && wall == instant.wallIn(zone)) {
TimeRead(property, instant, zoneHint?.takeIf { runCatching { ZoneId.of(it) }.isSuccess }, false, true, noTime = true)
val wall = ICalValues.floatingWall(property)?.takeIf { marked && it == untimedWall(end!!, it.toLocalDate()) }
if (wall != null) {
TimeRead(property, untimedInstantOf(end!!, wall.toLocalDate(), zoneOr(zoneHint)), zoneHint, false, true, noTime = true)
} else {
// Readable but not reproducible: the column gets the best-effort
// instant, the property round-trips from the residue verbatim.
@@ -596,12 +597,13 @@ object VTodoMapper {
// Resolve the zone once. Letting the parameter and the value each decide
// separately produces TZID on a `…Z` value, which §3.3.5 forbids and
// sabre answers 415 for.
val tzid = entity.timezone?.takeIf { runCatching { ZoneId.of(it) }.isSuccess }
if (entity.untimed?.propertyName == name) {
val tzid = knownZone(entity.timezone)
val untimed = entity.untimed
if (untimed?.propertyName == name) {
// A UTC value says nothing about its day: without a zone, the wall clock alone.
val mark = ICalParam(ICalValues.NO_TIME_PARAM, "TRUE")
return if (tzid == null) {
ICalProperty(name, listOf(mark), ICalValues.formatFloating(instant.wallIn(ZoneId.systemDefault())))
ICalProperty(name, listOf(mark), ICalValues.formatFloating(untimedWall(untimed, instant.untimedDate(null))))
} else {
ICalProperty(name, listOf(mark, ICalParam("TZID", tzid)), ICalValues.formatDateTime(instant, tzid))
}
@@ -111,8 +111,16 @@ object TaskFormWriter {
status = master.status,
percentComplete = master.percentComplete,
completedAt = master.completedAt,
exdate = if (delta == null || delta == kotlin.time.Duration.ZERO) edited.exdate
else shiftValues(edited.exdate, delta, master.timezone ?: tzId),
exdate = when {
delta == null || delta == kotlin.time.Duration.ZERO -> edited.exdate
// Exceptions of a start without a time move by whole days, as the start does.
!form.isAllDay && form.untimed == TaskEnd.START -> {
val zone = master.timezone ?: tzId
val days = ChronoUnit.DAYS.between(master.dtstart!!.untimedDate(zone), dtstart!!.untimedDate(tzId))
mapValues(edited.exdate, zone) { untimedMoved(TaskEnd.START, it, days, zone, zoneOr(zone)) }
}
else -> shiftValues(edited.exdate, delta, master.timezone ?: tzId)
},
)
}
@@ -224,7 +224,6 @@ class ExternalImport @Inject constructor(
dtstart = start,
due = due,
isAllDay = isAllDay,
untimed = untimed,
rrule = rrule,
rdate = rdate,
createdAt = created,
@@ -86,9 +86,21 @@ data class TaskDates(
fun countsFromUntimed(fromStart: Boolean): Boolean = !isAllDay && untimed == reminderEnd(fromStart)
}
/** [timeZone] when it names a zone this device knows, else null. */
fun knownZone(timeZone: String?): String? = timeZone?.takeIf { runCatching { ZoneId.of(it) }.isSuccess }
/** [timeZone] when it names a zone this device knows, else [fallback]. */
fun zoneOr(timeZone: String?, fallback: ZoneId = ZoneId.systemDefault()): ZoneId =
timeZone?.let { runCatching { ZoneId.of(it) }.getOrNull() } ?: fallback
knownZone(timeZone)?.let(ZoneId::of) ?: fallback
/**
* The wall clock an untimed [end] on [date] is written at where no zone
* applies (a floating value): the first or the last second of the day.
*/
fun untimedWall(end: TaskEnd, date: java.time.LocalDate): java.time.LocalDateTime = when (end) {
TaskEnd.START -> date.atStartOfDay()
TaskEnd.DUE -> date.atTime(23, 59, 59)
}
/**
* The stored instant for [date] on an untimed [end]: the first or the last
@@ -96,10 +108,7 @@ fun zoneOr(timeZone: String?, fallback: ZoneId = ZoneId.systemDefault()): ZoneId
* valid moment, not at a 00:00 that does not exist.
*/
fun untimedInstantOf(end: TaskEnd, date: java.time.LocalDate, zone: ZoneId = ZoneId.systemDefault()): Instant {
val at = when (end) {
TaskEnd.START -> date.atStartOfDay(zone)
TaskEnd.DUE -> date.atTime(23, 59, 59).atZone(zone)
}
val at = if (end == TaskEnd.START) date.atStartOfDay(zone) else untimedWall(end, date).atZone(zone)
return Instant.fromEpochMilliseconds(at.toInstant().toEpochMilli())
}
@@ -119,7 +119,12 @@ data class Task(
fun valueOf(end: TaskEnd): Instant? = if (end == TaskEnd.START) start else due
/** Whether [end] carries a time of day. */
fun hasTime(end: TaskEnd): Boolean = dates.hasTime(end)
fun hasTime(end: TaskEnd): Boolean = !isAllDay && untimed != end
/** The due as sorting reads it, worked out once: a due without a time counts from the start of its day. */
internal val dueSortMillis: Long by lazy(LazyThreadSafetyMode.NONE) {
wallClock(TaskEnd.DUE)?.toEpochMilliseconds() ?: Long.MAX_VALUE
}
/** [end] as an editor holds it: an untimed end re-written in this device's zone, on its own date. */
fun formValueOf(end: TaskEnd): Instant? {
@@ -9,7 +9,7 @@ object TaskSorting {
val DEFAULT: Comparator<Task> = compareBy(
{ it.isCompleted },
{ it.due == null },
{ it.dueSortKey },
{ it.dueSortMillis },
{ -it.priority.ordinal },
{ it.title.lowercase() },
)
@@ -23,7 +23,7 @@ fun TaskSortOrder.comparator(): Comparator<Task> = when (this) {
TaskSortOrder.PRIORITY -> compareBy<Task>(
{ it.isCompleted },
{ -it.priority.ordinal },
{ it.dueSortKey },
{ it.dueSortMillis },
{ it.title.lowercase() },
)
TaskSortOrder.TITLE -> compareBy({ it.isCompleted }, { it.title.lowercase() })
@@ -33,5 +33,3 @@ fun TaskSortOrder.comparator(): Comparator<Task> = when (this) {
{ it.title.lowercase() },
)
}
private val Task.dueSortKey: Long get() = wallClock(TaskEnd.DUE)?.toEpochMilliseconds() ?: Long.MAX_VALUE
@@ -8,6 +8,7 @@ import de.jeanlucmakiola.agendula.domain.ical.ICalSerializer
import de.jeanlucmakiola.agendula.domain.ical.ICalValues
import de.jeanlucmakiola.agendula.domain.toICal
import de.jeanlucmakiola.agendula.domain.untimedDate
import de.jeanlucmakiola.agendula.domain.untimedWall
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
import kotlin.time.Instant
@@ -123,11 +124,12 @@ object ICalendarWriter {
// Read in UTC, matching the storage convention (see AllDayTime): an
// all-day value *is* UTC midnight of the intended calendar date.
rawProperty("$name;VALUE=DATE", instant.calendarDate(allDay = true).format(DATE))
} else if (task.untimed == end) {
} else if (task.untimed == end && (end == TaskEnd.DUE || task.rrule.isNullOrBlank() && task.rdate.isNullOrBlank())) {
// A date without a time is the wall clock alone: UTC would move it to
// another day for whoever imports it elsewhere (#101).
val date = instant.untimedDate(task.timeZone)
val wall = if (end == TaskEnd.START) date.atStartOfDay() else date.atTime(23, 59, 59)
// another day for whoever imports it elsewhere (#101). Not for the
// start of a series, whose UNTIL and RDATE stay UTC and §3.3.10 wants
// them floating alongside a floating start: that one goes out timed.
val wall = untimedWall(end, instant.untimedDate(task.timeZone))
rawProperty("$name;${ICalValues.NO_TIME_PARAM}=TRUE", ICalValues.formatFloating(wall))
} else {
rawProperty(name, formatUtc(instant))
@@ -21,6 +21,7 @@ import de.jeanlucmakiola.agendula.ui.common.toLocalTime
import de.jeanlucmakiola.agendula.domain.TaskDates
import de.jeanlucmakiola.agendula.domain.calendarDate
import de.jeanlucmakiola.agendula.domain.untimedInstantOf
import de.jeanlucmakiola.agendula.domain.untimedWall
import de.jeanlucmakiola.agendula.ui.common.localToInstant
import de.jeanlucmakiola.agendula.domain.TaskList
import de.jeanlucmakiola.agendula.domain.TaskReminder
@@ -131,8 +132,7 @@ data class TaskEditUiState(
fun localValueOf(end: TaskEnd): LocalDateTime? {
val value = valueOf(end) ?: return null
if (hasTime(end)) return LocalDateTime.of(value.calendarDate(allDay = false), value.toLocalTime())
val date = value.calendarDate(isAllDay)
return if (end == TaskEnd.START) date.atStartOfDay() else date.atTime(23, 59)
return untimedWall(end, value.calendarDate(isAllDay))
}
}
@@ -50,6 +50,19 @@ class StoredRemindersTest {
assertThat(stored(untimedStart, with = withTimes)).contains(TaskReminder(0, fromStart = true))
}
@Test
fun `an untouched untimed end from another zone does not count as moved`() {
val day = LocalDate.of(2026, 9, 3)
val current = testTask(id = 1, start = now - 30.days, due = untimedInstantOf(TaskEnd.DUE, day, ZoneId.of("America/New_York")))
.copy(untimed = TaskEnd.DUE, timeZone = "America/New_York")
val own = TaskReminder(0, fromStart = true)
val form = TaskForm(
title = "t", listId = 1, start = current.start, due = current.formValueOf(TaskEnd.DUE),
untimed = TaskEnd.DUE, reminders = listOf(own),
)
assertThat(stored(form, current)).containsExactly(own)
}
@Test
fun `a new task takes the defaults for the dates it has`() {
val form = TaskForm(title = "t", listId = 1, start = now + 2.days, due = now + 5.days)
@@ -6,6 +6,7 @@ import de.jeanlucmakiola.agendula.data.tasks.room.TaskAlarmEntity
import de.jeanlucmakiola.agendula.data.tasks.room.TaskEntity
import de.jeanlucmakiola.agendula.domain.PRIORITY_NONE
import de.jeanlucmakiola.agendula.domain.TaskEnd
import de.jeanlucmakiola.agendula.domain.untimedDate
import de.jeanlucmakiola.agendula.domain.TaskStatus
import de.jeanlucmakiola.agendula.domain.ical.ICalComponent
import de.jeanlucmakiola.agendula.domain.ical.ICalParser
@@ -554,11 +555,23 @@ class VTodoMapperTest {
@Test
fun `without a zone the untimed end is written floating, never as UTC`() {
val start = VTodoMapper.write(mixed.copy(timezone = null)).property("DTSTART")!!
assertThat(start.value).doesNotContain("Z")
assertThat(start.value).endsWith("T000000")
assertThat(start.param("TZID")).isNull()
assertThat(start.param(ICalValues.NO_TIME_PARAM)).isEqualTo("TRUE")
}
@Test
fun `a floating untimed start on a day without a midnight still reads as one`() {
val read = VTodoMapper.read(
vtodo(
"DTSTART;X-AGENDULA-NO-TIME=TRUE:20260906T000000",
"DUE;TZID=America/Santiago:20260906T170000",
),
).entity
assertThat(read.untimed).isEqualTo(TaskEnd.START)
assertThat(read.dtstart!!.untimedDate(read.timezone)).isEqualTo(java.time.LocalDate.of(2026, 9, 6))
}
@Test
fun `a mark on a DATE is ignored, the task is all-day`() {
val read = VTodoMapper.read(
@@ -145,4 +145,28 @@ class TaskFormWriterSeriesTest {
assertThat(edited.dtstart).isEqualTo(untimedInstantOf(TaskEnd.START, LocalDate.of(2026, 1, 12), berlin))
assertThat(edited.untimed).isEqualTo(TaskEnd.START)
}
@Test
fun `its exceptions move by whole days with it`() {
val berlin = java.time.ZoneId.of("Europe/Berlin")
val untimed = master.copy(
dtstart = untimedInstantOf(TaskEnd.START, LocalDate.of(2026, 3, 20), berlin),
due = null,
untimed = TaskEnd.START,
rrule = "FREQ=DAILY",
exdate = "20260327T000000",
)
val occurrence = untimedInstantOf(TaskEnd.START, LocalDate.of(2026, 3, 28), berlin)
val form = TaskForm(
title = "Water plants",
listId = 1,
start = untimedInstantOf(TaskEnd.START, LocalDate.of(2026, 3, 30), berlin),
due = Instant.parse("2026-03-31T10:00:00Z"),
untimed = TaskEnd.START,
rrule = "FREQ=DAILY",
)
val edited = TaskFormWriter.seriesEdit(untimed, occurrence, null, form, now, "Europe/Berlin")
assertThat(edited.exdate).isEqualTo("20260329T000000")
}
}
@@ -153,6 +153,15 @@ class ICalendarWriterTest {
assertThat(lines).contains("DUE:20250802T120000Z")
}
@Test
fun `the untimed start of a series goes out timed, beside its UTC rule`() {
val start = Instant.parse("2025-08-01T22:00:00Z")
val lines = linesOf(
write(task(start = start, due = noon, untimed = TaskEnd.START, timeZone = "Europe/Berlin", rrule = "FREQ=DAILY;UNTIL=20250901T215959Z")),
)
assertThat(lines).contains("DTSTART:20250801T220000Z")
}
@Test
fun `an exported untimed end reads back as one, on its date`() {
val start = Instant.parse("2025-08-01T22:00:00Z")