Skip to content

Commit f9af27f

Browse files
committed
Merge pull request #16 from ochococo/feature/time-helpers
Improved time helpers.
2 parents 047fa1c + 4dfa99a commit f9af27f

File tree

5 files changed

+65
-9
lines changed

5 files changed

+65
-9
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ SwiftyTimer uses closures instead of target/selector/userInfo.
2323
You can specify time intervals with intuitive [Ruby on Rails](http://rubyonrails.org)-like helpers:
2424

2525
```swift
26+
100.milliseconds
2627
1.second
2728
2.5.seconds
2829
5.seconds
2930
10.minutes
3031
1.hour
32+
2.days
3133
```
3234

3335
You can pass method references instead of closures:

Src/SwiftyTime.swift

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 milliDivider: 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 millisecond: NSTimeInterval { return assertOneOrLess(milliseconds) }
40+
public var milliseconds: NSTimeInterval { return self / milliDivider }
41+
public var ms: NSTimeInterval { return milliseconds }
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+
}

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+
1A4561A11C19D12600313D20 /* SwiftyTime.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A4561A01C19D12600313D20 /* 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+
1A4561A01C19D12600313D20 /* SwiftyTime.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SwiftyTime.swift; path = ../../Src/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>"; };
@@ -48,6 +50,7 @@
4850
6EE9C1551B00BB5B00D6B91C /* SwiftyTimerTests */ = {
4951
isa = PBXGroup;
5052
children = (
53+
1A4561A01C19D12600313D20 /* SwiftyTime.swift */,
5154
6EE9C1731B00BBB700D6B91C /* SwiftyTimer.swift */,
5255
6EE9C1581B00BB5B00D6B91C /* main.swift */,
5356
6EE9C1561B00BB5B00D6B91C /* Supporting Files */,
@@ -130,6 +133,7 @@
130133
isa = PBXSourcesBuildPhase;
131134
buildActionMask = 2147483647;
132135
files = (
136+
1A4561A11C19D12600313D20 /* SwiftyTime.swift in Sources */,
133137
6EE9C1591B00BB5B00D6B91C /* main.swift in Sources */,
134138
6EE9C1741B00BBB700D6B91C /* SwiftyTimer.swift in Sources */,
135139
);

Tests/SwiftyTimerTests/main.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
1414
assert(1.2.seconds == 1.2)
1515
assert(1.5.minutes == 90.0)
1616
assert(1.5.hours == 5400.0)
17+
assert(1.3.milliseconds == 0.0013)
18+
assert(0.5.day == 43_200 )
19+
assert(1.day == 86_400 )
20+
assert(2.days == 172_800)
1721
test2()
1822
}
1923

0 commit comments

Comments
 (0)