mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-15 06:40:30 +05:30
Merge pull request #3140 from Isira-Seneviratne/ChronoUnit
Use ChronoUnit in formatRelativeDate().
This commit is contained in:
commit
77fb8ba4d4
@ -4,8 +4,11 @@ import android.icu.text.RelativeDateTimeFormatter
|
|||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.text.format.DateUtils
|
import android.text.format.DateUtils
|
||||||
import java.time.Instant
|
import java.time.Instant
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
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.util.*
|
import java.util.*
|
||||||
import kotlin.time.Duration
|
import kotlin.time.Duration
|
||||||
import kotlinx.datetime.LocalDate
|
import kotlinx.datetime.LocalDate
|
||||||
@ -23,10 +26,6 @@ object TextUtils {
|
|||||||
*/
|
*/
|
||||||
const val RESERVED_CHARS = "?:\"*|/\\<>\u0000"
|
const val RESERVED_CHARS = "?:\"*|/\\<>\u0000"
|
||||||
|
|
||||||
private const val weekInMillis: Long = 604800016
|
|
||||||
private const val monthInMillis: Long = 2629800000
|
|
||||||
private const val yearInMillis: Long = 31557600000
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Localize the date from a time string
|
* Localize the date from a time string
|
||||||
* @param date The date to parse
|
* @param date The date to parse
|
||||||
@ -61,15 +60,19 @@ object TextUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun formatRelativeDate(unixTime: Long): CharSequence {
|
fun formatRelativeDate(unixTime: Long): CharSequence {
|
||||||
val timeDiff = Instant.now().toEpochMilli() - unixTime
|
val date = LocalDateTime.ofInstant(Instant.ofEpochMilli(unixTime), ZoneId.systemDefault())
|
||||||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && timeDiff > weekInMillis) {
|
val now = LocalDateTime.now()
|
||||||
val fmt = RelativeDateTimeFormatter.getInstance()
|
val weeks = date.until(now, ChronoUnit.WEEKS)
|
||||||
|
|
||||||
|
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && weeks >= 1) {
|
||||||
|
val months = date.until(now, ChronoUnit.MONTHS)
|
||||||
val (timeFormat, time) = when {
|
val (timeFormat, time) = when {
|
||||||
timeDiff >= yearInMillis -> RelativeDateTimeFormatter.RelativeUnit.YEARS to timeDiff / yearInMillis
|
months / 12 > 0 -> RelativeDateTimeFormatter.RelativeUnit.YEARS to months / 12
|
||||||
timeDiff >= monthInMillis -> RelativeDateTimeFormatter.RelativeUnit.MONTHS to timeDiff / monthInMillis
|
months > 0 -> RelativeDateTimeFormatter.RelativeUnit.MONTHS to months
|
||||||
else -> RelativeDateTimeFormatter.RelativeUnit.WEEKS to timeDiff / weekInMillis
|
else -> RelativeDateTimeFormatter.RelativeUnit.WEEKS to weeks
|
||||||
}
|
}
|
||||||
fmt.format(time.toDouble(), RelativeDateTimeFormatter.Direction.LAST, timeFormat)
|
RelativeDateTimeFormatter.getInstance()
|
||||||
|
.format(time.toDouble(), RelativeDateTimeFormatter.Direction.LAST, timeFormat)
|
||||||
} else {
|
} else {
|
||||||
DateUtils.getRelativeTimeSpanString(unixTime)
|
DateUtils.getRelativeTimeSpanString(unixTime)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user