Skip to content

Commit

Permalink
CMTime: Added toTimecodeInterval(at:) method
Browse files Browse the repository at this point in the history
  • Loading branch information
orchetect committed Jan 3, 2023
1 parent 0533640 commit 76f2b13
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,27 @@ extension TimecodeInterval {
}
}

@available(macOS 10.7, iOS 4.0, tvOS 9.0, watchOS 6.0, *)
extension CMTime {
/// Convenience function to initialize a `TimecodeInterval` instance from a time duration
/// represented as a rational fraction.
/// A negative fraction will produce a negative time interval.
///
/// - Throws: ``ValidationError``
public func toTimecodeInterval(
at rate: TimecodeFrameRate,
limit: Timecode.UpperLimit = ._24hours,
base: Timecode.SubFramesBase = .default(),
format: Timecode.StringFormat = .default()
) throws -> TimecodeInterval {
try TimecodeInterval(
self,
at: rate,
limit: limit,
base: base,
format: format
)
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ class TimecodeInterval_Rational_CMTime_Tests: XCTestCase {
XCTAssertEqual(cmTime.value, 2)
XCTAssertEqual(cmTime.timescale, 1)
}

func testCMTime_toTimecodeInterval() throws {
let ti = try CMTime(value: 60, timescale: 30)
.toTimecodeInterval(at: ._24)

XCTAssertEqual(ti.sign, .plus)

XCTAssertEqual(
ti.absoluteInterval,
try TCC(s: 2).toTimecode(at: ._24)
)
}
}

#endif

0 comments on commit 76f2b13

Please sign in to comment.