Time.roundDuration
func Time.roundDuration(d: Time.Duration, smallestUnit: Time.Unit | null = null, largestUnit: Time.Unit | null = null, increment: int = 1, mode: Time.RoundingMode = Time.HalfExpand{}, relativeTo: Time.Dated | null = null): Time.DurationTime.roundDuration balances d so that no field below largestUnit exceeds its next unit, and rounds it to a multiple of increment of smallestUnit by mode, dropping the smaller fields. largestUnit defaults to the larger of the largest nonzero unit of d and smallestUnit; years, months and weeks need relativeTo, as in Time.total. increment follows Time.round for a time unit, and may be any positive one for a day or calendar unit when largestUnit is smallestUnit. It panics if neither unit is given, if largestUnit is smaller than smallestUnit, if increment is below 1, on years, months or weeks without relativeTo, and on an increment that breaks those rules.
Details
largestUnitalone keeps the length; onlysmallestUnitchanges it.- A duration with years, months or weeks, or a
largestUnitorsmallestUnitofYear,MonthorWeek, needsrelativeTo. A day is 24 hours unlessrelativeTois aZonedDateTime, whose days have their daylight-saving hours. - The default
largestUnitis the largest nonzero field ofd, orsmallestUnitwhen that is larger, so roundingPT59M50Sto minutes givesPT60M, notPT1H. PasslargestUnitto carry into the next unit. - The rounding modes treat a negative duration as a number below zero:
Ceilmoves it toward zero,Flooraway from it.
Basic examples
Time.roundDuration(90m, largestUnit: Time.Hour{}) // PT1H30M
Time.roundDuration(1h29m, smallestUnit: Time.Hour{}) // PT1H
Time.roundDuration(1h30m, smallestUnit: Time.Hour{}) // PT2HGive both units to balance and round in one call:
Time.roundDuration(7384s, largestUnit: Time.Hour{}, smallestUnit: Time.Minute{}) // PT2H3MScope
largestUnit balances up, carrying seconds into minutes, hours and days:
let d = 100000s
d.roundDuration(largestUnit: Time.Day{}) // P1DT3H46M40S
d.roundDuration(largestUnit: Time.Hour{}) // PT27H46M40SIt also balances down, spreading larger fields into a smaller unit:
1h30m.roundDuration(largestUnit: Time.Minute{}) // PT90M
Time.duration(days: 1).roundDuration(largestUnit: Time.Hour{}) // PT24HFields above smallestUnit stay as they are; only the part below it rounds:
Time.duration(days: 3, hours: 13).roundDuration(smallestUnit: Time.Day{}) // P4D
1234567ms.roundDuration(smallestUnit: Time.Second{}) // PT1235SA negative duration rounds by its length and keeps its sign:
Time.negate(2h30m).roundDuration(smallestUnit: Time.Hour{}) // -PT3HA zero duration stays zero:
Time.duration().roundDuration(smallestUnit: Time.Second{}) // PT0SNamed arguments
smallestUnit
The fields below smallestUnit are dropped after rounding:
let d = 2h37m25s
d.roundDuration(smallestUnit: Time.Minute{}) // PT2H37M
d.roundDuration(smallestUnit: Time.Hour{}) // PT3HlargestUnit
largestUnit sets the top field of the result. Rounding can carry into it:
let d = 59m50s
d.roundDuration(smallestUnit: Time.Minute{}) // PT60M
d.roundDuration(smallestUnit: Time.Minute{}, largestUnit: Time.Hour{}) // PT1Hincrement
increment rounds to a multiple of several units. For a time unit it must divide the count in the next unit, 60 minutes or 24 hours, and be smaller than it:
let d = 37m
d.roundDuration(smallestUnit: Time.Minute{}, increment: 15) // PT30M
37h.roundDuration(smallestUnit: Time.Hour{}, increment: 12) // PT36HFor days and calendar units any increment works, as long as largestUnit is smallestUnit:
Time.duration(days: 10).roundDuration(smallestUnit: Time.Day{}, increment: 7) // P7D
let jan = Time.plainDate(2026, 1, 1)
Time.duration(months: 5).roundDuration(smallestUnit: Time.Month{}, increment: 2, relativeTo: jan) // P6Mmode
mode picks the direction. Ceil and Floor go toward positive and negative infinity, Expand and Trunc away from and toward zero:
let d = Time.negate(37m)
d.roundDuration(smallestUnit: Time.Minute{}, increment: 15, mode: Time.Ceil{}) // -PT30M
d.roundDuration(smallestUnit: Time.Minute{}, increment: 15, mode: Time.Floor{}) // -PT45M
d.roundDuration(smallestUnit: Time.Minute{}, increment: 15, mode: Time.Expand{}) // -PT45M
d.roundDuration(smallestUnit: Time.Minute{}, increment: 15, mode: Time.Trunc{}) // -PT30MThe Half modes go to the nearest multiple and differ only on a tie. HalfEven takes the even multiple:
let tie = 1h30m
tie.roundDuration(smallestUnit: Time.Hour{}) // PT2H
tie.roundDuration(smallestUnit: Time.Hour{}, mode: Time.HalfTrunc{}) // PT1H
let even = Time.HalfEven{}
2500ms.roundDuration(smallestUnit: Time.Second{}, mode: even) // PT2S
3500ms.roundDuration(smallestUnit: Time.Second{}, mode: even) // PT4SrelativeTo
Months have different lengths, so the same 45 days balance differently from January and from February:
let d = Time.duration(days: 45)
d.roundDuration(largestUnit: Time.Month{}, relativeTo: Time.plainDate(2026, 1, 1)) // P1M14D
d.roundDuration(largestUnit: Time.Month{}, relativeTo: Time.plainDate(2026, 2, 1)) // P1M17DWeeks and years count from relativeTo too:
let jan = Time.plainDate(2026, 1, 1)
Time.duration(days: 10).roundDuration(smallestUnit: Time.Week{}, relativeTo: jan) // P1W
Time.duration(months: 14).roundDuration(largestUnit: Time.Year{}, relativeTo: jan) // P1Y2M
Time.duration(years: 1, months: 7).roundDuration(smallestUnit: Time.Year{}, relativeTo: jan) // P2YA ZonedDateTime gives each day its real length. Stockholm moves its clocks forward on 29 March 2026, so that day has 23 hours:
let z = Time.parseZoned("2026-03-29T00:00:00+01:00[Europe/Stockholm]").unwrap()
let day = Time.duration(days: 1)
day.roundDuration(largestUnit: Time.Hour{}, relativeTo: z) // PT23H
day.roundDuration(largestUnit: Time.Hour{}) // PT24HApplications
Show how long a job ran, to the minute:
let start = Time.plainDateTime(Time.plainDate(2026, 5, 4), Time.plainTime(hour: 9, minute: 12, second: 40))
let end = Time.plainDateTime(Time.plainDate(2026, 5, 4), Time.plainTime(hour: 11, minute: 50, second: 5))
let took = Time.until(start, end, largestUnit: Time.Second{})
println(took)
println(took.roundDuration(largestUnit: Time.Hour{}, smallestUnit: Time.Minute{}))PT9445S
PT2H37MBill each session in started blocks of 15 minutes:
let sessions = [7m, 31m, 45m]
let billed = sessions
| Array.map({ x.roundDuration(smallestUnit: Time.Minute{}, increment: 15, mode: Time.Ceil{}) })
println(billed)[PT15M, PT45M, PT45M]Turn a count of seconds into days, hours and minutes for a status line:
func uptime(seconds: int): string {
let d = Time.duration(seconds: seconds)
"up ${d.roundDuration(largestUnit: Time.Day{}, smallestUnit: Time.Minute{})}"
}
println(uptime(273_845))
println(uptime(59))up P3DT4H4M
up PT1MProperties and relations
Balancing alone keeps the length, which Time.compareDuration and Time.total measure:
let d = 100000s
let balanced = d.roundDuration(largestUnit: Time.Day{})
assert(Time.compareDuration(d, balanced) == 0)
assert(Time.total(d, Time.Second{}) == Time.total(balanced, Time.Second{}))
assert(d != balanced)Rounding a duration and negating it commute for a symmetric mode such as the default:
let d = 37m
let unit = Time.Minute{}
let a = Time.negate(d.roundDuration(smallestUnit: unit, increment: 15))
let b = Time.negate(d).roundDuration(smallestUnit: unit, increment: 15)
assert(a == b)Ceil never gives less than the input, and Floor never more:
let d = 37m
let up = d.roundDuration(smallestUnit: Time.Minute{}, increment: 15, mode: Time.Ceil{})
let down = d.roundDuration(smallestUnit: Time.Minute{}, increment: 15, mode: Time.Floor{})
assert(Time.compareDuration(up, d) >= 0)
assert(Time.compareDuration(down, d) <= 0)Possible issues
Without a unit there is nothing to do, and it panics:
Time.roundDuration(90m)
// panic: Time.roundDuration: roundDuration needs a smallest or a largest unitMonths, years and weeks need relativeTo, in d or as a unit:
Time.duration(months: 1).roundDuration(smallestUnit: Time.Day{})
// panic: Time.roundDuration: balancing months needs a relativeTo pointTime.duration(days: 45).roundDuration(largestUnit: Time.Month{})
// panic: Time.roundDuration: balancing to months needs a relativeTo pointAn increment of a time unit must divide the next unit and be smaller than it:
let d = 37m
d.roundDuration(smallestUnit: Time.Minute{}, increment: 7)
// panic: Time.roundDuration: the increment for minutes must divide 60 and be smaller, not 7The increment cannot be the whole next unit either:
let d = 37m
d.roundDuration(smallestUnit: Time.Minute{}, increment: 60)
// panic: Time.roundDuration: the increment for minutes must divide 60 and be smaller, not 60An increment of days or calendar units works only when largestUnit is smallestUnit:
let jan = Time.plainDate(2026, 1, 1)
let tenDays = Time.duration(days: 10)
tenDays.roundDuration(smallestUnit: Time.Day{}, increment: 7, largestUnit: Time.Week{}, relativeTo: jan)
// panic: Time.roundDuration: a date unit with an increment above 1 needs the largest unit to be the smallestBalancing a long span into a small unit can overflow a field:
let jan = Time.plainDate(2026, 1, 1)
Time.duration(years: 300).roundDuration(largestUnit: Time.Nanosecond{}, relativeTo: jan)
// panic: Time.roundDuration: a balanced field does not fit an intSee also
Time.round, Time.total, Time.compareDuration, Time.addDuration, Time.until, Time.duration, Time.Unit, Time.RoundingMode, Time.Duration
