git.iko.soy/opencode-android
on e37d1bb help sign in
22 lines 836 B history raw
1package soy.iko.opencode.ui.components
3import java.util.concurrent.TimeUnit
5/** Formats an epoch-millis timestamp as a short relative string (e.g. "3m", "2h", "5d"). */
6fun relativeTime(epochMillis: Long?): String {
7 if (epochMillis == null || epochMillis <= 0) return ""
8 val diff = System.currentTimeMillis() - epochMillis
9 if (diff < 0) return "now"
10 val minutes = TimeUnit.MILLISECONDS.toMinutes(diff)
11 if (minutes < 1) return "now"
12 if (minutes < 60) return "${minutes}m"
13 val hours = TimeUnit.MILLISECONDS.toHours(diff)
14 if (hours < 24) return "${hours}h"
15 val days = TimeUnit.MILLISECONDS.toDays(diff)
16 if (days < 7) return "${days}d"
17 val weeks = days / 7
18 if (weeks < 5) return "${weeks}w"
19 val months = days / 30
20 if (months < 12) return "${months}mo"
21 return "${days / 365}y"