improve ics parsing by calculating start + duration if end is missing

This commit is contained in:
tibbi 2017-02-18 23:08:15 +01:00
parent a76a638fae
commit f3b5e3a4ae
1 changed files with 22 additions and 0 deletions

View File

@ -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()