@@ -43,13 +43,13 @@ public struct Duration: Sendable {
43
43
internal var _high : Int64
44
44
45
45
@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
49
49
}
50
50
51
51
internal init ( _attoseconds: _Int128 ) {
52
- self . init ( _low : _attoseconds. low , high : _attoseconds. high )
52
+ self . init ( _high : _attoseconds. high , low : _attoseconds. low )
53
53
}
54
54
55
55
/// Construct a `Duration` by adding attoseconds to a seconds value.
@@ -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,21 +188,21 @@ 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
195
196
@available ( SwiftStdlib 5 . 7 , * )
196
- extension Duration : Codable {
197
+ extension Duration : Codable {
197
198
@available ( SwiftStdlib 5 . 7 , * )
198
199
public init ( from decoder: Decoder ) throws {
199
200
var container = try decoder. unkeyedContainer ( )
200
201
let high = try container. decode ( Int64 . self)
201
202
let low = try container. decode ( UInt64 . self)
202
- self . init ( _attoseconds : _Int128 ( high: high , low: low) )
203
+ self . init ( _high : high, low: low)
203
204
}
204
-
205
+
205
206
@available ( SwiftStdlib 5 . 7 , * )
206
207
public func encode( to encoder: Encoder ) throws {
207
208
var container = encoder. unkeyedContainer ( )
@@ -211,7 +212,7 @@ extension Duration: Codable {
211
212
}
212
213
213
214
@available ( SwiftStdlib 5 . 7 , * )
214
- extension Duration : Hashable {
215
+ extension Duration : Hashable {
215
216
@available ( SwiftStdlib 5 . 7 , * )
216
217
public func hash( into hasher: inout Hasher ) {
217
218
hasher. combine ( _attoseconds)
0 commit comments