Skip to content

Commit

Permalink
Fix locales for non-US devices
Browse files Browse the repository at this point in the history
  • Loading branch information
jocmp committed Dec 23, 2024
1 parent dce7864 commit 04bfccd
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
8 changes: 5 additions & 3 deletions capy/src/main/java/com/jocmp/capy/common/TimeFormats.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ internal object TimeFormats {
DateTimeFormatter.ISO_ZONED_DATE_TIME,
DateTimeFormatter.RFC_1123_DATE_TIME,
DateTimeFormatter.ofPattern(RSS_1123_UK).withLocale(Locale.UK),
) + patterns(DATETIME_PATTERNS)
) + usPatterns(DATETIME_PATTERNS)

fun dateFormatters() = patterns(DATE_PATTERNS)
fun dateFormatters() = usPatterns(DATE_PATTERNS)

private fun patterns(list: List<String>) = list.map { DateTimeFormatter.ofPattern(it) }
private fun usPatterns(list: List<String>) = list.map {
DateTimeFormatter.ofPattern(it).withLocale(Locale.US)
}
}
49 changes: 49 additions & 0 deletions capy/src/test/java/com/jocmp/capy/common/TimeHelpersTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@ import org.junit.Test
import java.time.ZoneOffset
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.util.Locale
import kotlin.test.AfterTest
import kotlin.test.assertEquals

class TimeHelpersTest {
private val defaultLocale = Locale.getDefault()

@AfterTest
fun teardown() {
Locale.setDefault(defaultLocale)
}

@Test
fun `parseISODate parses an offset ISO timestamp to UTC`() {
val result = "2023-12-25T09:00:00-05:00".toDateTime
Expand Down Expand Up @@ -118,6 +127,46 @@ class TimeHelpersTest {
}


@Test
fun `RFC1123 with timezone abbreviation non-US locale`() {
Locale.setDefault(Locale("en", "AU"))

val result = "Sun, 22 Dec 2024 08:18:56 EDT".toDateTime

val expected = ZonedDateTime.of(
2024,
12,
22,
13,
18,
56,
0,
ZoneOffset.UTC
)

assertEquals(expected = expected, actual = result)
}

@Test
fun `RFC1123 with timezone abbreviation non-English locale`() {
Locale.setDefault(Locale("es", "US"))

val result = "Sun, 22 Dec 2024 08:18:56 EDT".toDateTime

val expected = ZonedDateTime.of(
2024,
12,
22,
13,
18,
56,
0,
ZoneOffset.UTC
)

assertEquals(expected = expected, actual = result)
}

@Test
fun `Date only`() {
val result = "Sep 20, 2024".toDateTime
Expand Down

0 comments on commit 04bfccd

Please sign in to comment.