toNanosecondOfDay

Returns the time as a nanosecond of a day, in 0 until 24 * 60 * 60 * 1_000_000_000.

Note that this is not the number of nanoseconds since the start of the day! For example, LocalTime(4, 0).toMillisecondOfDay() will return 4 * 60 * 60 * 1_000_000_000, the four hours' worth of nanoseconds, but because of DST transitions, when clocks show 4:00, in fact, three, four, five, or some other number of hours could have passed since the day started. Use Instant to perform reliable time arithmetic.

See also

Samples

import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.random.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   // Obtaining the number of nanoseconds a clock has to advance since 00:00 to reach the given time
check(LocalTime(0, 0, 0, 0).toNanosecondOfDay() == 0L)
check(LocalTime(0, 0, 0, 1).toNanosecondOfDay() == 1L)
check(LocalTime(0, 0, 1, 0).toNanosecondOfDay() == 1_000_000_000L)
check(LocalTime(0, 1, 0, 0).toNanosecondOfDay() == 60_000_000_000L)
check(LocalTime(1, 0, 0, 0).toNanosecondOfDay() == 3_600_000_000_000L)
check(LocalTime(1, 1, 1, 1).toNanosecondOfDay() == 3_600_000_000_000L + 60_000_000_000 + 1_000_000_000 + 1) 
   //sampleEnd
}
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
}