Skip to content
This repository was archived by the owner on Jun 9, 2025. It is now read-only.

Commit 5f5d644

Browse files
committed
DateUtils.parseVTimeZone: return null instead of throwing a RuntimeException on parsing error
1 parent aa59e9e commit 5f5d644

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

lib/src/androidTest/kotlin/at/bitfire/ical4android/util/DateUtilsTest.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,23 @@ class DateUtilsTest {
6363
assertFalse(DateUtils.isDateTime(null))
6464
}
6565

66+
67+
@Test
68+
fun testParseVTimeZone() {
69+
val vtz = """
70+
BEGIN:VCALENDAR
71+
VERSION:2.0
72+
PRODID:DAVx5
73+
BEGIN:VTIMEZONE
74+
TZID:Asia/Shanghai
75+
END:VTIMEZONE
76+
END:VCALENDAR""".trimIndent()
77+
assertEquals("Asia/Shanghai", DateUtils.parseVTimeZone(vtz)?.timeZoneId?.value)
78+
}
79+
80+
@Test
81+
fun testParseVTimeZone_Invalid() {
82+
assertNull(DateUtils.parseVTimeZone("Invalid"))
83+
}
84+
6685
}

lib/src/main/kotlin/at/bitfire/ical4android/util/DateUtils.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,20 +123,22 @@ object DateUtils {
123123
fun isDateTime(date: DateProperty?) = date != null && date.date is DateTime
124124

125125
/**
126-
* Parses a VTIMEZONE definition to a VTimeZone object.
127-
* @param timezoneDef VTIMEZONE definition
128-
* @return parsed VTimeZone
129-
* @throws IllegalArgumentException when the timezone definition can't be parsed
126+
* Parses an iCalendar that only contains a `VTIMEZONE` definition to a VTimeZone object.
127+
*
128+
* @param timezoneDef iCalendar with only a `VTIMEZONE` definition
129+
*
130+
* @return parsed [VTimeZone], or `null` when the timezone definition can't be parsed
130131
*/
131-
fun parseVTimeZone(timezoneDef: String): VTimeZone {
132+
fun parseVTimeZone(timezoneDef: String): VTimeZone? {
132133
Ical4Android.checkThreadContextClassLoader()
133134

134135
val builder = CalendarBuilder(tzRegistry)
135136
try {
136137
val cal = builder.build(StringReader(timezoneDef))
137138
return cal.getComponent(VTimeZone.VTIMEZONE) as VTimeZone
138-
} catch (e: Exception) {
139-
throw IllegalArgumentException("Couldn't parse timezone definition")
139+
} catch (_: Exception) {
140+
// Couldn't parse timezone definition
141+
return null
140142
}
141143
}
142144

0 commit comments

Comments
 (0)