This repository was archived by the owner on Jun 9, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +28
-7
lines changed
androidTest/kotlin/at/bitfire/ical4android/util
main/kotlin/at/bitfire/ical4android/util Expand file tree Collapse file tree 2 files changed +28
-7
lines changed Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments