properly parse .ics file events if lines are limited to 75-80 chars

This commit is contained in:
tibbi 2017-02-16 22:47:25 +01:00
parent 6487fded8e
commit 6d7426b49e
1 changed files with 25 additions and 7 deletions

View File

@ -36,10 +36,19 @@ class IcsParser {
try {
val dbHelper = DBHelper.newInstance(context)
val importIDs = dbHelper.getImportIds()
var prevLine = ""
File(path).inputStream().bufferedReader().use {
while (true) {
val line = it.readLine()?.trim() ?: break
var line = it.readLine() ?: break
if (line.trim().isEmpty())
continue
if (line.substring(0, 1) == " ") {
line = prevLine + line.trim()
eventsFailed--
}
if (line == BEGIN_EVENT) {
resetValues()
} else if (line.startsWith(DTSTART)) {
@ -62,6 +71,7 @@ class IcsParser {
eventsImported++
resetValues()
}
prevLine = line
}
}
} catch (e: Exception) {
@ -79,15 +89,18 @@ class IcsParser {
private fun getTimestamp(fullString: String): Int {
try {
if (fullString.startsWith(';')) {
return if (fullString.startsWith(';')) {
curFlags = curFlags or FLAG_ALL_DAY
val value = fullString.substring(fullString.lastIndexOf(':') + 1)
val value = fullString.substring(fullString.lastIndexOf(':') + 1).replace("T", "").replace("Z", "")
if (value.length == 14) {
parseLongFormat(value)
} else {
val dateTimeFormat = DateTimeFormat.forPattern("yyyyMMdd")
return dateTimeFormat.parseDateTime(value).withHourOfDay(1).seconds()
dateTimeFormat.parseDateTime(value).withHourOfDay(1).seconds()
}
} else {
val digitString = fullString.substring(1).replace("T", "").replace("Z", "")
val dateTimeFormat = DateTimeFormat.forPattern("yyyyMMddHHmmss")
return dateTimeFormat.parseDateTime(digitString).withZoneRetainFields(DateTimeZone.UTC).seconds()
parseLongFormat(digitString)
}
} catch (e: Exception) {
eventsFailed++
@ -95,6 +108,11 @@ class IcsParser {
}
}
private fun parseLongFormat(digitString: String): Int {
val dateTimeFormat = DateTimeFormat.forPattern("yyyyMMddHHmmss")
return dateTimeFormat.parseDateTime(digitString).withZoneRetainFields(DateTimeZone.UTC).seconds()
}
private fun resetValues() {
curStart = -1
curEnd = -1