Created
August 6, 2014 18:07
-
-
Save maddiesch/0a4e85331347c92a839a to your computer and use it in GitHub Desktop.
Simple implementation of x.hours.ago() and others.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum FloatToDate { | |
case Second(Double) | |
case Minute(Double) | |
case Hour(Double) | |
case Day(Double) | |
case Week(Double) | |
case Month(Double) | |
case Year(Double) | |
func ago() -> NSDate { | |
return NSDate().dateByAddingTimeInterval(-timeInterval()) | |
} | |
func fromNow() -> NSDate { | |
return NSDate().dateByAddingTimeInterval(timeInterval()) | |
} | |
private func timeInterval() -> NSTimeInterval { | |
switch self { | |
case .Second(let seconds): | |
return NSTimeInterval(seconds) | |
case .Minute(let minutes): | |
return NSTimeInterval(minutes * 60.0) | |
case .Hour(let hours): | |
return NSTimeInterval(hours * 3_600.0) | |
case .Day(let days): | |
return NSTimeInterval(days * 86_400.0) | |
case .Week(let weeks): | |
return NSTimeInterval(weeks * 604_800.0) | |
case .Month(let months): | |
return NSTimeInterval(months * 2.63e+6) | |
case .Year(let years): | |
return NSTimeInterval(years * 3.156e+7) | |
} | |
} | |
} | |
protocol Dateable { | |
var seconds: FloatToDate { get } | |
var minutes: FloatToDate { get } | |
var hours: FloatToDate { get } | |
var days: FloatToDate { get } | |
var weeks: FloatToDate { get } | |
var months: FloatToDate { get } | |
var years: FloatToDate { get } | |
} | |
protocol SingleDateable : Dateable { | |
var second: FloatToDate { get } | |
var minute: FloatToDate { get } | |
var hour: FloatToDate { get } | |
var day: FloatToDate { get } | |
var week: FloatToDate { get } | |
var month: FloatToDate { get } | |
var year: FloatToDate { get } | |
} | |
extension Int : SingleDateable { | |
var seconds: FloatToDate { get { return .Second(Double(self)) } } | |
var minutes: FloatToDate { get { return .Minute(Double(self)) } } | |
var hours: FloatToDate { get { return .Hour(Double(self)) } } | |
var days: FloatToDate { get { return .Day(Double(self)) } } | |
var weeks: FloatToDate { get { return .Week(Double(self)) } } | |
var months: FloatToDate { get { return .Month(Double(self)) } } | |
var years: FloatToDate { get { return .Year(Double(self)) } } | |
var second: FloatToDate { get { return seconds } } | |
var minute: FloatToDate { get { return minutes } } | |
var hour: FloatToDate { get { return hours } } | |
var day: FloatToDate { get { return days } } | |
var week: FloatToDate { get { return weeks } } | |
var month: FloatToDate { get { return months } } | |
var year: FloatToDate { get { return years } } | |
} | |
extension Double : SingleDateable { | |
var seconds: FloatToDate { get { return .Second(self) } } | |
var minutes: FloatToDate { get { return .Minute(self) } } | |
var hours: FloatToDate { get { return .Hour(self) } } | |
var days: FloatToDate { get { return .Day(self) } } | |
var weeks: FloatToDate { get { return .Week(self) } } | |
var months: FloatToDate { get { return .Month(self) } } | |
var years: FloatToDate { get { return .Year(self) } } | |
var second: FloatToDate { get { return seconds } } | |
var minute: FloatToDate { get { return minutes } } | |
var hour: FloatToDate { get { return hours } } | |
var day: FloatToDate { get { return days } } | |
var week: FloatToDate { get { return weeks } } | |
var month: FloatToDate { get { return months } } | |
var year: FloatToDate { get { return years } } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage