Skip to content

Commit

Permalink
feat(ui): format time with system locale default (#617)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunkFood02 authored Feb 13, 2024
1 parent 5b51e0b commit 44752b8
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions app/src/main/java/me/ash/reader/ui/ext/DateExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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),
)
}

Expand All @@ -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'",
Expand Down

0 comments on commit 44752b8

Please sign in to comment.