mirror of
https://github.com/mihonapp/mihon.git
synced 2024-11-07 20:31:02 -05:00
Tweak relative date function (#6249)
* Tweak relative date function * Cleanup
This commit is contained in:
parent
845e061382
commit
f229a5e2ec
1 changed files with 15 additions and 4 deletions
|
@ -6,7 +6,6 @@ import java.text.DateFormat
|
||||||
import java.util.Calendar
|
import java.util.Calendar
|
||||||
import java.util.Date
|
import java.util.Date
|
||||||
import java.util.TimeZone
|
import java.util.TimeZone
|
||||||
import kotlin.math.floor
|
|
||||||
|
|
||||||
fun Date.toDateTimestampString(dateFormatter: DateFormat): String {
|
fun Date.toDateTimestampString(dateFormatter: DateFormat): String {
|
||||||
val date = dateFormatter.format(this)
|
val date = dateFormatter.format(this)
|
||||||
|
@ -98,7 +97,7 @@ fun Long.toLocalCalendar(): Calendar? {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private const val MILLISECONDS_IN_DAY = 86_400_000.0
|
private const val MILLISECONDS_IN_DAY = 86_400_000L
|
||||||
|
|
||||||
fun Date.toRelativeString(
|
fun Date.toRelativeString(
|
||||||
context: Context,
|
context: Context,
|
||||||
|
@ -109,8 +108,8 @@ fun Date.toRelativeString(
|
||||||
return dateFormat.format(this)
|
return dateFormat.format(this)
|
||||||
}
|
}
|
||||||
val now = Date()
|
val now = Date()
|
||||||
val difference = now.time - this.time
|
val difference = now.timeWithOffset.floorNearest(MILLISECONDS_IN_DAY) - this.timeWithOffset.floorNearest(MILLISECONDS_IN_DAY)
|
||||||
val days = floor(difference / MILLISECONDS_IN_DAY).toInt()
|
val days = difference.floorDiv(MILLISECONDS_IN_DAY).toInt()
|
||||||
return when {
|
return when {
|
||||||
difference < 0 -> context.getString(R.string.recently)
|
difference < 0 -> context.getString(R.string.recently)
|
||||||
difference < MILLISECONDS_IN_DAY -> context.getString(R.string.relative_time_today)
|
difference < MILLISECONDS_IN_DAY -> context.getString(R.string.relative_time_today)
|
||||||
|
@ -122,3 +121,15 @@ fun Date.toRelativeString(
|
||||||
else -> dateFormat.format(this)
|
else -> dateFormat.format(this)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val Date.timeWithOffset: Long
|
||||||
|
get() {
|
||||||
|
return Calendar.getInstance().run {
|
||||||
|
time = this@timeWithOffset
|
||||||
|
this@timeWithOffset.time + timeZone.rawOffset
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Long.floorNearest(to: Long): Long {
|
||||||
|
return this.floorDiv(to) * to
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue