Improved time helpers. · carloslema/SwiftyTimer@a1bcc5f · GitHub
Skip to content

Commit a1bcc5f

Browse files
committed
Improved time helpers.
1 parent 047fa1c commit a1bcc5f

5 files changed

Lines changed: 65 additions & 9 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 0 deletions

‎Src/SwiftyTimer.swift‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,4 @@ extension NSTimer {
8989
}
9090
}
9191

92-
extension Double {
93-
public var ms: NSTimeInterval { return self / 1000 }
94-
public var second: NSTimeInterval { return self }
95-
public var seconds: NSTimeInterval { return self }
96-
public var minute: NSTimeInterval { return self * 60 }
97-
public var minutes: NSTimeInterval { return self * 60 }
98-
public var hour: NSTimeInterval { return self * 3600 }
99-
public var hours: NSTimeInterval { return self * 3600 }
100-
}
92+

‎Tests/SwiftyTimerTests.xcodeproj/project.pbxproj‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
1AF1E4861BFE6E40004F5DEC /* SwiftyTime.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AF1E4851BFE6E40004F5DEC /* SwiftyTime.swift */; };
1011
6EE9C1591B00BB5B00D6B91C /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EE9C1581B00BB5B00D6B91C /* main.swift */; };
1112
6EE9C1741B00BBB700D6B91C /* SwiftyTimer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EE9C1731B00BBB700D6B91C /* SwiftyTimer.swift */; };
1213
/* End PBXBuildFile section */
1314

1415
/* Begin PBXFileReference section */
16+
1AF1E4851BFE6E40004F5DEC /* SwiftyTime.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwiftyTime.swift; sourceTree = "<group>"; };
1517
6EE9C1531B00BB5B00D6B91C /* SwiftyTimerTests.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwiftyTimerTests.app; sourceTree = BUILT_PRODUCTS_DIR; };
1618
6EE9C1571B00BB5B00D6B91C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
1719
6EE9C1581B00BB5B00D6B91C /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
@@ -49,6 +51,7 @@
4951
isa = PBXGroup;
5052
children = (
5153
6EE9C1731B00BBB700D6B91C /* SwiftyTimer.swift */,
54+
1AF1E4851BFE6E40004F5DEC /* SwiftyTime.swift */,
5255
6EE9C1581B00BB5B00D6B91C /* main.swift */,
5356
6EE9C1561B00BB5B00D6B91C /* Supporting Files */,
5457
);
@@ -130,6 +133,7 @@
130133
isa = PBXSourcesBuildPhase;
131134
buildActionMask = 2147483647;
132135
files = (
136+
1AF1E4861BFE6E40004F5DEC /* SwiftyTime.swift in Sources */,
133137
6EE9C1591B00BB5B00D6B91C /* main.swift in Sources */,
134138
6EE9C1741B00BBB700D6B91C /* SwiftyTimer.swift in Sources */,
135139
);
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
// SwiftyTimer
3+
//
4+
// Copyright (c) 2015 Oktawian Chojnacki and Radosław Pietruszewski
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in all
14+
// copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// SOFTWARE.
23+
//
24+
25+
import Foundation
26+
27+
private let miliDivider: Double = 1000
28+
private let secondsInMinute: Double = 60
29+
private let minutesInHour: Double = 60
30+
private let hoursInDay: Double = 24
31+
32+
extension Double {
33+
34+
private func assertOneOrLess(value: Double) -> Double {
35+
assert(self <= 1, "🤓 Use plural property for numbers above 1.")
36+
return value
37+
}
38+
39+
public var milisecond: NSTimeInterval { return assertOneOrLess(miliseconds) }
40+
public var miliseconds: NSTimeInterval { return self / miliDivider }
41+
public var ms: NSTimeInterval { return miliseconds }
42+
43+
public var second: NSTimeInterval { return assertOneOrLess(seconds) }
44+
public var seconds: NSTimeInterval { return self }
45+
46+
public var minute: NSTimeInterval { return assertOneOrLess(minutes) }
47+
public var minutes: NSTimeInterval { return self * secondsInMinute }
48+
49+
public var hour: NSTimeInterval { return assertOneOrLess(hours)}
50+
public var hours: NSTimeInterval { return minutes * minutesInHour }
51+
52+
public var day: NSTimeInterval { return assertOneOrLess(days) }
53+
public var days: NSTimeInterval { return hours * hoursInDay }
54+
}

‎Tests/SwiftyTimerTests/main.swift‎

Lines changed: 4 additions & 0 deletions

0 commit comments

Comments
 (0)