Converting a Duration to a Double of seconds?

Will this work?

extension Duration {
var inMilliseconds: Double {
Double((self * 1000).components.seconds)

Edit: no, that was not correct, this one is better:

extension Duration {
    var inMilliseconds: Double {
        let v = components
        return Double(v.seconds) * 1000 + Double(v.attoseconds) * 1e-15
    }
}

Gee, I've just corrected another mistake (was multiplying integer seconds - which may overflow) :sweat_smile: Shouldn't this be in the standard library or was it deliberately not included?

3 Likes