Skip to content

Instantly share code, notes, and snippets.

@y2k
Created July 17, 2020 15:25
Show Gist options
  • Save y2k/30d15c52a4d1f3723d7552af70d5d330 to your computer and use it in GitHub Desktop.
Save y2k/30d15c52a4d1f3723d7552af70d5d330 to your computer and use it in GitHub Desktop.
package kotlin.time
inline class Instant(val value: Duration) : Comparable<Instant> {
companion object {
val ZERO = Instant(Duration.ZERO)
fun fromUnixTimeSeconds(time: Long): Instant =
Instant(time.seconds)
fun fromUnixTimeMilliSeconds(time: Long): Instant =
Instant(time.milliseconds)
}
operator fun plus(duration: Duration): Instant =
Instant(value + duration)
operator fun minus(duration: Duration): Instant =
Instant(value - duration)
operator fun minus(duration: Instant): Duration =
value - duration.value
override operator fun compareTo(other: Instant): Int =
value.compareTo(other.value)
fun toUnixTimeSecondsLong(): Long =
value.inSeconds.toLong()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment