to Second Of Day
Returns the time as a second of a day, in 0 until 24 * 60 * 60
.
Note that this is not the number of seconds since the start of the day! For example, LocalTime(4, 0).toMillisecondOfDay()
will return 4 * 60 * 60
, the four hours worth of seconds, 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 seconds a clock has to advance since 00:00 to reach the given time
check(LocalTime(0, 0, 0, 0).toSecondOfDay() == 0)
check(LocalTime(0, 0, 0, 1).toSecondOfDay() == 0)
check(LocalTime(0, 0, 1, 0).toSecondOfDay() == 1)
check(LocalTime(0, 1, 0, 0).toSecondOfDay() == 60)
check(LocalTime(1, 0, 0, 0).toSecondOfDay() == 3_600)
check(LocalTime(1, 1, 1, 0).toSecondOfDay() == 3_600 + 60 + 1)
check(LocalTime(1, 1, 1, 999_999_999).toSecondOfDay() == 3_600 + 60 + 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 seconds since the start of the day and back
val secondsInDay = 24 * 60 * 60
val randomNumberOfSeconds = Random.nextInt(secondsInDay)
val time = LocalTime.fromSecondOfDay(randomNumberOfSeconds)
check(time.toSecondOfDay() == randomNumberOfSeconds)
check(time.nanosecond == 0) // sub-second part is zero
//sampleEnd
}