Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit b376d2ec authored by Arnau Mora's avatar Arnau Mora Committed by GitHub
Browse files

Handle contact events with date/time and time zone offset (#25)



* Added birthday with timezone test

Signed-off-by: default avatarArnau Mora <arnyminerz@proton.me>

* Added test for `ZonedDateTime`

Signed-off-by: default avatarArnau Mora <arnyminerz@proton.me>

* Add comment, remove println

* Updated test name and assert

Signed-off-by: default avatarArnau Mora <arnyminerz@proton.me>

* Test offset to UTC conversion, too

---------

Signed-off-by: default avatarArnau Mora <arnyminerz@proton.me>
Co-authored-by: default avatarRicki Hirner <hirner@bitfire.at>
parent 16650815
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -17,6 +17,8 @@ import org.junit.Test
import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.ZoneOffset
import java.time.ZonedDateTime

class EventBuilderTest {

@@ -62,6 +64,17 @@ class EventBuilderTest {
        }
    }

    @Test
    fun testStartDate_DateTime_WithOffset() {
        EventBuilder(Uri.EMPTY, null, Contact().apply {
            birthDay = Birthday(
                ZonedDateTime.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() {
+7 −8
Original line number Diff line number Diff line
@@ -11,12 +11,7 @@ import at.bitfire.vcard4android.Constants
import at.bitfire.vcard4android.Contact
import ezvcard.property.DateOrTimeProperty
import ezvcard.util.PartialDate
import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.ZoneOffset
import java.time.ZonedDateTime
import java.time.*
import java.time.format.DateTimeFormatter
import java.util.LinkedList
import java.util.Locale
@@ -62,8 +57,7 @@ class EventBuilder(dataRowUri: Uri, rawContactId: Long?, contact: Contact, readO
        val androidStr: String? =
            when {
                dateOrTime.date != null -> {
                    val date = dateOrTime.date
                    when (date) {
                    when (val date = dateOrTime.date) {
                        is Instant -> {
                            val utc = ZonedDateTime.ofInstant(date, ZoneOffset.UTC)
                            DateTimeFormatter.ofPattern(DATE_AND_TIME_FORMAT, Locale.US).format(utc)
@@ -72,6 +66,11 @@ 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 -> {
                            // time zones not supported by Contacts storage, convert to UTC
                            val utc = date.withZoneSameInstant(ZoneOffset.UTC)
                            DateTimeFormatter.ofPattern(DATE_AND_TIME_FORMAT, Locale.US).format(utc)
                        }
                        else ->
                            null
                    }