[SMF] Short My Function - Seconds to string

Get help with using AutoHotkey and its commands and hotkeys
4GForce
Posts: 553
Joined: 25 Jan 2017, 03:18
GitHub: 4GForce

Re: [SMF] Short My Function - Seconds to string

23 Mar 2017, 17:06

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...)
Is there a wrong answer ?

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
}
Benchmarks are looking good ... vs jNizM's DllCall and vs just me's modified jeeswg's function ... didn't take the time to test all the others.

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 : "")
Edit2: Wait what ...

Code: Select all

for i, v in TimeArr
    GetTime .= CalcTime(TimeArr[i]) "`n" ; why not use 'v' in CalcTime() param ?
User avatar
jeeswg
Posts: 6541
Joined: 19 Dec 2016, 01:58
Location: UK

Re: [SMF] Short My Function - Seconds to string

28 Aug 2017, 00:18

I wanted to try and understand the function here:
Format number of seconds to days, hours, minutes, seconds - Page 2 - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/1239 ... entry87852

Code: Select all

gft(s) {

	w = day.hour.minute.second

	loop parse, w, .

		s -= (t := s // (x := 60 ** (e := 4 - a_index) - 129600 * (e = 3))) * x

		, t ? m .= t . " " . a_loopfield . chr(115 * (t != 1)) . chr(32 * !!e)

	return m ? m : "0 seconds"

}
Here is what I came up with.

Code: Select all

q::
MsgBox %
( Join
 GetFormatedTime(11001001) . "`n" .
 GetFormatedTime(1001001) . "`n" .
 GetFormatedTime(101001) . "`n" .
 GetFormatedTime(11001) . "`n" .
 GetFormatedTime(3661) . "`n" .
 GetFormatedTime(1001) . "`n" .
 GetFormatedTime(100) . "`n" .
 GetFormatedTime(10) . "`n" .
 GetFormatedTime(1) . "`n" .
 GetFormatedTime(0) . "`n" .
 ""
)
return

;==================================================

;note: I added the RTrim to prevent there being
;a trailing space when D or H or M > 0, but S = 0
;note: 60 ** 3 - 129600 = 86400 [60**3 = 216000]
;note: 60 ** 2 = 3600
;note: 60 ** 1 = 60
;note: 60 ** 0 = 1
;note: Chr(115) is 's'
GetFormatedTime(s)
{
	s -= (t := s // (x := 60 ** 3 - 129600)) * x
	t ? m .= t . " day" chr(115 * (t != 1)) chr(32)

	s -= (t := s // (x := 60 ** 2)) * x
	t ? m .= t " hour" chr(115 * (t != 1)) chr(32)

	s -= (t := s // (x := 60)) * x
	t ? m .= t " minute" chr(115 * (t != 1)) chr(32)

	s -= (t := s // (x := 1)) * x
	t ? m .= t " second" chr(115 * (t != 1))

	return m ? RTrim(m) : "0 seconds"
}
Classic: 60 ** 3 - 129600 = 86400
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask For Help”

Who is online

Users browsing this forum: Google [Bot], Graesholt, Worly and 138 guests