Last active
May 30, 2016 18:31
-
-
Save romainmenke/7a9a2067892b1d8493fcccc84a3e8815 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
extension NSDate { | |
func yearsFrom(date:NSDate) -> Int{ | |
return NSCalendar.currentCalendar().components(.Year, fromDate: self, toDate: date, options: []).year | |
} | |
func monthsFrom(date:NSDate) -> Int{ | |
return NSCalendar.currentCalendar().components(.Month, fromDate: self, toDate: date, options: []).month | |
} | |
func weeksFrom(date:NSDate) -> Int{ | |
return NSCalendar.currentCalendar().components(.WeekOfYear, fromDate: self, toDate: date, options: []).weekOfYear | |
} | |
func daysFrom(date:NSDate) -> Int{ | |
return NSCalendar.currentCalendar().components(.Day, fromDate: self, toDate: date, options: []).day | |
} | |
func hoursFrom(date:NSDate) -> Int{ | |
return NSCalendar.currentCalendar().components(.Hour, fromDate: self, toDate: date, options: []).hour | |
} | |
func minutesFrom(date:NSDate) -> Int{ | |
return NSCalendar.currentCalendar().components(.Minute, fromDate: self, toDate: date, options: []).minute | |
} | |
func secondsFrom(date:NSDate) -> Int{ | |
return NSCalendar.currentCalendar().components(.Second, fromDate: self, toDate: date, options: []).second | |
} | |
func offset(fromDate date:NSDate = NSDate()) -> (offset:Int,period:TimePeriod) { | |
let years = yearsFrom(date) | |
if years > 1 { return (years, .Years) } | |
if years > 0 { return (years, .Year) } | |
let month = monthsFrom(date) | |
if month > 1 { return (month, .Months) } | |
if month > 0 { return (month, .Month) } | |
let week = weeksFrom(date) | |
if week > 1 { return (week, .Weeks) } | |
if week > 0 { return (week, .Week) } | |
let day = daysFrom(date) | |
if day > 1 { return (day, .Days) } | |
if day > 0 { return (day, .Day) } | |
let hour = hoursFrom(date) | |
if hour > 1 { return (hour, .Hours) } | |
if hour > 0 { return (hour, .Hour) } | |
let minute = minutesFrom(date) | |
if minute > 1 { return (minute, .Minutes) } | |
if minute > 0 { return (minute, .Minute) } | |
let second = secondsFrom(date) | |
if second > 1 { return (second, .Seconds) } | |
if second > 0 { return (second, .Seconds) } | |
return (0,.Now) | |
} | |
func formatTimeOffset(fromDate date:NSDate = NSDate(), withFormatting formatting:((offset:Int,period:TimePeriod) -> String)) -> String { | |
let currentOffset = offset(fromDate: date) | |
return formatting(offset: currentOffset.offset, period: currentOffset.period) | |
} | |
} | |
enum TimePeriod { | |
case Now | |
case Second | |
case Seconds | |
case Minute | |
case Minutes | |
case Hour | |
case Hours | |
case Day | |
case Days | |
case Week | |
case Weeks | |
case Month | |
case Months | |
case Year | |
case Years | |
} | |
date.formatTimeOffset { (offset, period) -> String in | |
switch period { | |
case .Now : return "Now" | |
case .Second : return "\(offset)" + "s" | |
case .Seconds : return "\(offset)" + "s" | |
case .Minute : return "\(offset)" + "m" | |
case .Minutes : return "\(offset)" + "m" | |
case .Hour : return "\(offset)" + "h" | |
case .Hours : return "\(offset)" + "h" | |
case .Day : return "\(offset)" + "d" | |
case .Days : return "\(offset)" + "d" | |
case .Week : return "\(offset)" + "w" | |
case .Weeks : return "\(offset)" + "w" | |
case .Month : return "\(offset)" + "m" | |
case .Months : return "\(offset)" + "m" | |
case .Year : return "\(offset)" + "y" | |
case .Years : return "\(offset)" + "y" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment