Last active
November 21, 2015 16:18
-
-
Save tanner0101/e5d2e0bab5a58b938b08 to your computer and use it in GitHub Desktop.
Return human readable string for any NSTimeInterval
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
/** | |
NSTimeIntervalExtensions.swift | |
Return human readable string for any NSTimeInterval | |
<https://gist.github.com/tannernelson/e5d2e0bab5a58b938b08> | |
*/ | |
import Foundation | |
public extension NSTimeInterval { | |
var readable: String { | |
get { | |
var second = 1 | |
var minute = second * 60 | |
var hour = minute * 60 | |
var day = hour * 24 | |
var num: Int = Int(abs(self)) | |
var unit = "day" | |
if num >= day { | |
num /= day | |
} else if num >= hour { | |
num /= hour | |
unit = "hour" | |
} else if num >= minute { | |
num /= minute | |
unit = "minute" | |
} else if num >= second { | |
num /= second | |
unit = "second" | |
} else { | |
num = 0 | |
} | |
if num > 1 { | |
unit += "s" | |
} | |
if num == 0 { | |
return "now" | |
} else { | |
return "\(num) \(unit)" | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment