toDateTimePeriod

Constructs a DateTimePeriod from a Duration.

If the duration value is too big to be represented as a Long number of nanoseconds, the result will be Long.MAX_VALUE nanoseconds.

Pitfall: a DateTimePeriod obtained this way will always have zero date components. The reason is that even a Duration obtained via Duration.Companion.days just means a multiple of 24 hours, whereas in kotlinx-datetime, a day is a calendar day, which can be different from 24 hours. See DateTimeUnit.DayBased for details.

Samples

import kotlinx.datetime.*
import kotlin.test.*
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.minutes
fun main() { 
   //sampleStart 
   // Converting a Duration to a DateTimePeriod that only has time-based components
check(130.minutes.toDateTimePeriod() == DateTimePeriod(minutes = 130))
check(2.days.toDateTimePeriod() == DateTimePeriod(days = 0, hours = 48)) 
   //sampleEnd
}