From 44752b8e7a684909a71e2ff52267eac6d8b7a43f Mon Sep 17 00:00:00 2001 From: junkfood <69683722+JunkFood02@users.noreply.github.com> Date: Tue, 13 Feb 2024 14:47:08 +0800 Subject: [PATCH] feat(ui): format time with system locale default (#617) --- app/src/main/java/me/ash/reader/ui/ext/DateExt.kt | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/me/ash/reader/ui/ext/DateExt.kt b/app/src/main/java/me/ash/reader/ui/ext/DateExt.kt index 09a0911..1958902 100644 --- a/app/src/main/java/me/ash/reader/ui/ext/DateExt.kt +++ b/app/src/main/java/me/ash/reader/ui/ext/DateExt.kt @@ -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 = arrayOf( "yyyy-MM-dd'T'HH:mm:ss'Z'",