Convert seconds to compound duration: Difference between revisions
Content added Content deleted
(ââJS ES6: Tidied.) |
(new Swift version) |
||
Line 3,675: | Line 3,675: | ||
val () = app (fn s => print (compoundDuration s ^ "\n")) [7259, 86400, 6000000]</lang> |
val () = app (fn s => print (compoundDuration s ^ "\n")) [7259, 86400, 6000000]</lang> |
||
=={{header|Swift}}== |
|||
<lang swift>func duration (_ secs:Int) -> String { |
|||
if secs <= 0 { return "" } |
|||
let units = [(604800,"wk"), (86400,"d"), (3600,"hr"), (60,"min")] |
|||
var secs = secs |
|||
var result = "" |
|||
for v in units { |
|||
if secs >= v.0 { |
|||
result += "\(secs/v.0) \(v.1), " |
|||
secs = secs % v.0 |
|||
} |
|||
} |
|||
if secs == 0 { |
|||
result.removeLast(2) // remove ", " |
|||
} else { |
|||
result += "\(secs) sec" |
|||
} |
|||
return result |
|||
} |
|||
print(duration(7259)) |
|||
print(duration(86400)) |
|||
print(duration(6000000))</lang> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |