Skip to content

Commit 88e29fa

Browse files
[6.2] Add static .nanoseconds(_: Double) to Duration (#81210) (#81236)
SE-0329 defines the following static factory methods: ``` public static func seconds<T: BinaryInteger>(_ seconds: T) -> Duration public static func seconds(_ seconds: Double) -> Duration public static func milliseconds<T: BinaryInteger>(_ milliseconds: T) -> Duration public static func milliseconds(_ milliseconds: Double) -> Duration public static func microseconds<T: BinaryInteger>(_ microseconds: T) -> Duration public static func microseconds(_ microseconds: Double) -> Duration public static func nanoseconds<T: BinaryInteger>(_ value: T) -> Duration ``` For no good reason, the obvious additional method: ``` public static func nanoseconds(_ nanoseconds: Double) -> Duration ``` was omitted. After talking this through with the LSG, we have decided that this is simply a bug, and we will add this method without formal evolution review.
1 parent f3380ef commit 88e29fa

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

stdlib/public/core/Duration.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,17 @@ extension Duration {
250250
let highScaled = high * 1_000_000_000
251251
return Duration(_high: highScaled + Int64(lowScaled.high), low: lowScaled.low)
252252
}
253+
254+
/// Construct a `Duration` given a number of seconds nanoseconds as a
255+
/// `Double` by converting the value into the closest attosecond scale value.
256+
///
257+
/// let d: Duration = .nanoseconds(382.9)
258+
///
259+
/// - Returns: A `Duration` representing a given number of nanoseconds.
260+
@available(SwiftStdlib 6.2, *)
261+
public static func nanoseconds(_ nanoseconds: Double) -> Duration {
262+
Duration(nanoseconds, scale: 1_000_000_000)
263+
}
253264
}
254265

255266
@available(SwiftStdlib 5.7, *)

test/abi/macOS/arm64/stdlib.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1127,4 +1127,7 @@ Added: $ld$previous$@rpath/libswiftCompatibilitySpan.dylib$$1$10.14$15.0$_$ss7Ra
11271127
Added: $ld$previous$@rpath/libswiftCompatibilitySpan.dylib$$1$10.14$15.0$_$ss7RawSpanV9byteCountSivpMV$
11281128
Added: $ld$previous$@rpath/libswiftCompatibilitySpan.dylib$$1$10.14$15.0$_$ss7RawSpanVMa$
11291129
Added: $ld$previous$@rpath/libswiftCompatibilitySpan.dylib$$1$10.14$15.0$_$ss7RawSpanVMn$
1130-
Added: $ld$previous$@rpath/libswiftCompatibilitySpan.dylib$$1$10.14$15.0$_$ss7RawSpanVN$
1130+
Added: $ld$previous$@rpath/libswiftCompatibilitySpan.dylib$$1$10.14$15.0$_$ss7RawSpanVN$
1131+
1132+
// Duration.nanoseconds(_:)
1133+
Added: _$ss8DurationV11nanosecondsyABSdFZ

test/abi/macOS/x86_64/stdlib.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,3 +1130,5 @@ Added: $ld$previous$@rpath/libswiftCompatibilitySpan.dylib$$1$10.14$15.0$_$ss7Ra
11301130
Added: $ld$previous$@rpath/libswiftCompatibilitySpan.dylib$$1$10.14$15.0$_$ss7RawSpanVMn$
11311131
Added: $ld$previous$@rpath/libswiftCompatibilitySpan.dylib$$1$10.14$15.0$_$ss7RawSpanVN$
11321132

1133+
// Duration.nanoseconds(_:)
1134+
Added: _$ss8DurationV11nanosecondsyABSdFZ

test/stdlib/Duration.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ if #available(SwiftStdlib 5.7, *) {
2828
// Divide by 1000 to get back to a duration with representable components:
2929
let smallerDuration = duration / 1000
3030
expectEqual(smallerDuration.components, (170_000_000_000_000_000, 0))
31-
#if !os(WASI)
31+
#if !os(WASI)
3232
// Now check that the components of the original value trap:
3333
expectCrashLater()
3434
let _ = duration.components
35-
#endif
35+
#endif
3636
}
3737

3838
suite.test("milliseconds from Double") {
@@ -266,3 +266,14 @@ if #available(SwiftStdlib 6.0, *) {
266266
expectEqual(min.attoseconds, .min)
267267
}
268268
}
269+
270+
if #available(SwiftStdlib 6.2, *) {
271+
suite.test("nanoseconds from Double") {
272+
for _ in 0 ..< 100 {
273+
let integerValue = Double(Int64.random(in: 0 ... 0x7fff_ffff_ffff_fc00))
274+
let (sec, attosec) = Duration.nanoseconds(integerValue).components
275+
expectEqual(sec, Int64(integerValue) / 1_000_000_000)
276+
expectEqual(attosec, Int64(integerValue) % 1_000_000_000 * 1_000_000_000)
277+
}
278+
}
279+
}

0 commit comments

Comments
 (0)