I would like to get the current time in microseconds since epoch with as much accuracy as possible, and in a monotonic way.
Does anyone have experience with that? I tried the following two approaches, but the first one is inaccurate and not monotonic, and I'm not sure the second won't have a weird behaviour when time jumps from one millisecond to the next.
(I'm working with Kotlin so I'm open to any kind of Java solution)
fun nowInMicrosSinceEpoch() : Long {
val now = Instant.now()
return now.toEpochMilli() * 1000L + (now.getNano().toLong() / 1000L)
}
fun nowInMicrosSinceEpoch() : Long {
return (System.currentTimeMillis() * 1000L) + (System.nanoTime() % 1_000_000L / 1000L)
}