Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package at.bitfire.synctools.icalendar

import androidx.annotation.VisibleForTesting
import at.bitfire.synctools.util.AndroidTimeUtils.toInstant
import at.bitfire.synctools.util.Utils.trimToNull
import net.fortuna.ical4j.data.CalendarOutputter
import net.fortuna.ical4j.model.Calendar
Expand All @@ -13,7 +14,6 @@ import net.fortuna.ical4j.model.ComponentList
import net.fortuna.ical4j.model.Parameter
import net.fortuna.ical4j.model.PropertyContainer
import net.fortuna.ical4j.model.PropertyList
import net.fortuna.ical4j.model.TemporalAdapter
import net.fortuna.ical4j.model.TimeZoneRegistryFactory
import net.fortuna.ical4j.model.component.VEvent
import net.fortuna.ical4j.model.component.VTimeZone
Expand Down Expand Up @@ -66,7 +66,10 @@ class ICalendarGenerator {
ical += exception

exception.dtStart<Temporal>()?.date?.let { start ->
if (earliestStart == null || TemporalAdapter.isBefore(start, earliestStart))
// Convert both Temporals to Instants for comparison, because they may be in different time zones. If conversion fails, ignore the start date of this exception.
val startInstant = runCatching { start.toInstant() }.getOrNull() ?: return@let
val earliestInstant = earliestStart?.let { runCatching { it.toInstant() }.getOrNull() }
if (earliestInstant == null || startInstant < earliestInstant)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: Maybe explicitly check for the expected types, handle them and throw a specific Exception if it's not one of the supported types? I'd find it easier to understand if it would be clear when toInstant is expected to work and when not, instead of using a somehow intransparent runCatching.

But didn't look in depth, just an idea.

Also have unassigned myself (not available tomorrow)

earliestStart = start
}
usedTimezoneIds += timeZonesOf(exception)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,27 @@ class ICalendarGeneratorTest {
assertTrue(result.isEmpty())
}

@Test
fun `Write event with UTC Instant exception DTSTART does not throw`() {
val iCal = StringWriter()
writer.write(AssociatedEvents(
main = VEvent(propertyListOf(
Uid("UTCTEST"),
DtStart(ZonedDateTime.of(LocalDateTime.parse("2025-08-22T05:30:00"), tzBerlin)),
DtStamp("20250822T000000Z"),
RRule<Temporal>("FREQ=DAILY;COUNT=3")
)),
exceptions = listOf(
VEvent(propertyListOf(
Uid("UTCTEST"),
RecurrenceId(Instant.parse("2025-08-22T03:30:00Z")),
DtStart(Instant.parse("2025-08-22T03:30:00Z")),
DtStamp("20250822T000000Z")
))
),
prodId = userAgent
), iCal)
assertTrue(iCal.toString().contains("BEGIN:VCALENDAR"))
}

}
Loading