parse
Parses the ISO 8601 duration representation as a DatePeriod, for example, P1Y2M30D
.
This function is equivalent to DateTimePeriod.parse, but will fail if any of the time components are not zero.
See also
Throws
if the text cannot be parsed, the boundaries of DatePeriod are exceeded, or any time components are not zero.
Samples
import kotlinx.datetime.*
import kotlin.test.*
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.minutes
fun main() {
//sampleStart
// Parsing a string representation of a DatePeriod
// ISO duration strings are supported:
val datePeriod = DatePeriod.parse("P1Y16M60D")
check(datePeriod == DatePeriod(years = 2, months = 4, days = 60))
// it's okay to have time components as long as they amount to zero in total:
val datePeriodWithTimeComponents = DatePeriod.parse("P1Y2M3DT1H-60M")
check(datePeriodWithTimeComponents == DatePeriod(years = 1, months = 2, days = 3))
//sampleEnd
}