Is there a wrong answer ?4GForce wrote:jNizM wrote:A pure mathematical function (with mod / floor /... maybe some bithacks or magic numbers if possible) would be nice too see (but without FormatTime / Env... / SubStr / RegEx...)
Code: Select all
CalcTime(sec) {
static values := [{symbol: "d", val: 86400, modulus: 365}, {symbol: "h", val: 3600, modulus: 24}, {symbol: "m", val: 60, modulus: 60}, {symbol: "s", val: 1, modulus: 60}]
for i, v in values {
result .= " " . ((sec >= v.val) ? (0 != x := Mod(sec // v.val, v.modulus)) ? x > 9 ? x . v.symbol : " " . x . v.symbol : " 0" . v.symbol : "")
}
Return result
}
Edit: Slight upgrade ( I added the extra space at last second to match your output without thinking about the processing impact ... so pushing it beyond the 1st condition should be at least a nanosecond faster and a cleaner output )
Code: Select all
result .= ((sec >= v.val) ? " " . (0 != x := Mod(sec // v.val, v.modulus)) ? x > 9 ? x . v.symbol : " " . x . v.symbol : " 0" . v.symbol : "")
Code: Select all
for i, v in TimeArr
GetTime .= CalcTime(TimeArr[i]) "`n" ; why not use 'v' in CalcTime() param ?