Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions stdlib/public/core/Duration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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, *)
Expand Down
5 changes: 4 additions & 1 deletion test/abi/macOS/arm64/stdlib.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions test/abi/macOS/x86_64/stdlib.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 13 additions & 2 deletions test/stdlib/Duration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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)
}
}
}