Since Durations can be both negative and positive, is there not a abs() method to get the positive difference?
Yes, I can convert both to time intervals and create the absolute value of the double and cast that back to a returned duration in an extension. But do I need to do that? I think that will be ok because of my application domain and time resolution required, but is there a non lossy way?
The abs generic function is constrained to Comparable & SignedNumeric, because AdditiveArithmetic didn't exist yet when it was written, which is why it isn't available on Duration. However, Duration has everything you need for it to be defined.
I would probably write it as:
func abs(_ a: Duration) -> Duration {
a < .zero ? .zero - a : a
}
Thanks everyone. These are good suggestions as I was lost in the weeds making it overly complicated. But yeah, having durations a little richer would be helpful in my migration to utilizing them. Specifically, having richer time formats such as mm:ss.SSS would be useful for my application domain.