From f3b5e3a4aefae1efae0491909b17b12fedfcabf7 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 18 Feb 2017 23:08:15 +0100 Subject: [PATCH] improve ics parsing by calculating start + duration if end is missing --- .../calendar/helpers/IcsParser.kt | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsParser.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsParser.kt index 9ac8cab5b..db11db738 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsParser.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsParser.kt @@ -18,6 +18,7 @@ class IcsParser { private val END = "END:VEVENT" private val DTSTART = "DTSTART" private val DTEND = "DTEND" + private val DURATION = "DURATION:" private val SUMMARY = "SUMMARY:" private val DESCRIPTION = "DESCRIPTION:" private val UID = "UID:" @@ -55,6 +56,9 @@ class IcsParser { curStart = getTimestamp(line.substring(DTSTART.length)) } else if (line.startsWith(DTEND)) { curEnd = getTimestamp(line.substring(DTEND.length)) + } else if (line.startsWith(DURATION)) { + val duration = line.substring(DURATION.length) + curEnd = calculateEndTime(curStart, duration) } else if (line.startsWith(SUMMARY)) { curTitle = line.substring(SUMMARY.length) curTitle = curTitle.substring(0, Math.min(curTitle.length, 50)) @@ -108,6 +112,24 @@ class IcsParser { } } + // P0DT1H0M0S + private fun calculateEndTime(curStart: Int, duration: String): Int { + val weeks = getDurationValue(duration, "W") + val days = getDurationValue(duration, "DT") + val hours = getDurationValue(duration, "H") + val minutes = getDurationValue(duration, "M") + val seconds = getDurationValue(duration, "S") + + val minSecs = 60 + val hourSecs = minSecs * 60 + val daySecs = hourSecs * 24 + val weekSecs = daySecs * 7 + + return curStart + seconds + (minutes * minSecs) + (hours * hourSecs) + (days * daySecs) + (weeks * weekSecs) + } + + private fun getDurationValue(duration: String, char: String): Int = Regex("[0-9]+(?=$char)").find(duration)?.value?.toInt() ?: 0 + private fun parseLongFormat(digitString: String): Int { val dateTimeFormat = DateTimeFormat.forPattern("yyyyMMddHHmmss") return dateTimeFormat.parseDateTime(digitString).withZoneRetainFields(DateTimeZone.UTC).seconds()