Skip to content
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
2 changes: 0 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ var targets: [PackageDescription.Target] = [
dependencies: ["NIOPosix", "NIOCore", "NIOHTTP1"]),
.testTarget(name: "NIOCoreTests",
dependencies: ["NIOCore", "NIOEmbedded", "NIOFoundationCompat"]),
.testTarget(name: "NIOBetaTests",
dependencies: ["_NIOBeta"]),
.testTarget(name: "NIOEmbeddedTests",
dependencies: ["NIOConcurrencyHelpers", "NIOCore", "NIOEmbedded"]),
.testTarget(name: "NIOPosixTests",
Expand Down
2 changes: 0 additions & 2 deletions [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ var targets: [PackageDescription.Target] = [
dependencies: ["NIOPosix", "NIOCore", "NIOHTTP1"]),
.testTarget(name: "NIOCoreTests",
dependencies: ["NIOCore", "NIOEmbedded", "NIOFoundationCompat"]),
.testTarget(name: "NIOBetaTests",
dependencies: ["_NIOBeta"]),
.testTarget(name: "NIOEmbeddedTests",
dependencies: ["NIOConcurrencyHelpers", "NIOCore", "NIOEmbedded"]),
.testTarget(name: "NIOPosixTests",
Expand Down
56 changes: 56 additions & 0 deletions Sources/NIOCore/TimeAmount+Duration.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2022 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

#if (os(macOS) && swift(>=5.7.1)) || (!os(macOS) && swift(>=5.7))
extension TimeAmount {
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
/// Creates a new `TimeAmount` for the given `Duration`, truncating and clamping if necessary.
///
/// - returns: `TimeAmount`, truncated to nanosecond precision, and clamped to `Int64.max` nanoseconds.
public init(_ duration: Swift.Duration) {
self = .nanoseconds(duration.nanosecondsClamped)
}
}

@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
extension Swift.Duration {
/// Construct a `Duration` given a number of nanoseconds represented as a `TimeAmount`.
///
/// - returns: A `Duration` representing a given number of nanoseconds.
public init(_ timeAmount: TimeAmount) {
self = .nanoseconds(timeAmount.nanoseconds)
}
}

@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
internal extension Swift.Duration {
/// The duration represented as nanoseconds, clamped to maximum expressible value.
var nanosecondsClamped: Int64 {
let components = self.components

let secondsComponentNanos = components.seconds.multipliedReportingOverflow(by: 1_000_000_000)
let attosCompononentNanos = components.attoseconds / 1_000_000_000
let combinedNanos = secondsComponentNanos.partialValue.addingReportingOverflow(attosCompononentNanos)

guard
!secondsComponentNanos.overflow,
!combinedNanos.overflow
else {
return .max
}

return combinedNanos.partialValue
}
}
#endif // (os(macOS) && swift(>=5.7.1)) || (!os(macOS) && swift(>=5.7))
3 changes: 3 additions & 0 deletions Sources/_NIOBeta/TimeAmount+Duration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import NIOCore
#if (os(macOS) && swift(>=5.7.1)) || (!os(macOS) && swift(>=5.7))
extension TimeAmount {
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
@available(*, deprecated, message: "This API has been moved to NIOCore and will be removed in a future update.")
/// Creates a new `TimeAmount` for the given `Duration`, truncating and clamping if necessary.
///
/// - returns: `TimeAmount`, truncated to nanosecond precision, and clamped to `Int64.max` nanoseconds.
Expand All @@ -30,6 +31,7 @@ extension Swift.Duration {
/// Construct a `Duration` given a number of nanoseconds represented as a `TimeAmount`.
///
/// - returns: A `Duration` representing a given number of nanoseconds.
@available(*, deprecated, message: "This API has been moved to NIOCore and will be removed in a future update.")
public init(_ timeAmount: TimeAmount) {
self = .nanoseconds(timeAmount.nanoseconds)
}
Expand All @@ -38,6 +40,7 @@ extension Swift.Duration {
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
internal extension Swift.Duration {
/// The duration represented as nanoseconds, clamped to maximum expressible value.
@available(*, deprecated, message: "This API has been moved to NIOCore and will be removed in a future update.")
var nanosecondsClamped: Int64 {
let components = self.components

Expand Down
1 change: 0 additions & 1 deletion Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import XCTest

#if !compiler(>=5.5)
#if os(Linux) || os(FreeBSD) || os(Android)
@testable import NIOBetaTests
@testable import NIOConcurrencyHelpersTests
@testable import NIOCoreTests
@testable import NIODataStructuresTests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import NIOCore
@testable import _NIOBeta
@testable import NIOCore
import XCTest

class TimeAmountDurationTests: XCTestCase {
Expand Down