OOps, I forgot to add this then I was adding the format of listing klotter in near and home tab.
This commit is contained in:
109
shared/src/commonMain/kotlin/app/klottr/util/RelativeShort.kt
Normal file
109
shared/src/commonMain/kotlin/app/klottr/util/RelativeShort.kt
Normal file
@@ -0,0 +1,109 @@
|
||||
package app.klottr.util
|
||||
|
||||
/**
|
||||
* Superkort relativ tidsform för listor:
|
||||
*
|
||||
* Regler (alla jämförelser mot "nu"):
|
||||
* - < 1 h: "X min"
|
||||
* - >= 1 h < 24 h: "X h"
|
||||
* - >= 1 d < 2 d: "igår"
|
||||
* - >= 2 d < 7 d: "X d"
|
||||
* - >= 7 d < 365 d: "d MMM" (svenska månadsförkortningar)
|
||||
* - >= 365 d: "yyyy-MM-dd"
|
||||
*
|
||||
* Inga klockslag skrivs ut.
|
||||
*/
|
||||
fun formatRelativeShort(epochMs: Long, nowMs: Long = nowEpochMs()): String {
|
||||
val diffMs = (nowMs - epochMs).coerceAtLeast(0L)
|
||||
|
||||
val minuteMs = 60_000L
|
||||
val hourMs = 60 * minuteMs
|
||||
val dayMs = 24 * hourMs
|
||||
val weekMs = 7 * dayMs
|
||||
val yearMs = 365 * dayMs
|
||||
|
||||
return when {
|
||||
diffMs < hourMs -> {
|
||||
val mins = (diffMs / minuteMs).coerceAtLeast(1L)
|
||||
"$mins min"
|
||||
}
|
||||
diffMs < dayMs -> {
|
||||
val hours = (diffMs / hourMs).coerceAtLeast(1L)
|
||||
"$hours h"
|
||||
}
|
||||
diffMs < 2 * dayMs -> {
|
||||
"igår"
|
||||
}
|
||||
diffMs < weekMs -> {
|
||||
val days = (diffMs / dayMs)
|
||||
"$days d"
|
||||
}
|
||||
diffMs < yearMs -> {
|
||||
dateDMmm(epochMs)
|
||||
}
|
||||
else -> {
|
||||
dateDMmmYYYY(epochMs)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Plattformsabstraktioner som redan finns/skapats i UnitFormat.android/ios */
|
||||
expect fun nowEpochMs(): Long
|
||||
expect fun parseIsoToEpochMsOrNull(iso: String?): Long?
|
||||
|
||||
// ===== Hjälpare: datumformat helt i common (UTC-baserat) =====
|
||||
|
||||
private fun dateDMmm(epochMs: Long): String {
|
||||
// Beräkna d & m i UTC utan externa beroenden
|
||||
val ymd = utcYmd(epochMs)
|
||||
val month = when (ymd.month) {
|
||||
1 -> "jan"; 2 -> "feb"; 3 -> "mar"; 4 -> "apr"; 5 -> "maj"; 6 -> "jun"
|
||||
7 -> "jul"; 8 -> "aug"; 9 -> "sep"; 10 -> "okt"; 11 -> "nov"; else -> "dec"
|
||||
}
|
||||
return "${ymd.day} $month"
|
||||
}
|
||||
|
||||
private fun dateYMD(epochMs: Long): String {
|
||||
val ymd = utcYmd(epochMs)
|
||||
// yyyy-MM-dd
|
||||
val mm = if (ymd.month < 10) "0${ymd.month}" else "${ymd.month}"
|
||||
val dd = if (ymd.day < 10) "0${ymd.day}" else "${ymd.day}"
|
||||
return "${ymd.year}-$mm-$dd"
|
||||
}
|
||||
|
||||
private data class Ymd(val year: Int, val month: Int, val day: Int)
|
||||
|
||||
/**
|
||||
* Minimal UTC Y-M-D frammatning från epoch utan bibliotek.
|
||||
* Algoritm: civil-from-days (Howard Hinnant), förenklad för UTC.
|
||||
*/
|
||||
private fun utcYmd(epochMs: Long): Ymd {
|
||||
val days = floorDiv(epochMs, 86_400_000L).toInt()
|
||||
var z = days + 719468
|
||||
val era = if (z >= 0) z / 146097 else (z - 146096) / 146097
|
||||
val doe = z - era * 146097 // [0, 146096]
|
||||
val yoe = (doe - doe/1460 + doe/36524 - doe/146096) / 365 // [0, 399]
|
||||
var y = yoe + era * 400
|
||||
val doy = doe - (365*yoe + yoe/4 - yoe/100 + yoe/400) // [0, 365]
|
||||
val mp = (5*doy + 2) / 153 // [0, 11]
|
||||
val d = doy - (153*mp + 2)/5 + 1 // [1, 31]
|
||||
var m = mp + 3
|
||||
if (m > 12) { m -= 12; y += 1 }
|
||||
return Ymd(year = y + 1970, month = m, day = d)
|
||||
}
|
||||
|
||||
private fun floorDiv(x: Long, y: Long): Long {
|
||||
var r = x / y
|
||||
// Anpassning som matchar floor-division även för negativa tal (inte aktuellt här, men korrekt)
|
||||
if ((x xor y) < 0 && (r * y != x)) r--
|
||||
return r
|
||||
}
|
||||
|
||||
private fun dateDMmmYYYY(epochMs: Long): String {
|
||||
val ymd = utcYmd(epochMs)
|
||||
val month = when (ymd.month) {
|
||||
1 -> "jan"; 2 -> "feb"; 3 -> "mar"; 4 -> "apr"; 5 -> "maj"; 6 -> "jun"
|
||||
7 -> "jul"; 8 -> "aug"; 9 -> "sep"; 10 -> "okt"; 11 -> "nov"; else -> "dec"
|
||||
}
|
||||
return "${ymd.day} $month ${ymd.year}"
|
||||
}
|
||||
Reference in New Issue
Block a user