Skip to content

Commit

Permalink
Extract NSTimeInterval formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Dec 18, 2015
1 parent 3159227 commit be50440
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 10 deletions.
10 changes: 10 additions & 0 deletions Aware.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
036EBD191C1408C200121D0B /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 036EBD181C1408C200121D0B /* AppDelegate.swift */; };
036EBD1B1C1408C200121D0B /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 036EBD1A1C1408C200121D0B /* Assets.xcassets */; };
036EBD1E1C1408C200121D0B /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 036EBD1C1C1408C200121D0B /* MainMenu.xib */; };
03F9E2311C24CCA8001DBE86 /* NSTimeIntervalFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03F9E2301C24CCA8001DBE86 /* NSTimeIntervalFormatter.swift */; };
03F9E2331C24CD2E001DBE86 /* NSTimeIntervalFormatterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03F9E2321C24CD2E001DBE86 /* NSTimeIntervalFormatterTests.swift */; };
03F9E2341C24CDAB001DBE86 /* NSTimeIntervalFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03F9E2301C24CCA8001DBE86 /* NSTimeIntervalFormatter.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -32,6 +35,8 @@
036EBD1F1C1408C200121D0B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
03F9E2261C24CAD3001DBE86 /* AwareTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = AwareTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
03F9E22A1C24CAD3001DBE86 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
03F9E2301C24CCA8001DBE86 /* NSTimeIntervalFormatter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSTimeIntervalFormatter.swift; sourceTree = "<group>"; };
03F9E2321C24CD2E001DBE86 /* NSTimeIntervalFormatterTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSTimeIntervalFormatterTests.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -74,6 +79,7 @@
isa = PBXGroup;
children = (
036EBD181C1408C200121D0B /* AppDelegate.swift */,
03F9E2301C24CCA8001DBE86 /* NSTimeIntervalFormatter.swift */,
0337E7861C14E37B003A8150 /* NSTimer+Block.swift */,
036EBD1A1C1408C200121D0B /* Assets.xcassets */,
036EBD1C1C1408C200121D0B /* MainMenu.xib */,
Expand All @@ -85,6 +91,7 @@
03F9E2271C24CAD3001DBE86 /* AwareTests */ = {
isa = PBXGroup;
children = (
03F9E2321C24CD2E001DBE86 /* NSTimeIntervalFormatterTests.swift */,
03F9E22A1C24CAD3001DBE86 /* Info.plist */,
);
path = AwareTests;
Expand Down Expand Up @@ -190,6 +197,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
03F9E2311C24CCA8001DBE86 /* NSTimeIntervalFormatter.swift in Sources */,
036EBD191C1408C200121D0B /* AppDelegate.swift in Sources */,
0337E7871C14E37B003A8150 /* NSTimer+Block.swift in Sources */,
);
Expand All @@ -199,6 +207,8 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
03F9E2331C24CD2E001DBE86 /* NSTimeIntervalFormatterTests.swift in Sources */,
03F9E2341C24CDAB001DBE86 /* NSTimeIntervalFormatter.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
11 changes: 1 addition & 10 deletions Aware/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,6 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

let duration = NSDate().timeIntervalSinceDate(timerStart)
statusItem.button!.title = formatDuration(duration)
}

func formatDuration(duration: NSTimeInterval) -> String {
let minutes = NSInteger(duration) / 60
if minutes < 60 {
return "\(minutes)m"
} else {
return "\(minutes / 60)h \(minutes % 60)m"
}
statusItem.button!.title = NSTimeIntervalFormatter().stringFromTimeInterval(duration)
}
}
20 changes: 20 additions & 0 deletions Aware/NSTimeIntervalFormatter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Foundation

class NSTimeIntervalFormatter {
/**
Formats time interval as a human readable duration string.

- Parameters:
- interval: The time interval in seconds.

- Returns: A `String`.
*/
func stringFromTimeInterval(interval: NSTimeInterval) -> String {
let minutes = NSInteger(interval) / 60
if minutes < 60 {
return "\(minutes)m"
} else {
return "\(minutes / 60)h \(minutes % 60)m"
}
}
}
33 changes: 33 additions & 0 deletions AwareTests/NSTimeIntervalFormatterTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import XCTest

class NSTimeIntervalFormatterTests: XCTestCase {
func testStringFromTimeInterval() {
let formatter = NSTimeIntervalFormatter()

XCTAssertEqual(formatter.stringFromTimeInterval(0), "0m")
XCTAssertEqual(formatter.stringFromTimeInterval(1), "0m")
XCTAssertEqual(formatter.stringFromTimeInterval(30), "0m")
XCTAssertEqual(formatter.stringFromTimeInterval(59), "0m")

XCTAssertEqual(formatter.stringFromTimeInterval(60), "1m")
XCTAssertEqual(formatter.stringFromTimeInterval(61), "1m")
XCTAssertEqual(formatter.stringFromTimeInterval(119), "1m")

XCTAssertEqual(formatter.stringFromTimeInterval(120), "2m")
XCTAssertEqual(formatter.stringFromTimeInterval(300), "5m")
XCTAssertEqual(formatter.stringFromTimeInterval(900), "15m")
XCTAssertEqual(formatter.stringFromTimeInterval(1800), "30m")
XCTAssertEqual(formatter.stringFromTimeInterval(2700), "45m")
XCTAssertEqual(formatter.stringFromTimeInterval(3540), "59m")
XCTAssertEqual(formatter.stringFromTimeInterval(3599), "59m")

XCTAssertEqual(formatter.stringFromTimeInterval(3600), "1h 0m")
XCTAssertEqual(formatter.stringFromTimeInterval(3601), "1h 0m")
XCTAssertEqual(formatter.stringFromTimeInterval(3660), "1h 1m")
XCTAssertEqual(formatter.stringFromTimeInterval(4500), "1h 15m")
XCTAssertEqual(formatter.stringFromTimeInterval(5400), "1h 30m")
XCTAssertEqual(formatter.stringFromTimeInterval(6300), "1h 45m")

XCTAssertEqual(formatter.stringFromTimeInterval(7200), "2h 0m")
}
}

0 comments on commit be50440

Please sign in to comment.