diff --git a/fleet/util/core/src/fleet/util/DateUtil.kt b/fleet/util/core/src/fleet/util/DateUtil.kt index 422f2dd6e41c..a060c55b168e 100644 --- a/fleet/util/core/src/fleet/util/DateUtil.kt +++ b/fleet/util/core/src/fleet/util/DateUtil.kt @@ -3,6 +3,7 @@ package fleet.util import java.time.* import java.time.format.DateTimeFormatter +import kotlin.time.toKotlinDuration private val TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm") private val DATE_FORMATTER = DateTimeFormatter.ofPattern("MMM d") @@ -32,4 +33,28 @@ fun LocalDateTime.formatTimestamp(shorten: Boolean): String { return DATE_WITH_YEAR_FORMATTER.format(ldt) } return FULL_FORMATTER.format(ldt) -} \ No newline at end of file +} + + +fun prettyDate(time: Long): String { + val ldt = utcSeconds(time / 1000) + val now = OffsetDateTime.now() + + val dif = Duration.between(ldt, now).toKotlinDuration() + return when { + dif.inWholeSeconds < 60 -> "Now" + dif.inWholeMinutes < 60 -> "${dif.inWholeMinutes} min ago" + dif.inWholeHours == 1L -> "1 hour ago" + dif.inWholeHours < 10 -> "${dif.inWholeHours} hours ago" + dif.inWholeDays == 0L && ldt.dayOfYear == now.dayOfYear -> "Today" + dif.inWholeDays < 365 -> { + val month = ldt.month.name.lowercase().replaceFirstChar { it.uppercase() }.substring(0, 3) + val day = ldt.dayOfMonth.toString().padStart(2, '0') + "${month} ${day}" + } + else -> { + val years = now.year - ldt.year + "$years year${if (years > 1) "s" else ""} ago" + } + } +}