Time.compareDuration
func Time.compareDuration(a: Time.Duration, b: Time.Duration, relativeTo: Time.Dated | null = null): intTime.compareDuration returns -1, 0 or 1 as a is shorter than, as long as or longer than b, by signed length, so a negative duration is shorter than any positive one. Years, months and weeks are measured from relativeTo, a week being 7 days, and a day is 24 hours unless relativeTo is a ZonedDateTime, whose days have their daylight-saving hours. It panics if either has years, months or weeks and relativeTo is null.
Details
- It compares lengths, not fields:
90mand1h30mcompare as0though they are not==. - Two durations with the same fields compare as
0withoutrelativeTo, even with years, months or weeks. - A
PlainDateTimegiven asrelativeTocounts as its date; its time is ignored. - The comparison operators do not apply to durations;
Time.compareDurationis their ordering.
Basic examples
let month = Time.duration(months: 1)
let feb = Time.plainDate(2026, 2, 1)
Time.compareDuration(month, Time.duration(days: 30), relativeTo: feb) // -1Durations without years, months or weeks need no relativeTo:
Time.compareDuration(30m, 1h) // -1
Time.compareDuration(90m, 1h) // 1
Time.compareDuration(3600s, 1h) // 0Scope
Days count as 24 hours:
Time.compareDuration(Time.duration(days: 1), 24h) // 0
Time.compareDuration(25h, Time.duration(days: 1)) // 1Subsecond fields count at their exact length:
let a = 1500ms
let b = 1s500ms
Time.compareDuration(a, b) // 0
Time.compareDuration(1ns, Time.duration()) // 1Named arguments
relativeTo
A month has the length of the month it starts in, so the same pair can compare either way:
let month = Time.duration(months: 1)
let days30 = Time.duration(days: 30)
month.compareDuration(days30, relativeTo: Time.plainDate(2026, 2, 1)) // -1
month.compareDuration(days30, relativeTo: Time.plainDate(2026, 3, 1)) // 1A year from a leap year has 366 days:
let year = Time.duration(years: 1)
let days365 = Time.duration(days: 365)
year.compareDuration(days365, relativeTo: Time.plainDate(2027, 1, 1)) // 0
year.compareDuration(days365, relativeTo: Time.plainDate(2028, 1, 1)) // 1A ZonedDateTime gives the 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.compareDuration(24h, relativeTo: z) // -1
day.compareDuration(23h, relativeTo: z) // 0
day.compareDuration(24h) // 0A PlainDateTime counts as its date; its time does not change the result:
let start = Time.plainDateTime(Time.plainDate(2026, 2, 1), Time.plainTime(hour: 23))
let month = Time.duration(months: 1)
month.compareDuration(Time.duration(days: 28), relativeTo: start) // 0Applications
Check how long a job ran against its time budget:
let started = Time.plainDateTime(Time.plainDate(2026, 5, 4), Time.plainTime(hour: 9))
let finished = Time.plainDateTime(Time.plainDate(2026, 5, 4), Time.plainTime(hour: 11, minute: 15))
let budget = 2h
let took = Time.until(started, finished)
if took.compareDuration(budget) > 0 {
println("over budget: ${took}")
} else {
println("on time: ${took}")
}over budget: PT2H15MSort durations by length. Time.compareDuration is a comparator, so it passes to Array.sortWith directly:
let timeouts = [2h, 30m, 4000s]
timeouts | Array.sortWith(Time.compareDuration) // [PT30M, PT4000S, PT2H]Find the months of a year in which a one-month plan is shorter than a 30-day trial:
let month = Time.duration(months: 1)
let trial = Time.duration(days: 30)
let shorter = Array.range(1, 13)
| Array.filter({ month.compareDuration(trial, relativeTo: Time.plainDate(2026, x, 1)) < 0 })
println(shorter)[2]Properties and relations
Swapping the arguments negates the result:
let a = 90m
let b = 2h
assert(Time.compareDuration(a, b) == -Time.compareDuration(b, a))The order agrees with Time.total in any unit:
let a = 1h30m
let b = 5000s
let longer = a.total(Time.Second{}) > b.total(Time.Second{})
assert((Time.compareDuration(a, b) > 0) == longer)0 means the same length, while == compares the fields as given:
let a = 90m
let b = 1h30m
assert(Time.compareDuration(a, b) == 0)
assert(a != b)Possible issues
Without relativeTo, a duration with years, months or weeks panics:
let month = Time.duration(months: 1)
month.compareDuration(Time.duration(days: 30))
// panic: Time.compareDuration: years, months or weeks need a relativeTo point to compareTwo durations with the same fields are the exception, so this does not panic:
Time.compareDuration(Time.duration(months: 1), Time.duration(months: 1)) // 0relativeTo takes a date, so an Instant such as Time.now() does not fit:
let month = Time.duration(months: 1)
month.compareDuration(Time.duration(days: 30), relativeTo: Time.now())
// error: cannot use Time.Instant where Time.Dated | null is expectedPlace the instant in a zone first:
let zone = Time.zone("Europe/Stockholm").unwrap()
let month = Time.duration(months: 1)
let now = Time.now().inZone(zone)
let shorter = month.compareDuration(Time.duration(days: 32), relativeTo: now) < 0
assert(shorter)The comparison operators do not apply to durations:
let a = 1h
let b = 30m
let longer = a > b
// error: > applies to a single numeric kind, string, or a named primitive type over one, not to Time.Duration== between two duration literals of the same length with different fields does not compile:
let same = 90m == 1h30m
// error: 90m == 1h30m compares the fields as written, and == is false: compare the lengths with Time.compareDuration(90m, 1h30m)Compare their lengths instead:
Time.compareDuration(90m, 1h30m) // 0See also
Time.compare, Time.total, Time.addDuration, Time.sign, Time.roundDuration, Time.duration, Array.sortWith, Duration literals
