Skip to content

Commit

Permalink
Added support for OffsetDateTime
Browse files Browse the repository at this point in the history
Signed-off-by: Arnau Mora <[email protected]>
  • Loading branch information
ArnyminerZ committed Nov 5, 2023
1 parent b376d2e commit 5695613
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ->
Expand Down

0 comments on commit 5695613

Please sign in to comment.