feat(ui): format time with system locale default (#617)

This commit is contained in:
junkfood 2024-02-13 14:47:08 +08:00 committed by GitHub
parent 5b51e0bff7
commit 44752b8e7a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 8 additions and 4 deletions

View File

@ -2,13 +2,13 @@ package me.ash.reader.ui.ext
import android.annotation.SuppressLint
import android.content.Context
import androidx.core.os.ConfigurationCompat
import me.ash.reader.R
import java.text.DateFormat
import java.text.ParsePosition
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.Locale
@SuppressLint("SimpleDateFormat")
object DateFormat {
@ -25,18 +25,18 @@ fun Date.formatAsString(
onlyHourMinute: Boolean? = false,
atHourMinute: Boolean? = false,
): String {
val locale = ConfigurationCompat.getLocales(context.resources.configuration)[0]
val locale = Locale.getDefault()
val df = DateFormat.getDateInstance(DateFormat.FULL, locale)
return when {
onlyHourMinute == true -> {
SimpleDateFormat("HH:mm", locale).format(this)
this.toTimeString(context = context)
}
atHourMinute == true -> {
context.getString(
R.string.date_at_time,
df.format(this),
SimpleDateFormat("HH:mm", locale).format(this),
this.toTimeString(context = context),
)
}
@ -59,6 +59,10 @@ fun Date.formatAsString(
}
}
private fun Date.toTimeString(context: Context): String =
android.text.format.DateFormat.getTimeFormat(context).format(this)
private fun String.parseToDate(
patterns: Array<String> = arrayOf(
"yyyy-MM-dd'T'HH:mm:ss'Z'",