Skip to content
This repository was archived by the owner on May 1, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ SwiftyTimer uses closures instead of target/selector/userInfo.
You can specify time intervals with intuitive [Ruby on Rails](http://rubyonrails.org)-like helpers:

```swift
100.milliseconds
1.second
2.5.seconds
5.seconds
10.minutes
1.hour
2.days
```

You can pass method references instead of closures:
Expand Down
54 changes: 54 additions & 0 deletions Src/SwiftyTime.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// SwiftyTimer
//
// Copyright (c) 2015 Oktawian Chojnacki and Radosław Pietruszewski
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//

import Foundation

private let milliDivider: Double = 1000
private let secondsInMinute: Double = 60
private let minutesInHour: Double = 60
private let hoursInDay: Double = 24

extension Double {

private func assertOneOrLess(value: Double) -> Double {
assert(self <= 1, "🤓 Use plural property for numbers above 1.")
return value
}

public var millisecond: NSTimeInterval { return assertOneOrLess(milliseconds) }
public var milliseconds: NSTimeInterval { return self / milliDivider }
public var ms: NSTimeInterval { return milliseconds }

public var second: NSTimeInterval { return assertOneOrLess(seconds) }
public var seconds: NSTimeInterval { return self }

public var minute: NSTimeInterval { return assertOneOrLess(minutes) }
public var minutes: NSTimeInterval { return self * secondsInMinute }

public var hour: NSTimeInterval { return assertOneOrLess(hours)}
public var hours: NSTimeInterval { return minutes * minutesInHour }

public var day: NSTimeInterval { return assertOneOrLess(days) }
public var days: NSTimeInterval { return hours * hoursInDay }
}
10 changes: 1 addition & 9 deletions Src/SwiftyTimer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,4 @@ extension NSTimer {
}
}

extension Double {
public var ms: NSTimeInterval { return self / 1000 }
public var second: NSTimeInterval { return self }
public var seconds: NSTimeInterval { return self }
public var minute: NSTimeInterval { return self * 60 }
public var minutes: NSTimeInterval { return self * 60 }
public var hour: NSTimeInterval { return self * 3600 }
public var hours: NSTimeInterval { return self * 3600 }
}

4 changes: 4 additions & 0 deletions Tests/SwiftyTimerTests.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
objects = {

/* Begin PBXBuildFile section */
1A4561A11C19D12600313D20 /* SwiftyTime.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A4561A01C19D12600313D20 /* SwiftyTime.swift */; };
6EE9C1591B00BB5B00D6B91C /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EE9C1581B00BB5B00D6B91C /* main.swift */; };
6EE9C1741B00BBB700D6B91C /* SwiftyTimer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EE9C1731B00BBB700D6B91C /* SwiftyTimer.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
1A4561A01C19D12600313D20 /* SwiftyTime.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SwiftyTime.swift; path = ../../Src/SwiftyTime.swift; sourceTree = "<group>"; };
6EE9C1531B00BB5B00D6B91C /* SwiftyTimerTests.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwiftyTimerTests.app; sourceTree = BUILT_PRODUCTS_DIR; };
6EE9C1571B00BB5B00D6B91C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
6EE9C1581B00BB5B00D6B91C /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -48,6 +50,7 @@
6EE9C1551B00BB5B00D6B91C /* SwiftyTimerTests */ = {
isa = PBXGroup;
children = (
1A4561A01C19D12600313D20 /* SwiftyTime.swift */,
6EE9C1731B00BBB700D6B91C /* SwiftyTimer.swift */,
6EE9C1581B00BB5B00D6B91C /* main.swift */,
6EE9C1561B00BB5B00D6B91C /* Supporting Files */,
Expand Down Expand Up @@ -130,6 +133,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
1A4561A11C19D12600313D20 /* SwiftyTime.swift in Sources */,
6EE9C1591B00BB5B00D6B91C /* main.swift in Sources */,
6EE9C1741B00BBB700D6B91C /* SwiftyTimer.swift in Sources */,
);
Expand Down
4 changes: 4 additions & 0 deletions Tests/SwiftyTimerTests/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
assert(1.2.seconds == 1.2)
assert(1.5.minutes == 90.0)
assert(1.5.hours == 5400.0)
assert(1.3.milliseconds == 0.0013)
assert(0.5.day == 43_200 )
assert(1.day == 86_400 )
assert(2.days == 172_800)
test2()
}

Expand Down