Skip to content

Commit 773561c

Browse files
ph1psstephentyrone
andauthored
Expose attosecond representation of Duration (#78202)
* Add Int128 to Duration * Update stdlib/public/core/Duration.swift Co-authored-by: Stephen Canon <[email protected]> * Add unit tests for duration + attoseconds * Add docs to duration + attosecond * Update docs for attoseconds property * Update stdlib.swift * Update stdlib.swift --------- Co-authored-by: Stephen Canon <[email protected]>
1 parent 73164a0 commit 773561c

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

stdlib/public/core/Duration.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,36 @@ extension Duration {
9999
}
100100
}
101101

102+
@available(SwiftStdlib 6.0, *)
103+
extension Duration {
104+
/// The number of attoseconds represented by this `Duration`.
105+
///
106+
/// This property provides direct access to the underlying number of attoseconds
107+
/// that the current `Duration` represents.
108+
///
109+
/// let d = Duration.seconds(1)
110+
/// print(d.attoseconds) // 1_000_000_000_000_000_000
111+
@available(SwiftStdlib 6.0, *)
112+
@_alwaysEmitIntoClient
113+
public var attoseconds: Int128 {
114+
Int128(_low: _low, _high: _high)
115+
}
116+
117+
/// Construct a `Duration` from the given number of attoseconds.
118+
///
119+
/// This directly constructs a `Duration` from the given number of attoseconds.
120+
///
121+
/// let d = Duration(attoseconds: 1_000_000_000_000_000_000)
122+
/// print(d) // 1.0 seconds
123+
///
124+
/// - Parameter attoseconds: The total duration expressed in attoseconds.
125+
@available(SwiftStdlib 6.0, *)
126+
@_alwaysEmitIntoClient
127+
public init(attoseconds: Int128) {
128+
self.init(_high: attoseconds._high, low: attoseconds._low)
129+
}
130+
}
131+
102132
@available(SwiftStdlib 5.7, *)
103133
extension Duration {
104134
/// Construct a `Duration` given a number of seconds represented as a

test/abi/macOS/arm64/stdlib.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,3 +891,6 @@ Added: _swift_cvw_destroyMultiPayloadEnumFN
891891
Added: _swift_cvw_initWithCopyMultiPayloadEnumFN
892892
Added: _swift_cvw_initWithTakeMultiPayloadEnumFN
893893
Added: _swift_cvw_initializeBufferWithCopyOfBufferMultiPayloadEnumFN
894+
895+
// SE-0457 Expose attosecond representation of Duration
896+
Added: _$ss8DurationV11attosecondss6Int128VvpMV

test/abi/macOS/x86_64/stdlib.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,3 +892,6 @@ Added: _swift_cvw_destroyMultiPayloadEnumFN
892892
Added: _swift_cvw_initWithCopyMultiPayloadEnumFN
893893
Added: _swift_cvw_initWithTakeMultiPayloadEnumFN
894894
Added: _swift_cvw_initializeBufferWithCopyOfBufferMultiPayloadEnumFN
895+
896+
// SE-0457 Expose attosecond representation of Duration
897+
Added: _$ss8DurationV11attosecondss6Int128VvpMV

test/stdlib/Duration.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,35 @@ if #available(SwiftStdlib 6.0, *) {
234234
let _ = Duration.nanoseconds( 170141183460469231731687303716 as Int128)
235235
#endif
236236
}
237+
238+
suite.test("attoseconds init") {
239+
let zero = Duration(attoseconds: 0)
240+
expectEqual(zero._high, 0)
241+
expectEqual(zero._low, 0)
242+
let one = Duration(attoseconds: 1)
243+
expectEqual(one._high, 0)
244+
expectEqual(one._low, 1)
245+
let mone = Duration(attoseconds: -1)
246+
expectEqual(mone._high, -1)
247+
expectEqual(mone._low, .max)
248+
let max = Duration(attoseconds: .max)
249+
expectEqual(max._high, 9_223_372_036_854_775_807)
250+
expectEqual(max._low, 18_446_744_073_709_551_615)
251+
let min = Duration(attoseconds: .min)
252+
expectEqual(min._high, -9_223_372_036_854_775_808)
253+
expectEqual(min._low, 0)
254+
}
255+
256+
suite.test("attoseconds var") {
257+
let zero = Duration(_high: 0, low: 0)
258+
expectEqual(zero.attoseconds, 0)
259+
let one = Duration(_high: 0, low: 1)
260+
expectEqual(one.attoseconds, 1)
261+
let mone = Duration(_high: -1, low: .max)
262+
expectEqual(mone.attoseconds, -1)
263+
let max = Duration(_high: 9_223_372_036_854_775_807, low: 18_446_744_073_709_551_615)
264+
expectEqual(max.attoseconds, .max)
265+
let min = Duration(_high: -9_223_372_036_854_775_808, low: 0)
266+
expectEqual(min.attoseconds, .min)
267+
}
237268
}

0 commit comments

Comments
 (0)