from Nanosecond Of Day
Constructs a LocalTime that represents the specified number of nanoseconds since the start of a calendar day.
See also
Throws
if nanosecondOfDay is outside the 0 until 86400 * 1_000_000_000
range, with 86400 being the number of seconds in a calendar day.
It is incorrect to pass to this function the number of nanoseconds that have physically elapsed since the start of the day. The reason is that, due to daylight-saving-time transitions, the number of nanoseconds since the start of the day is not a constant value: clocks could be shifted by an hour or more on some dates. Use Instant to perform reliable time arithmetic.
Samples
import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.random.*
import kotlin.test.*
fun main() {
//sampleStart
// Converting a LocalTime to the number of nanoseconds since the start of the day and back
val originalTime = LocalTime(
hour = Random.nextInt(24),
minute = Random.nextInt(60),
second = Random.nextInt(60),
nanosecond = Random.nextInt(1_000_000_000)
)
val nanosecondOfDay = originalTime.toNanosecondOfDay()
val reconstructedTime = LocalTime.fromNanosecondOfDay(nanosecondOfDay)
check(originalTime == reconstructedTime)
//sampleEnd
}