@@ -86,16 +86,18 @@ public struct Duration: Sendable {
86
86
internal var _attoseconds : _Int128 {
87
87
_Int128 ( high: _high, low: _low)
88
88
}
89
+ }
89
90
91
+ @available ( SwiftStdlib 5 . 7 , * )
92
+ extension Duration {
90
93
/// The composite components of the `Duration`.
91
94
///
92
95
/// This is intended for facilitating conversions to existing time types. The
93
96
/// attoseconds value will not exceed 1e18 or be lower than -1e18.
97
+ @available ( SwiftStdlib 5 . 7 , * )
94
98
public var components : ( seconds: Int64 , attoseconds: Int64 ) {
95
- let seconds = _attoseconds / 1_000_000_000_000_000_000
96
- let attoseconds =
97
- Int64 ( ( _attoseconds - seconds * 1_000_000_000_000_000_000 ) )
98
- return ( Int64 ( seconds) , attoseconds)
99
+ let ( seconds, attoseconds) = _attoseconds. dividedBy1e18 ( )
100
+ return ( Int64 ( seconds) , Int64 ( attoseconds) )
99
101
}
100
102
}
101
103
@@ -109,8 +111,8 @@ extension Duration {
109
111
/// - Returns: A `Duration` representing a given number of seconds.
110
112
@available ( SwiftStdlib 5 . 7 , * )
111
113
public static func seconds< T: BinaryInteger > ( _ seconds: T ) -> Duration {
112
- return Duration ( _attoseconds: _Int128 ( seconds ) *
113
- 1_000_000_000_000_000_000 )
114
+ return Duration ( _attoseconds:
115
+ _Int128 ( seconds ) . multiplied ( by : 1_000_000_000_000_000_000 as UInt64 ) )
114
116
}
115
117
116
118
/// Construct a `Duration` given a number of seconds represented as a
@@ -121,8 +123,7 @@ extension Duration {
121
123
/// - Returns: A `Duration` representing a given number of seconds.
122
124
@available ( SwiftStdlib 5 . 7 , * )
123
125
public static func seconds( _ seconds: Double ) -> Duration {
124
- return Duration ( _attoseconds: _Int128 ( seconds *
125
- 1_000_000_000_000_000_000 ) )
126
+ return Duration ( _attoseconds: _Int128 ( seconds * 1_000_000_000_000_000_000 ) )
126
127
}
127
128
128
129
/// Construct a `Duration` given a number of milliseconds represented as a
@@ -135,8 +136,8 @@ extension Duration {
135
136
public static func milliseconds< T: BinaryInteger > (
136
137
_ milliseconds: T
137
138
) -> Duration {
138
- return Duration ( _attoseconds: _Int128 ( milliseconds ) *
139
- 1_000_000_000_000_000 )
139
+ return Duration ( _attoseconds:
140
+ _Int128 ( milliseconds ) . multiplied ( by : 1_000_000_000_000_000 as UInt64 ) )
140
141
}
141
142
142
143
/// Construct a `Duration` given a number of seconds milliseconds as a
@@ -147,8 +148,8 @@ extension Duration {
147
148
/// - Returns: A `Duration` representing a given number of milliseconds.
148
149
@available ( SwiftStdlib 5 . 7 , * )
149
150
public static func milliseconds( _ milliseconds: Double ) -> Duration {
150
- return Duration ( _attoseconds: _Int128 ( milliseconds *
151
- 1_000_000_000_000_000 ) )
151
+ return Duration ( _attoseconds:
152
+ _Int128 ( milliseconds * 1_000_000_000_000_000 ) )
152
153
}
153
154
154
155
/// Construct a `Duration` given a number of microseconds represented as a
@@ -161,8 +162,8 @@ extension Duration {
161
162
public static func microseconds< T: BinaryInteger > (
162
163
_ microseconds: T
163
164
) -> Duration {
164
- return Duration ( _attoseconds: _Int128 ( microseconds ) *
165
- 1_000_000_000_000 )
165
+ return Duration ( _attoseconds:
166
+ _Int128 ( microseconds ) . multiplied ( by : 1_000_000_000_000 as UInt64 ) )
166
167
}
167
168
168
169
/// Construct a `Duration` given a number of seconds microseconds as a
@@ -173,8 +174,8 @@ extension Duration {
173
174
/// - Returns: A `Duration` representing a given number of microseconds.
174
175
@available ( SwiftStdlib 5 . 7 , * )
175
176
public static func microseconds( _ microseconds: Double ) -> Duration {
176
- return Duration ( _attoseconds: _Int128 ( microseconds *
177
- 1_000_000_000_000 ) )
177
+ return Duration ( _attoseconds:
178
+ _Int128 ( microseconds * 1_000_000_000_000 ) )
178
179
}
179
180
180
181
/// Construct a `Duration` given a number of nanoseconds represented as a
@@ -187,8 +188,8 @@ extension Duration {
187
188
public static func nanoseconds< T: BinaryInteger > (
188
189
_ nanoseconds: T
189
190
) -> Duration {
190
- return Duration ( _attoseconds: _Int128 ( nanoseconds ) *
191
- 1_000_000_000 )
191
+ return Duration ( _attoseconds:
192
+ _Int128 ( nanoseconds ) . multiplied ( by : 1_000_000_000 ) )
192
193
}
193
194
}
194
195
0 commit comments