Skip to content

Commit

Permalink
No ticket: [refactor] Combined Cloisimis's TimeInterval.durationStrin…
Browse files Browse the repository at this point in the history
…g extension with the TimeIntervalExtensions.
  • Loading branch information
jrtibbetts committed Jul 31, 2021
1 parent a59b2e9 commit e148ff6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Source/Util/TimeIntervalExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,16 @@ public extension TimeInterval {
var weeks: TimeInterval { return 7.0 * days }
var years: TimeInterval { return 365.0 * days }

var formattedString: String? {
return Self.durationFormatter.string(from: self)
}

static var durationFormatter: DateComponentsFormatter = {
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.hour, .minute, .second]
formatter.collapsesLargestUnit = true

return formatter
}()

}
20 changes: 20 additions & 0 deletions Tests/Util/TimeIntervalExtensionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,24 @@ class TimeIntervalExtensionsTests: XCTestCase {
XCTAssertEqual(2.years, 104.weeks + 2.days)
}

// MARK: - durationString

func testFormattedStringDropsHours() {
let timeInterval = 165.0000 // 2:45
let durationString = timeInterval.formattedString
XCTAssertEqual(durationString, "2:45")
}

func testFormattedStringIncludesHours() {
let timeInterval = (3.0 * 60.0 * 60.0) + 165.0000 // 3:02:45
let durationString = timeInterval.formattedString
XCTAssertEqual(durationString, "3:02:45")
}

func testFormattedStringHandlesNegativeValues() {
let timeInterval = -165.0000 // -2:45
let durationString = timeInterval.formattedString
XCTAssertEqual(durationString, "-2:45")
}

}

0 comments on commit e148ff6

Please sign in to comment.