Identify
func secondsToHoursMinutesSeconds (seconds : Int) -> (Int, Int, Int) { return (seconds / 3600, (seconds % 3600) / 60, (seconds % 3600) % 60) }
Using
> secondsToHoursMinutesSeconds(27005) (7,30,5)
or
let (h,m,s) = secondsToHoursMinutesSeconds(27005)
The function above uses Swift tuples to return three values ​​at once. You destroy the tuple using the let (var, ...) syntax, or you can access individual members of the set if necessary.
If you really need to print it with the words Hours , etc., then use something like this:
func printSecondsToHoursMinutesSeconds (seconds:Int) -> () { let (h, m, s) = secondsToHoursMinutesSeconds (seconds) print ("\(h) Hours, \(m) Minutes, \(s) Seconds") }
Note that the above secondsToHoursMinutesSeconds() implementation works for Int arguments. If you need a Double version, you will need to decide what the return values ​​are - maybe (Int, Int, Double) or maybe (Double, Double, Double) . You can try something like:
func secondsToHoursMinutesSeconds (seconds : Double) -> (Double, Double, Double) { let (hr, minf) = modf (seconds / 3600) let (min, secf) = modf (60 * minf) return (hr, min, 60 * secf) }
GoZoner Nov 07 '14 at 5:42 a.m. 2014-11-07 05:42
source share