From d5d56ec127f177622f36c82a3a9724a6a5ecf1d0 Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 19 Jan 2018 20:16:04 +0100 Subject: [PATCH] fix off-by-day issue at syncing caldav events --- .../calendar/helpers/CalDAVHandler.kt | 16 +++++++++++++++- .../calendar/helpers/IcsImporter.kt | 3 ++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/CalDAVHandler.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/CalDAVHandler.kt index c7cbc03d2..42e853565 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/CalDAVHandler.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/CalDAVHandler.kt @@ -17,6 +17,7 @@ import com.simplemobiletools.calendar.models.EventType import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.PERMISSION_READ_CALENDAR import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_CALENDAR +import org.joda.time.DateTimeZone import java.util.* import kotlin.collections.ArrayList @@ -206,6 +207,7 @@ class CalDAVHandler(val context: Context) { CalendarContract.Events.DESCRIPTION, CalendarContract.Events.DTSTART, CalendarContract.Events.DTEND, + CalendarContract.Events.EVENT_TIMEZONE, CalendarContract.Events.DURATION, CalendarContract.Events.ALL_DAY, CalendarContract.Events.RRULE, @@ -222,8 +224,9 @@ class CalDAVHandler(val context: Context) { val id = cursor.getLongValue(CalendarContract.Events._ID) val title = cursor.getStringValue(CalendarContract.Events.TITLE) ?: "" val description = cursor.getStringValue(CalendarContract.Events.DESCRIPTION) ?: "" - val startTS = (cursor.getLongValue(CalendarContract.Events.DTSTART) / 1000).toInt() + var startTS = (cursor.getLongValue(CalendarContract.Events.DTSTART) / 1000).toInt() var endTS = (cursor.getLongValue(CalendarContract.Events.DTEND) / 1000).toInt() + val timeZone = cursor.getStringValue(CalendarContract.Events.EVENT_TIMEZONE) ?: "UTC" val allDay = cursor.getIntValue(CalendarContract.Events.ALL_DAY) val rrule = cursor.getStringValue(CalendarContract.Events.RRULE) ?: "" val location = cursor.getStringValue(CalendarContract.Events.EVENT_LOCATION) ?: "" @@ -231,6 +234,17 @@ class CalDAVHandler(val context: Context) { val originalInstanceTime = cursor.getLongValue(CalendarContract.Events.ORIGINAL_INSTANCE_TIME) val reminders = getCalDAVEventReminders(id) + if (allDay == 1 && timeZone == "UTC") { + val offset = DateTimeZone.getDefault().getOffset(System.currentTimeMillis()) / 1000 + val FIVE_HOURS = 5 * 60 * 60 + startTS -= offset + startTS += FIVE_HOURS + if (endTS != 0) { + endTS -= offset + endTS += FIVE_HOURS + } + } + if (endTS == 0) { val duration = cursor.getStringValue(CalendarContract.Events.DURATION) ?: "" endTS = startTS + Parser().parseDurationSeconds(duration) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsImporter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsImporter.kt index 4b7fb55f6..0f75d4292 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsImporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsImporter.kt @@ -53,8 +53,9 @@ class IcsImporter(val activity: SimpleActivity) { inputStream.bufferedReader().use { while (true) { var line = it.readLine() ?: break - if (line.trim().isEmpty()) + if (line.trim().isEmpty()) { continue + } if (line.substring(0, 1) == " ") { line = prevLine + line.trim()