ru.hnau.jutils.TimeValue.kt Maven / Gradle / Ivy
package ru.hnau.jutils
import java.util.*
class TimeValue(
@JvmField val milliseconds: Long
) : Comparable {
constructor(calendar: Calendar) : this(calendar.timeInMillis)
constructor(date: Date) : this(date.time)
companion object {
@JvmStatic
fun now() =
TimeValue(System.currentTimeMillis())
@JvmStatic
fun fromNanoseconds(nanoseconds: Long) =
fromMicroseconds(nanoseconds / 1000L)
@JvmStatic
fun fromMicroseconds(microseconds: Long) =
TimeValue(microseconds / 1000L)
@JvmStatic
fun isAfterPause(previousTime: TimeValue, pause: TimeValue) =
now() > previousTime + pause
@JvmStatic
fun isInPause(previousTime: TimeValue, pause: TimeValue) =
now() < previousTime + pause
@JvmField
val ZERO = TimeValue(0)
@JvmField
val MILLISECOND = TimeValue(1)
@JvmField
val SECOND = MILLISECOND * 1000
@JvmField
val MINUTE = SECOND * 60
@JvmField
val HOUR = MINUTE * 60
@JvmField
val DAY = HOUR * 24
@JvmField
val WEEK = DAY * 7
}
val seconds: Long
get() = milliseconds / 1000
val minutes: Long
get() = seconds / 60
val hours: Long
get() = minutes / 60
val days: Long
get() = hours / 24
val weeks: Long
get() = days / 7
val millisecondsDigits: Int
get() = (milliseconds - seconds * 1000).toInt()
val secondsDigits: Int
get() = (seconds - minutes * 60).toInt()
val minutesDigits: Int
get() = (minutes - hours * 60).toInt()
val hoursDigits: Int
get() = (hours - days * 24).toInt()
val daysDigits: Int
get() = (days - weeks * 7).toInt()
val weeksDigits: Int
get() = weeks.toInt()
fun toCalendar() = Calendar.getInstance().apply { timeInMillis = milliseconds }!!
fun toDate() = Date(milliseconds)
fun toMillisecondsString() = "${milliseconds}ms"
fun toSecondsString() = "${seconds}s"
fun toMinutesString() = "${minutes}m"
fun toHoursString() = "${hours}h"
fun toDaysString() = "${days}d"
fun toWeeksString() = "${weeks}w"
override fun equals(other: Any?) = (this === other) || (other is TimeValue && milliseconds == other.milliseconds)
override fun hashCode() = milliseconds.toInt()
override fun toString() = "TimeValue(milliseconds=$milliseconds)"
fun toLevelsString(levels: Int = 3) =
listOf(
weeksDigits to "w",
daysDigits to "d",
hoursDigits to "h",
minutesDigits to "m",
secondsDigits to "s",
millisecondsDigits to "ms"
)
.dropWhile { it.first <= 0 }
.take(levels)
.takeIfNotEmpty()
?.joinToString(
separator = "",
transform = { it.first.toString() + it.second }
) ?: "0ms"
operator fun plus(other: TimeValue) = TimeValue(this.milliseconds + other.milliseconds)
operator fun minus(other: TimeValue) = TimeValue(this.milliseconds - other.milliseconds)
operator fun times(count: Byte) = TimeValue(milliseconds * count)
operator fun times(count: Int) = TimeValue(milliseconds * count)
operator fun times(count: Long) = TimeValue(milliseconds * count)
operator fun times(count: Float) = TimeValue((milliseconds * count).toLong())
operator fun times(count: Double) = TimeValue((milliseconds * count).toLong())
operator fun div(count: Byte) = TimeValue(milliseconds / count)
operator fun div(count: Int) = TimeValue(milliseconds / count)
operator fun div(count: Long) = TimeValue(milliseconds / count)
operator fun div(count: Float) = TimeValue((milliseconds / count).toLong())
operator fun div(count: Double) = TimeValue((milliseconds / count).toLong())
operator fun div(other: TimeValue) = milliseconds.toDouble() / other.milliseconds.toDouble()
override operator fun compareTo(other: TimeValue) = this.milliseconds.compareTo(other.milliseconds)
}