Saturday, 15 March 2014

Swift - Integer conversion to Hours/Minutes/Seconds -



Swift - Integer conversion to Hours/Minutes/Seconds -

i have (somewhat?) basic question regarding time conversions in swift.

i have integer converted hours / minutes / seconds.

example: int = 27005 give me:

7 hours 30 minutes 5 seconds

i know how in php, alas, swift isn't php :-)

any tips on how can accomplish in swift fantastic! give thanks in advance!

define

func secondstohoursminutesseconds (seconds : int) -> (int, int, int) { homecoming (seconds / 3600, (seconds % 3600) / 60, (seconds % 3600) % 60) }

use

> secondstohoursminutesseconds(27005) (7,30,5)

or

let (h,m,s) = secondstohoursminutesseconds(27005)

the above function makes utilize of swift tuples homecoming 3 values @ once. destructure tuple using let (var, ...) syntax or can access individual tuple members, if need be.

if need print out words hours etc utilize this:

func printsecondstohoursminutesseconds (seconds:int) -> () { allow (h, m, s) = secondstohoursminutesseconds (seconds) println ("\(h) hours, \(m) minutes, \(s) seconds") }

note above implementation of secondstohoursminutesseconds() works int arguments. if want double version you'll need decide homecoming values - (int, int, double) or (double, double, double). seek like:

func secondstohoursminutesseconds (seconds : double) -> (double, double, double) { allow (hr, minf) = modf (seconds / 3600) allow (min, secf) = modf (60 * minf) homecoming (hr, min, 60 * secf) }

swift

No comments:

Post a Comment