Skip to content

Commit

Permalink
Merge pull request #51 from yoheimuta/fix-duration-notation
Browse files Browse the repository at this point in the history
Fix duration notation
  • Loading branch information
yoheimuta authored Dec 28, 2021
2 parents 9f6848c + 0c1fa75 commit 81f66d3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
12 changes: 9 additions & 3 deletions RxMusicPlayer/CMTime+String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ import AVFoundation

extension CMTime {
public var displayTime: String? {
guard let sec = seconds?.rounded().toInt() else { return nil }
guard let sec = seconds?.rounded() else { return nil }

let formatter = DateComponentsFormatter()
formatter.unitsStyle = .positional
formatter.zeroFormattingBehavior = .pad
if sec < 60 * 60 {
return String(format: "%02d:%02d", sec / 60, sec % 60)
formatter.allowedUnits = [.minute, .second]
} else {
formatter.allowedUnits = [.hour, .minute, .second]
}
return String(format: "%02d:%02d:%02d", sec / (60 * 60), sec % 60 * 60, sec % 60)
return formatter.string(from: sec) ?? nil
}

public var seconds: Double? {
Expand Down
1 change: 1 addition & 0 deletions RxMusicPlayerTests/RxMusicPlayerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class RxMusicPlayerTests: XCTestCase {
XCTAssertEqual(CMTimeMake(value: 60, timescale: 1).displayTime, "01:00", "convert a minute")
XCTAssertEqual(CMTimeMake(value: 601, timescale: 1).displayTime, "10:01", "convert ten minutes")
XCTAssertEqual(CMTimeMake(value: 3600, timescale: 1).displayTime, "01:00:00", "convert an hour")
XCTAssertEqual(CMTimeMake(value: 5430, timescale: 1).displayTime, "01:30:30", "convert an hour")
XCTAssertEqual(CMTimeMake(value: 86400, timescale: 1).displayTime, "24:00:00", "convert a day")
}

Expand Down

0 comments on commit 81f66d3

Please sign in to comment.