DateTimePeriod

fun DateTimePeriod(years: Int = 0, months: Int = 0, days: Int = 0, hours: Int = 0, minutes: Int = 0, seconds: Int = 0, nanoseconds: Long = 0): DateTimePeriod(source)

Constructs a new DateTimePeriod. If all the time components are zero, returns a DatePeriod.

It is recommended to always name the arguments explicitly when constructing this manually, like DateTimePeriod(years = 1, months = 12, days = 16).

The passed numbers are not stored as is but are normalized instead for human readability, so, for example, DateTimePeriod(months = 24, days = 41) becomes DateTimePeriod(years = 2, days = 41).

If only a single component is set and is always non-zero and is semantically a fixed time interval (like "yearly" or "quarterly"), please consider using a multiple of DateTimeUnit instead. For example, instead of DateTimePeriod(months = 6), one can use DateTimeUnit.MONTH * 6.

Throws

if the total number of months in hours, minutes, seconds and nanoseconds overflows a Long.

Samples

import kotlinx.datetime.*
import kotlin.test.*
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.minutes

fun main() { 
   //sampleStart 
   // Constructing a DateTimePeriod using its constructor function
val dateTimePeriod = DateTimePeriod(months = 16, days = -60, hours = 16, minutes = -61)
check(dateTimePeriod.years == 1) // months overflowed to years
check(dateTimePeriod.months == 4) // 16 months % 12
check(dateTimePeriod.days == -60) // days are separate from months and are not normalized
check(dateTimePeriod.hours == 14) // the negative minutes overflowed to hours
check(dateTimePeriod.minutes == 59) // (-61 minutes) + (2 hours) * (60 minutes / hour)

val datePeriod = DateTimePeriod(months = 15, days = 3, hours = 2, minutes = -120)
check(datePeriod is DatePeriod) // the time components are zero 
   //sampleEnd
}