diff --git a/README.md b/README.md index 016f8a1..13c4e13 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/Src/SwiftyTime.swift b/Src/SwiftyTime.swift new file mode 100644 index 0000000..303f764 --- /dev/null +++ b/Src/SwiftyTime.swift @@ -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 } +} diff --git a/Src/SwiftyTimer.swift b/Src/SwiftyTimer.swift index 47f0081..dc79eff 100644 --- a/Src/SwiftyTimer.swift +++ b/Src/SwiftyTimer.swift @@ -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 } -} + diff --git a/Tests/SwiftyTimerTests.xcodeproj/project.pbxproj b/Tests/SwiftyTimerTests.xcodeproj/project.pbxproj index 1c3f8ff..5ba7122 100644 --- a/Tests/SwiftyTimerTests.xcodeproj/project.pbxproj +++ b/Tests/SwiftyTimerTests.xcodeproj/project.pbxproj @@ -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 = ""; }; 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 = ""; }; 6EE9C1581B00BB5B00D6B91C /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; @@ -48,6 +50,7 @@ 6EE9C1551B00BB5B00D6B91C /* SwiftyTimerTests */ = { isa = PBXGroup; children = ( + 1A4561A01C19D12600313D20 /* SwiftyTime.swift */, 6EE9C1731B00BBB700D6B91C /* SwiftyTimer.swift */, 6EE9C1581B00BB5B00D6B91C /* main.swift */, 6EE9C1561B00BB5B00D6B91C /* Supporting Files */, @@ -130,6 +133,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 1A4561A11C19D12600313D20 /* SwiftyTime.swift in Sources */, 6EE9C1591B00BB5B00D6B91C /* main.swift in Sources */, 6EE9C1741B00BBB700D6B91C /* SwiftyTimer.swift in Sources */, ); diff --git a/Tests/SwiftyTimerTests/main.swift b/Tests/SwiftyTimerTests/main.swift index 7c835fb..e33639e 100644 --- a/Tests/SwiftyTimerTests/main.swift +++ b/Tests/SwiftyTimerTests/main.swift @@ -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() }