Add static .nanoseconds(_: Double) to Duration (#81210)

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.
This commit is contained in:
Stephen Canon
2025-05-01 15:50:17 -04:00
committed by GitHub
parent 11cf263347
commit ab2b28cc4b
4 changed files with 30 additions and 3 deletions

View File

@@ -250,6 +250,17 @@ extension Duration {
let highScaled = high * 1_000_000_000
return Duration(_high: highScaled + Int64(lowScaled.high), low: lowScaled.low)
}
/// Construct a `Duration` given a number of seconds nanoseconds as a
/// `Double` by converting the value into the closest attosecond scale value.
///
/// let d: Duration = .nanoseconds(382.9)
///
/// - Returns: A `Duration` representing a given number of nanoseconds.
@available(SwiftStdlib 6.2, *)
public static func nanoseconds(_ nanoseconds: Double) -> Duration {
Duration(nanoseconds, scale: 1_000_000_000)
}
}
@available(SwiftStdlib 5.7, *)

View File

@@ -1124,4 +1124,7 @@ Added: $ld$previous$@rpath/libswiftCompatibilitySpan.dylib$$1$10.14$15.0$_$ss7Ra
Added: $ld$previous$@rpath/libswiftCompatibilitySpan.dylib$$1$10.14$15.0$_$ss7RawSpanV9byteCountSivpMV$
Added: $ld$previous$@rpath/libswiftCompatibilitySpan.dylib$$1$10.14$15.0$_$ss7RawSpanVMa$
Added: $ld$previous$@rpath/libswiftCompatibilitySpan.dylib$$1$10.14$15.0$_$ss7RawSpanVMn$
Added: $ld$previous$@rpath/libswiftCompatibilitySpan.dylib$$1$10.14$15.0$_$ss7RawSpanVN$
Added: $ld$previous$@rpath/libswiftCompatibilitySpan.dylib$$1$10.14$15.0$_$ss7RawSpanVN$
// Duration.nanoseconds(_:)
Added: _$ss8DurationV11nanosecondsyABSdFZ

View File

@@ -1127,3 +1127,5 @@ Added: $ld$previous$@rpath/libswiftCompatibilitySpan.dylib$$1$10.14$15.0$_$ss7Ra
Added: $ld$previous$@rpath/libswiftCompatibilitySpan.dylib$$1$10.14$15.0$_$ss7RawSpanVMn$
Added: $ld$previous$@rpath/libswiftCompatibilitySpan.dylib$$1$10.14$15.0$_$ss7RawSpanVN$
// Duration.nanoseconds(_:)
Added: _$ss8DurationV11nanosecondsyABSdFZ

View File

@@ -28,11 +28,11 @@ if #available(SwiftStdlib 5.7, *) {
// Divide by 1000 to get back to a duration with representable components:
let smallerDuration = duration / 1000
expectEqual(smallerDuration.components, (170_000_000_000_000_000, 0))
#if !os(WASI)
#if !os(WASI)
// Now check that the components of the original value trap:
expectCrashLater()
let _ = duration.components
#endif
#endif
}
suite.test("milliseconds from Double") {
@@ -266,3 +266,14 @@ if #available(SwiftStdlib 6.0, *) {
expectEqual(min.attoseconds, .min)
}
}
if #available(SwiftStdlib 6.2, *) {
suite.test("nanoseconds from Double") {
for _ in 0 ..< 100 {
let integerValue = Double(Int64.random(in: 0 ... 0x7fff_ffff_ffff_fc00))
let (sec, attosec) = Duration.nanoseconds(integerValue).components
expectEqual(sec, Int64(integerValue) / 1_000_000_000)
expectEqual(attosec, Int64(integerValue) % 1_000_000_000 * 1_000_000_000)
}
}
}