From 56956136adbad655bc99ee8db75dfb013c9979e3 Mon Sep 17 00:00:00 2001 From: Arnau Mora Date: Sun, 5 Nov 2023 13:00:29 +0100 Subject: [PATCH] Added support for OffsetDateTime Signed-off-by: Arnau Mora --- .../vcard4android/contactrow/EventBuilderTest.kt | 15 ++++++++++++++- .../vcard4android/contactrow/EventBuilder.kt | 8 ++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/src/androidTest/java/at/bitfire/vcard4android/contactrow/EventBuilderTest.kt b/lib/src/androidTest/java/at/bitfire/vcard4android/contactrow/EventBuilderTest.kt index 69bf6cd..22d9298 100644 --- a/lib/src/androidTest/java/at/bitfire/vcard4android/contactrow/EventBuilderTest.kt +++ b/lib/src/androidTest/java/at/bitfire/vcard4android/contactrow/EventBuilderTest.kt @@ -13,10 +13,12 @@ import ezvcard.property.Anniversary import ezvcard.property.Birthday import ezvcard.util.PartialDate import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue import org.junit.Test import java.time.Instant import java.time.LocalDate import java.time.LocalDateTime +import java.time.OffsetDateTime import java.time.ZoneOffset import java.time.ZonedDateTime @@ -65,7 +67,7 @@ class EventBuilderTest { } @Test - fun testStartDate_DateTime_WithOffset() { + fun testStartDate_DateTime_WithOffset_ZonedDateTime() { EventBuilder(Uri.EMPTY, null, Contact().apply { birthDay = Birthday( ZonedDateTime.of(1984, 7, 20, 0, 0, 0, 0, ZoneOffset.ofHours(1)) @@ -75,6 +77,17 @@ class EventBuilderTest { } } + @Test + fun testStartDate_DateTime_WithOffset_OffsetDateTime() { + EventBuilder(Uri.EMPTY, null, Contact().apply { + birthDay = Birthday( + OffsetDateTime.of(1984, 7, 20, 0, 0, 0, 0, ZoneOffset.ofHours(1)) + ) + }, false).build().also { result -> + assertEquals("1984-07-19T23:00:00.000Z", result[0].values[CommonDataKinds.Event.START_DATE]) + } + } + @Test fun testStartDate_PartialDate_NoYear() { diff --git a/lib/src/main/java/at/bitfire/vcard4android/contactrow/EventBuilder.kt b/lib/src/main/java/at/bitfire/vcard4android/contactrow/EventBuilder.kt index 41e5ecd..7a6fe68 100644 --- a/lib/src/main/java/at/bitfire/vcard4android/contactrow/EventBuilder.kt +++ b/lib/src/main/java/at/bitfire/vcard4android/contactrow/EventBuilder.kt @@ -12,6 +12,7 @@ import at.bitfire.vcard4android.Contact import ezvcard.property.DateOrTimeProperty import ezvcard.util.PartialDate import java.time.* +import java.time.OffsetDateTime import java.time.format.DateTimeFormatter import java.util.LinkedList import java.util.Locale @@ -66,9 +67,12 @@ class EventBuilder(dataRowUri: Uri, rawContactId: Long?, contact: Contact, readO DateTimeFormatter.ofPattern(FULL_DATE_FORMAT, Locale.US).format(date) is LocalDateTime -> DateTimeFormatter.ofPattern(DATE_AND_TIME_FORMAT, Locale.US).format(date) - is ZonedDateTime -> { + is ZonedDateTime, is OffsetDateTime -> { // time zones not supported by Contacts storage, convert to UTC - val utc = date.withZoneSameInstant(ZoneOffset.UTC) + val utc = if (date is ZonedDateTime) + date.withZoneSameInstant(ZoneOffset.UTC) + else + (date as OffsetDateTime).atZoneSameInstant(ZoneOffset.UTC) DateTimeFormatter.ofPattern(DATE_AND_TIME_FORMAT, Locale.US).format(utc) } else ->