Skip to content

Commit

Permalink
Fix a bug in the implementation of TimeInterval.duration() with attos…
Browse files Browse the repository at this point in the history
…econds
  • Loading branch information
Jeehut committed Mar 26, 2024
1 parent 970dad7 commit 6eeec48
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Sources/HandySwift/Extensions/TimeIntervalExt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ extension TimeInterval {
@available(iOS 16, macOS 13, tvOS 16, visionOS 1, watchOS 9, *)
public func duration() -> Duration {
let fullSeconds = Int64(self.seconds)
let remainingInterval = self - .seconds(Double(fullSeconds))
let remainingInterval = self - Double(fullSeconds)

let attosecondsPerNanosecond = Double(1_000 * 1_000 * 1_000)
let fullAttoseconds = Int64(remainingInterval.nanoseconds / attosecondsPerNanosecond)
let fullAttoseconds = Int64(remainingInterval.nanoseconds * attosecondsPerNanosecond)

return Duration(secondsComponent: fullSeconds, attosecondsComponent: fullAttoseconds)
}
Expand Down
6 changes: 6 additions & 0 deletions Tests/HandySwiftTests/Extensions/TimeIntervalExtTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ class TimeIntervalExtTests: XCTestCase {
XCTAssertEqual(multipledTimeInterval.microseconds, 12 * 60 * 60 * 1_000_000, accuracy: 0.001)
XCTAssertEqual(multipledTimeInterval.nanoseconds, 12 * 60 * 60 * 1_000_000_000, accuracy: 0.001)
}

func testDurationConversion() {
XCTAssertEqual(TimeInterval.milliseconds(0.999).duration().timeInterval.milliseconds, 0.999, accuracy: 0.000001)
XCTAssertEqual(TimeInterval.seconds(2.5).duration().timeInterval.seconds, 2.5, accuracy: 0.001)
XCTAssertEqual(TimeInterval.days(5).duration().timeInterval.days, 5, accuracy: 0.001)
}
}

0 comments on commit 6eeec48

Please sign in to comment.