Created
January 23, 2017 16:12
-
-
Save Wooder/2c873b3d479db638475bf79a610b98b6 to your computer and use it in GitHub Desktop.
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
extension TimeInterval { | |
func asHoursMinutesSeconds () -> (Int, Int, Int) { | |
let interval = Int(self) | |
return ( interval / 3600, interval % 3600 / 60, (interval % 3600) % 60) | |
} | |
/// Creates a string with hours, minutes, seconds, | |
/// divided by the given separator. | |
/// For example: "1:23:59" | |
/// | |
/// - Parameters: | |
/// - separator: separator between time units | |
/// - Returns: <#return value description#> | |
func asHoursMinutesSecondsString(_ separator:String) -> String { | |
let interval = Int(self) | |
let (h, m, s) = self.asHoursMinutesSeconds () | |
let strHours = String(format: "%02d", h) | |
let strMinutes = String(format: "%02d", m) | |
let strSeconds = String(format: "%02d", s) | |
if (interval < 3600) { | |
return "\(strMinutes)\(separator)\(strSeconds)" | |
} | |
else { | |
return "\(strHours)\(separator)\(strMinutes):\(strSeconds)" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment