Use WEEK_IN_MILLIS flag to format weeks.

This commit is contained in:
Isira Seneviratne 2023-04-24 18:48:29 +05:30
parent 4260b21538
commit 7ff4b60cd1

View File

@ -6,13 +6,14 @@ import android.os.Build
import android.text.format.DateUtils import android.text.format.DateUtils
import com.github.libretube.R import com.github.libretube.R
import java.time.Instant import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime import java.time.LocalDateTime
import java.time.ZoneId import java.time.ZoneId
import java.time.format.DateTimeFormatter import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle import java.time.format.FormatStyle
import java.time.temporal.ChronoUnit import java.time.temporal.ChronoUnit
import kotlin.time.Duration import kotlin.time.Duration
import kotlinx.datetime.LocalDate import kotlinx.datetime.LocalDate as KotlinLocalDate
import kotlinx.datetime.toJavaLocalDate import kotlinx.datetime.toJavaLocalDate
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
@ -37,7 +38,7 @@ object TextUtils {
* @param date The date to parse * @param date The date to parse
* @return localized date string * @return localized date string
*/ */
fun localizeDate(date: LocalDate): String { fun localizeDate(date: KotlinLocalDate): String {
return date.toJavaLocalDate().format(MEDIUM_DATE_FORMATTER) return date.toJavaLocalDate().format(MEDIUM_DATE_FORMATTER)
} }
@ -66,31 +67,32 @@ object TextUtils {
// TODO: Use LocalDate.ofInstant() when it is available in SDK 34. // TODO: Use LocalDate.ofInstant() when it is available in SDK 34.
val date = LocalDateTime.ofInstant(Instant.ofEpochMilli(unixTime), ZoneId.systemDefault()) val date = LocalDateTime.ofInstant(Instant.ofEpochMilli(unixTime), ZoneId.systemDefault())
.toLocalDate() .toLocalDate()
val now = java.time.LocalDate.now() val now = LocalDate.now()
val weeks = date.until(now, ChronoUnit.WEEKS)
return if (weeks > 0) {
val months = date.until(now, ChronoUnit.MONTHS) val months = date.until(now, ChronoUnit.MONTHS)
return if (months > 0) {
val years = months / 12 val years = months / 12
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val (timeFormat, time) = when { val (timeFormat, time) = if (years > 0) {
years > 0 -> RelativeDateTimeFormatter.RelativeUnit.YEARS to years RelativeDateTimeFormatter.RelativeUnit.YEARS to years
months > 0 -> RelativeDateTimeFormatter.RelativeUnit.MONTHS to months } else {
else -> RelativeDateTimeFormatter.RelativeUnit.WEEKS to weeks RelativeDateTimeFormatter.RelativeUnit.MONTHS to months
} }
RelativeDateTimeFormatter.getInstance() RelativeDateTimeFormatter.getInstance()
.format(time.toDouble(), RelativeDateTimeFormatter.Direction.LAST, timeFormat) .format(time.toDouble(), RelativeDateTimeFormatter.Direction.LAST, timeFormat)
} else { } else {
val (timeAgoRes, time) = when { val (timeAgoRes, time) = if (years > 0) {
years > 0 -> R.plurals.years_ago to years R.plurals.years_ago to years
months > 0 -> R.plurals.months_ago to months } else {
else -> R.plurals.weeks_ago to weeks R.plurals.months_ago to months
} }
context.resources.getQuantityString(timeAgoRes, time.toInt(), time) context.resources.getQuantityString(timeAgoRes, time.toInt(), time)
} }
} else { } else {
DateUtils.getRelativeTimeSpanString(unixTime) val weeks = date.until(now, ChronoUnit.WEEKS)
val minResolution = if (weeks > 0) DateUtils.WEEK_IN_MILLIS else 0L
DateUtils.getRelativeTimeSpanString(unixTime, System.currentTimeMillis(), minResolution)
} }
} }