Skip to content

Instantly share code, notes, and snippets.

@billglover
Last active August 29, 2015 14:21
Show Gist options
  • Save billglover/6db7c3bda303cc25cfb4 to your computer and use it in GitHub Desktop.
Save billglover/6db7c3bda303cc25cfb4 to your computer and use it in GitHub Desktop.
Add computed properties to NSTimeInterval to return component parts of a time interval
extension NSTimeInterval {
var inHours: Double { return self.inMinutes/60 } // express the time interval in hours
var inMinutes: Double { return self/60 } // express the time interval in minutes
var inSeconds: Double { return self } // express the time interval in seconds
// express the time interval in hours, minutes and seconds
var inHoursMinutesSeconds: (hours: Double, minutes: Double, seconds: Double) {
let hours: Double = floor(round(self) / 60 / 60)
let minutes: Double = trunc((round(self) - (hours * 60 * 60)) / 60)
let seconds: Double = trunc(round(self) - minutes * 60)
return (hours: hours, minutes: minutes, seconds: seconds)
}
// express the time interval in minutes and seconds
var inMinutesSeconds: (minutes: Double, seconds: Double) {
let minutes: Double = floor(round(self) / 60)
let seconds: Double = trunc(round(self) - minutes * 60)
return (minutes: minutes, seconds: seconds)
}
}
func setTimerLabel {
timerLabel.text = String(format: "%02.0f:%02.0f", remainingIntervalDuration.inMinutesSeconds.minutes, remainingIntervalDuration.inMinutesSeconds.seconds)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment