@@ -43,13 +43,13 @@ public struct Duration: Sendable {
4343 internal var _high : Int64
4444
4545 @inlinable
46- internal init ( _low : UInt64 , high : Int64 ) {
47- self . _low = _low
48- self . _high = high
46+ internal init ( _high : Int64 , low : UInt64 ) {
47+ self . _low = low
48+ self . _high = _high
4949 }
5050
5151 internal init ( _attoseconds: _Int128 ) {
52- self . init ( _low : _attoseconds. low , high : _attoseconds. high )
52+ self . init ( _high : _attoseconds. high , low : _attoseconds. low )
5353 }
5454
5555 /// Construct a `Duration` by adding attoseconds to a seconds value.
@@ -86,16 +86,18 @@ public struct Duration: Sendable {
8686 internal var _attoseconds : _Int128 {
8787 _Int128 ( high: _high, low: _low)
8888 }
89+ }
8990
91+ @available ( SwiftStdlib 5 . 7 , * )
92+ extension Duration {
9093 /// The composite components of the `Duration`.
9194 ///
9295 /// This is intended for facilitating conversions to existing time types. The
9396 /// attoseconds value will not exceed 1e18 or be lower than -1e18.
97+ @available ( SwiftStdlib 5 . 7 , * )
9498 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) )
99101 }
100102}
101103
@@ -109,8 +111,8 @@ extension Duration {
109111 /// - Returns: A `Duration` representing a given number of seconds.
110112 @available ( SwiftStdlib 5 . 7 , * )
111113 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 ) )
114116 }
115117
116118 /// Construct a `Duration` given a number of seconds represented as a
@@ -121,8 +123,7 @@ extension Duration {
121123 /// - Returns: A `Duration` representing a given number of seconds.
122124 @available ( SwiftStdlib 5 . 7 , * )
123125 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 ) )
126127 }
127128
128129 /// Construct a `Duration` given a number of milliseconds represented as a
@@ -135,8 +136,8 @@ extension Duration {
135136 public static func milliseconds< T: BinaryInteger > (
136137 _ milliseconds: T
137138 ) -> 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 ) )
140141 }
141142
142143 /// Construct a `Duration` given a number of seconds milliseconds as a
@@ -147,8 +148,8 @@ extension Duration {
147148 /// - Returns: A `Duration` representing a given number of milliseconds.
148149 @available ( SwiftStdlib 5 . 7 , * )
149150 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 ) )
152153 }
153154
154155 /// Construct a `Duration` given a number of microseconds represented as a
@@ -161,8 +162,8 @@ extension Duration {
161162 public static func microseconds< T: BinaryInteger > (
162163 _ microseconds: T
163164 ) -> 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 ) )
166167 }
167168
168169 /// Construct a `Duration` given a number of seconds microseconds as a
@@ -173,8 +174,8 @@ extension Duration {
173174 /// - Returns: A `Duration` representing a given number of microseconds.
174175 @available ( SwiftStdlib 5 . 7 , * )
175176 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 ) )
178179 }
179180
180181 /// Construct a `Duration` given a number of nanoseconds represented as a
@@ -187,21 +188,21 @@ extension Duration {
187188 public static func nanoseconds< T: BinaryInteger > (
188189 _ nanoseconds: T
189190 ) -> 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 ) )
192193 }
193194}
194195
195196@available ( SwiftStdlib 5 . 7 , * )
196- extension Duration : Codable {
197+ extension Duration : Codable {
197198 @available ( SwiftStdlib 5 . 7 , * )
198199 public init ( from decoder: Decoder ) throws {
199200 var container = try decoder. unkeyedContainer ( )
200201 let high = try container. decode ( Int64 . self)
201202 let low = try container. decode ( UInt64 . self)
202- self . init ( _attoseconds : _Int128 ( high: high , low: low) )
203+ self . init ( _high : high, low: low)
203204 }
204-
205+
205206 @available ( SwiftStdlib 5 . 7 , * )
206207 public func encode( to encoder: Encoder ) throws {
207208 var container = encoder. unkeyedContainer ( )
@@ -211,7 +212,7 @@ extension Duration: Codable {
211212}
212213
213214@available ( SwiftStdlib 5 . 7 , * )
214- extension Duration : Hashable {
215+ extension Duration : Hashable {
215216 @available ( SwiftStdlib 5 . 7 , * )
216217 public func hash( into hasher: inout Hasher ) {
217218 hasher. combine ( _attoseconds)
0 commit comments