Skip to content
Open
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
33 changes: 32 additions & 1 deletion Sources/ErrorKit/Logging/ErrorKit+OSLog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@
}
}

// HandySwift defines the same three helpers. Disfavoring these lets a file that imports both modules (as FlineDevKit
// does) pick HandySwift's without an ambiguity error, while ErrorKit alone still offers `.minutes(30)` for its log APIs.
extension Duration {
/// Returns the duration as a `TimeInterval`.
///
Expand All @@ -169,8 +171,37 @@
/// ```
///
/// - Returns: The duration as a `TimeInterval`, which represents the duration in seconds.
var timeInterval: TimeInterval {
@_disfavoredOverload
public var timeInterval: TimeInterval {
TimeInterval(self.components.seconds) + (TimeInterval(self.components.attoseconds) / 1_000_000_000_000_000_000)
}

/// Constructs a `Duration` given a number of minutes represented as a `BinaryInteger`.
///
/// Example:
/// ```swift
/// try ErrorKit.logAttachment(ofLast: .minutes(30))
/// ```
///
/// - Parameter minutes: The number of minutes.
/// - Returns: A `Duration` representing the given number of minutes.
@_disfavoredOverload
public static func minutes<T: BinaryInteger>(_ minutes: T) -> Duration {
self.seconds(minutes * 60)
}

/// Constructs a `Duration` given a number of hours represented as a `BinaryInteger`.
///
/// Example:
/// ```swift
/// try ErrorKit.logAttachment(ofLast: .hours(1), minLevel: .error)
/// ```
///
/// - Parameter hours: The number of hours.
/// - Returns: A `Duration` representing the given number of hours.
@_disfavoredOverload
public static func hours<T: BinaryInteger>(_ hours: T) -> Duration {
self.seconds(hours * 3600)
}
}
#endif
19 changes: 19 additions & 0 deletions Tests/ErrorKitTests/DurationHelperTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#if canImport(OSLog)
import Foundation
import Testing

import ErrorKit

@Suite("Duration helpers")
struct DurationHelperTests {
@Test func minutesAndHoursBuildTheirDuration() {
#expect(Duration.minutes(30) == .seconds(1800))
#expect(Duration.hours(2) == .seconds(7200))
}

@Test func timeIntervalKeepsFractionalSeconds() {
#expect(Duration.milliseconds(1500).timeInterval == 1.5)
#expect(Duration.hours(1).timeInterval == 3600)
}
}
#endif
Loading