Move timed sub-tasks of an all-day routine in the device zone (#160)

This commit is contained in:
makiolaj committed 2026-11-18 21:00:19 +01:00
1 parent 3531e169cf
commit c835c13b49
3 files changed
+36 -8

No files matched your search

@@ -75,22 +75,34 @@ internal class SubtaskRestore(val restore: List<SubtaskBack>, val changed: List<
/**
* How far a series moved in [seriesZone]. A timed sub-task moves by the series'
* wall-clock change there, so it keeps its offset to the routine across a DST
* change whatever zone it is stored in; an all-day one moves by the change of
* the series' calendar date.
* wall-clock change in [timedZone], so it keeps its offset to the routine across
* a DST change whatever zone it is stored in; an all-day one moves by the change
* of the series' calendar date.
*/
internal class SeriesShift(from: Long, to: Long, private val seriesZone: ZoneId) {
internal class SeriesShift(from: Long, to: Long, seriesZone: ZoneId, private val timedZone: ZoneId = seriesZone) {
private val delta = Duration.between(local(from, seriesZone), local(to, seriesZone))
private val days = ChronoUnit.DAYS.between(local(from, seriesZone).toLocalDate(), local(to, seriesZone).toLocalDate())
/** [at] moved along. */
fun apply(at: Long, isAllDay: Boolean): Long {
val zone = if (isAllDay) dateZone(isAllDay = true, timeZone = null) else seriesZone
val zone = if (isAllDay) dateZone(isAllDay = true, timeZone = null) else timedZone
val moved = if (isAllDay) local(at, zone).plusDays(days) else local(at, zone).plus(delta)
return moved.atZone(zone).toInstant().toEpochMilli()
}
private fun local(at: Long, zone: ZoneId) = LocalDateTime.ofInstant(java.time.Instant.ofEpochMilli(at), zone)
companion object {
/**
* The shift of a series read in its own zone ([dateZone]). An all-day
* series' is UTC, where a timed sub-task would drift across DST, so
* that one moves in [floating], the device's zone.
*/
fun of(from: Long, to: Long, isAllDay: Boolean, timeZone: String?, floating: ZoneId): SeriesShift {
val seriesZone = dateZone(isAllDay, timeZone, floating)
return SeriesShift(from, to, seriesZone, timedZone = if (isAllDay) floating else seriesZone)
}
}
}
/**
@@ -259,7 +271,7 @@ internal object SubtaskReset {
val from = anchor(before) ?: return null
val to = anchor(after) ?: return null
val reader = MapColumnReader(after)
return SeriesShift(from, to, dateZone(reader.getBoolean(Tasks.IS_ALLDAY), reader.getString(Tasks.TZ), floating))
return SeriesShift.of(from, to, reader.getBoolean(Tasks.IS_ALLDAY), reader.getString(Tasks.TZ), floating)
}
/**
@@ -32,7 +32,6 @@ import de.jeanlucmakiola.agendula.domain.recurrence.RecurrenceExpander
import de.jeanlucmakiola.agendula.domain.recurrence.RecurrenceSpec
import de.jeanlucmakiola.agendula.domain.recurrence.RepeatFromCompletion
import java.time.ZoneId
import de.jeanlucmakiola.agendula.domain.recurrence.dateZone
import java.util.UUID
import javax.inject.Inject
import javax.inject.Singleton
@@ -761,7 +760,7 @@ class RoomTasksDataSource @Inject constructor(
): Pair<List<ResetSubtask>, Set<Long>> {
val from = master.recurrenceAnchor?.toEpochMilliseconds()
val to = newAnchor.toEpochMilliseconds()
val shift = from?.let { SeriesShift(it, to, dateZone(master.isAllDay, master.timezone, ZoneId.of(zone()))) }
val shift = from?.let { SeriesShift.of(it, to, master.isAllDay, master.timezone, ZoneId.of(zone())) }
val loaded = HashMap<Long, TaskEntity>().apply { put(master.id, master) }
val since = from?.let { SubtaskReset.cycleStart(it, cycleEnd.toEpochMilliseconds()) }
val reset = SubtaskReset.toReset(master.id, shift, since = since) { id ->
@@ -203,6 +203,23 @@ class SubtaskResetTest {
assertThat(moved - at("2026-10-26T08:00:00Z")).isEqualTo(at("2026-10-19T08:00:00Z") - at("2026-10-19T07:00:00Z"))
}
@Test
fun `a timed sub-task under an all-day routine keeps its local time across DST`() {
// A weekly all-day routine on Saturdays moves past the change to summer time in Berlin.
val shift = SeriesShift.of(at("2026-03-28T00:00:00Z"), at("2026-04-04T00:00:00Z"), isAllDay = true, timeZone = null, floating = berlin)
// Saturday 09:00 Berlin, still winter time, to the next Saturday 09:00, summer time.
assertThat(shift.apply(at("2026-03-28T08:00:00Z"), isAllDay = false)).isEqualTo(at("2026-04-04T07:00:00Z"))
assertThat(shift.apply(at("2026-03-29T00:00:00Z"), isAllDay = true)).isEqualTo(at("2026-04-05T00:00:00Z"))
}
@Test
fun `a provider series' shift reads an all-day one's timed sub-tasks in the device zone`() {
val before = mapOf<String, Any?>(Tasks.DUE to at("2026-03-28T00:00:00Z"), Tasks.IS_ALLDAY to 1L)
val after = mapOf<String, Any?>(Tasks.DUE to at("2026-04-04T00:00:00Z"), Tasks.IS_ALLDAY to 1L)
val shift = SubtaskReset.shiftOf(before, after, floating = berlin)!!
assertThat(shift.apply(at("2026-03-28T08:00:00Z"), isAllDay = false)).isEqualTo(at("2026-04-04T07:00:00Z"))
}
@Test
fun `an all-day sub-task follows the routine's change of date, also on a step under a day`() {
// An eight-hourly routine moves from 20:00 to 04:00 the next day in Berlin.