diff --git a/libraries/dateformatter/src/main/kotlin/io/element/android/libraries/dateformatter/di/DateFormatterModule.kt b/libraries/dateformatter/src/main/kotlin/io/element/android/libraries/dateformatter/di/DateFormatterModule.kt index 85deac5274..feab851a8b 100644 --- a/libraries/dateformatter/src/main/kotlin/io/element/android/libraries/dateformatter/di/DateFormatterModule.kt +++ b/libraries/dateformatter/src/main/kotlin/io/element/android/libraries/dateformatter/di/DateFormatterModule.kt @@ -21,6 +21,7 @@ import dagger.Module import dagger.Provides import io.element.android.libraries.di.AppScope import kotlinx.datetime.Clock +import kotlinx.datetime.TimeZone import java.util.* @Module @@ -31,4 +32,7 @@ object DateFormatterModule { @Provides fun providesLocale(): Locale = Locale.getDefault() + + @Provides + fun providesTimezone(): TimeZone = TimeZone.currentSystemDefault() } diff --git a/libraries/dateformatter/src/main/kotlin/io/element/android/libraries/dateformatter/impl/DefaultLastMessageFormatter.kt b/libraries/dateformatter/src/main/kotlin/io/element/android/libraries/dateformatter/impl/DefaultLastMessageFormatter.kt index f3b9d03bef..a466491766 100644 --- a/libraries/dateformatter/src/main/kotlin/io/element/android/libraries/dateformatter/impl/DefaultLastMessageFormatter.kt +++ b/libraries/dateformatter/src/main/kotlin/io/element/android/libraries/dateformatter/impl/DefaultLastMessageFormatter.kt @@ -39,6 +39,7 @@ import kotlin.math.absoluteValue class DefaultLastMessageFormatter @Inject constructor( private val clock: Clock, private val locale: Locale, + private val timezone: TimeZone, ) : LastMessageFormatter { private val onlyTimeFormatter: DateTimeFormatter by lazy { val pattern = DateFormat.getBestDateTimePattern(locale, "HH:mm") ?: "HH:mm" @@ -59,8 +60,8 @@ class DefaultLastMessageFormatter @Inject constructor( if (timestamp == null) return "" val now: Instant = clock.now() val tsInstant = Instant.fromEpochMilliseconds(timestamp) - val nowDateTime = now.toLocalDateTime(TimeZone.currentSystemDefault()) - val tsDateTime = tsInstant.toLocalDateTime(TimeZone.currentSystemDefault()) + val nowDateTime = now.toLocalDateTime(timezone) + val tsDateTime = tsInstant.toLocalDateTime(timezone) val isSameDay = nowDateTime.date == tsDateTime.date return when { isSameDay -> { @@ -80,7 +81,7 @@ class DefaultLastMessageFormatter @Inject constructor( return if (period.years.absoluteValue >= 1) { formatDateWithYear(date) } else if (period.days.absoluteValue < 2 && period.months.absoluteValue < 1) { - getRelativeDay(date.toInstant(TimeZone.currentSystemDefault()).toEpochMilliseconds()) + getRelativeDay(date.toInstant(timezone).toEpochMilliseconds()) } else { formatDateWithMonth(date) } diff --git a/libraries/dateformatter/src/test/kotlin/io/element/android/libraries/dateformatter/impl/DefaultLastMessageFormatterTest.kt b/libraries/dateformatter/src/test/kotlin/io/element/android/libraries/dateformatter/impl/DefaultLastMessageFormatterTest.kt index fbf038a562..c21dcf4230 100644 --- a/libraries/dateformatter/src/test/kotlin/io/element/android/libraries/dateformatter/impl/DefaultLastMessageFormatterTest.kt +++ b/libraries/dateformatter/src/test/kotlin/io/element/android/libraries/dateformatter/impl/DefaultLastMessageFormatterTest.kt @@ -19,6 +19,7 @@ package io.element.android.libraries.dateformatter.impl import com.google.common.truth.Truth.assertThat import io.element.android.libraries.dateformatter.LastMessageFormatter import kotlinx.datetime.Instant +import kotlinx.datetime.TimeZone import org.junit.Test import java.util.Locale @@ -43,7 +44,7 @@ class DefaultLastMessageFormatterTest { val now = "1980-04-06T18:35:24.00Z" val dat = "1980-04-06T18:35:24.00Z" val formatter = createFormatter(now) - assertThat(formatter.format(Instant.parse(dat).toEpochMilliseconds())).isEqualTo("20:35") + assertThat(formatter.format(Instant.parse(dat).toEpochMilliseconds())).isEqualTo("18:35") } @Test @@ -51,7 +52,7 @@ class DefaultLastMessageFormatterTest { val now = "1980-04-06T18:35:24.00Z" val dat = "1980-04-06T18:35:23.00Z" val formatter = createFormatter(now) - assertThat(formatter.format(Instant.parse(dat).toEpochMilliseconds())).isEqualTo("20:35") + assertThat(formatter.format(Instant.parse(dat).toEpochMilliseconds())).isEqualTo("18:35") } @Test @@ -59,7 +60,7 @@ class DefaultLastMessageFormatterTest { val now = "1980-04-06T18:35:24.00Z" val dat = "1980-04-06T18:34:24.00Z" val formatter = createFormatter(now) - assertThat(formatter.format(Instant.parse(dat).toEpochMilliseconds())).isEqualTo("20:34") + assertThat(formatter.format(Instant.parse(dat).toEpochMilliseconds())).isEqualTo("18:34") } @Test @@ -67,7 +68,7 @@ class DefaultLastMessageFormatterTest { val now = "1980-04-06T18:35:24.00Z" val dat = "1980-04-06T17:35:24.00Z" val formatter = createFormatter(now) - assertThat(formatter.format(Instant.parse(dat).toEpochMilliseconds())).isEqualTo("19:35") + assertThat(formatter.format(Instant.parse(dat).toEpochMilliseconds())).isEqualTo("17:35") } @Test @@ -100,6 +101,6 @@ class DefaultLastMessageFormatterTest { */ private fun createFormatter(@Suppress("SameParameterValue") currentDate: String): LastMessageFormatter { val clock = FakeClock().also { it.givenInstant(Instant.parse(currentDate)) } - return DefaultLastMessageFormatter(clock, Locale.US) + return DefaultLastMessageFormatter(clock, Locale.US, TimeZone.UTC) } }