Created
May 21, 2017 04:24
-
-
Save badcc/b6d9b68692236fc44d00828c8c8fbcde to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local FormatSeconds do | |
-- Seconds: %s; Minutes: %m; Hours: %h | |
-- Days: %D; Months: %M; Years: %Y | |
local floor = math.floor | |
local q={60,60,24,365/12,12} -- Remove/add from here to change max precision from years to some other value. | |
local m={['s']=1,['m']=2,['h']=3,['D']=4,['M']=5,['Y']=6} | |
local qb={1} | |
for i = 1,#q do table.insert(qb,1,q[i]*qb[1]) end | |
function FormatSeconds(f,t) | |
local r={} | |
for i=1,#qb do | |
local m=qb[i] | |
local d=floor(t/m) | |
table.insert(r,1,d) | |
t=t-d*m | |
end | |
local m = f:gsub('%%(%a)', function(a) | |
return r[assert(m[a],'invalid time option: '..a)] | |
end) | |
return m | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment