Skip to content

Commit d2537cf

Browse files
authored
Merge pull request #1124 from guzino/fix/localized-timestamp-locale
fix: localize app status and backup timestamps
2 parents 0e83f7d + e460f06 commit d2537cf

3 files changed

Lines changed: 42 additions & 2 deletions

File tree

app/src/main/java/to/bitkit/ext/DateTime.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import kotlin.time.Clock
2929
import kotlin.time.Duration.Companion.days
3030
import kotlin.time.Duration.Companion.milliseconds
3131
import kotlin.time.ExperimentalTime
32+
import java.text.DateFormat as JavaDateFormat
3233
import java.time.LocalDate as JavaLocalDate
3334
import kotlin.time.Instant as KInstant
3435

@@ -68,10 +69,10 @@ fun Long.toDateUTC(): String {
6869
return dateTime.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"))
6970
}
7071

71-
fun Long.toLocalizedTimestamp(locale: Locale = Locale.US): String {
72+
fun Long.toLocalizedTimestamp(locale: Locale = Locale.getDefault()): String {
7273
val date = Date(this)
7374
val formatter = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT, ULocale.forLocale(locale))
74-
?: return SimpleDateFormat("MMMM d, yyyy 'at' h:mm a", locale).format(date)
75+
?: return JavaDateFormat.getDateTimeInstance(JavaDateFormat.LONG, JavaDateFormat.SHORT, locale).format(date)
7576
return formatter.format(date)
7677
}
7778

app/src/test/java/to/bitkit/ext/DateTimeExtTest.kt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class DateTimeExtTest : BaseUnitTest() {
2020
val AFTERNOON: Instant = Instant.parse("2026-03-07T15:23:00Z")
2121
val MIDNIGHT: Instant = Instant.parse("2026-03-07T00:15:00Z")
2222
val NOON: Instant = Instant.parse("2026-03-07T12:05:00Z")
23+
24+
// Midday so the calendar day, and with it the month name, holds in every time zone
25+
val MIDDAY_IN_JULY: Long = Instant.parse("2026-07-27T12:00:00Z").toEpochMilli()
2326
}
2427

2528
@Test
@@ -261,5 +264,40 @@ class DateTimeExtTest : BaseUnitTest() {
261264
assertEquals(UiDateStyle.DATE_TIME, uiDateStyleFor(lateEvening.epochSecond.toULong(), today, bucharest))
262265
}
263266

267+
@Test
268+
fun `toLocalizedTimestamp uses the supplied locale`() {
269+
val inGerman = MIDDAY_IN_JULY.toLocalizedTimestamp(Locale.GERMANY)
270+
val inUs = MIDDAY_IN_JULY.toLocalizedTimestamp(Locale.US)
271+
272+
assertTrue(inGerman.contains("Juli"), "expected a German month name, got '$inGerman'")
273+
assertTrue(inUs.contains("July"), "expected an English month name, got '$inUs'")
274+
}
275+
276+
@Test
277+
fun `toLocalizedTimestamp follows the default locale when none is supplied`() {
278+
val original = Locale.getDefault()
279+
try {
280+
Locale.setDefault(Locale.GERMANY)
281+
val result = MIDDAY_IN_JULY.toLocalizedTimestamp()
282+
283+
assertTrue(result.contains("Juli"), "expected a German month name, got '$result'")
284+
} finally {
285+
Locale.setDefault(original)
286+
}
287+
}
288+
289+
@Test
290+
fun `toLocalizedTimestamp orders the date the way the locale does`() {
291+
val original = Locale.getDefault()
292+
try {
293+
Locale.setDefault(Locale.GERMANY)
294+
val result = MIDDAY_IN_JULY.toLocalizedTimestamp()
295+
296+
assertTrue(result.indexOf("27") < result.indexOf("Juli"), "expected day before month, got '$result'")
297+
} finally {
298+
Locale.setDefault(original)
299+
}
300+
}
301+
264302
private fun Instant.formattedInUtc(pattern: String) = formatted(pattern, Locale.US, UTC)
265303
}

changelog.d/next/1124.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
App Status and Backup timestamps now follow the device language instead of always using the US date format.

0 commit comments

Comments
 (0)