Skip to content

xtr.duration

The duration module leverages the ISO-8601 duration format.

of

of(obj: Object[Number]): String

Returns the ISO-8601 duration of the given obj, an Object of the form:

{
    years: Number, months: Number, days: Number,
    hours: Number, minutes: Number, seconds: Number
}

where all elements are optional and default to zero; keys outside this form are an error. The seconds part may be fractional ({seconds: 1.5} yields 'PT1.5S'); every other part must be a whole number. The result is in canonical form: zero-valued parts are omitted, e.g. {hours: 2} yields 'PT2H', and all-zero parts yield 'PT0S'.

Example

local parts = {
    years: 20, months: 3, days: 1,
    hours: 12, minutes: 30, seconds: 45
};

xtr.duration.of(parts)
Result
'P20Y3M1DT12H30M45S'


toParts

toParts(str: String): Object[Number]

Returns the constituent parts of the given str duration, as an Object of the form:

{
    years: Number, months: Number, days: Number,
    hours: Number, minutes: Number, seconds: Number
}

All six parts are always present. Time-only durations like 'PT4H' and negative durations like '-P1DT2H' are supported; a leading minus sign negates every part. Fractional seconds are kept exactly: toParts('PT1.5S') yields seconds: 1.5.

Example

xtr.duration.toParts('P20Y3M1DT12H30M45S')
Result
{
    years: 20, months: 3, days: 1,
    hours: 12, minutes: 30, seconds: 45
}