Fix 24hr time format bug on older OSes.

This commit is contained in:
Cody Henthorne
2021-12-13 10:16:27 -05:00
parent a0235cbc6c
commit 93270b90df
5 changed files with 21 additions and 13 deletions

View File

@@ -1,5 +1,8 @@
package org.thoughtcrime.securesms.util
import android.content.Context
import android.os.Build
import android.text.format.DateFormat
import java.time.DayOfWeek
import java.time.Instant
import java.time.LocalDateTime
@@ -51,8 +54,12 @@ fun Long.toLocalTime(zoneId: ZoneId = ZoneId.systemDefault()): LocalTime {
/**
* Formats [LocalTime] as localized time. For example, "8:00 AM"
*/
fun LocalTime.formatHours(): String {
return DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT).format(this)
fun LocalTime.formatHours(context: Context): String {
return if (Build.VERSION.SDK_INT >= 26 || !DateFormat.is24HourFormat(context)) {
DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT).format(this)
} else {
DateTimeFormatter.ofPattern("HH:mm", Locale.getDefault()).format(this)
}
}
/**