Trying to format a ban time using os.time()

I’m having trouble formating a string. I thought that this would be a rather easy task, but it’s a lot harder than I thought. I’m trying to format the string so os.time() + bantime will be shown like 2months 23days 22hours 53minutes 33seconds.

Here’s my code but I wrote this like 2 years ago and I can’t quite figure out why I did all the math in there. :lol:

[lua]
function ASA.FormatBanTime( sec )
local i = math.floor( sec )
Msg( "i = "…i )
local month = math.floor( ( i / 2592000 ) - ( math.floor( i / 31536000 ) * 31536000 ) )
local day = math.floor( ( i / 86400 ) - ( math.floor( i / 2592000 ) * 2592000 ) )
local hour = math.floor( ( i / 3600 ) - ( math.floor( i / 86400 ) * 86400 ) )
local min = math.floor( ( i / 60 ) - ( math.floor( i / 3600 ) * 3600 ) )
local sec = math.floor( sec - ( math.floor( i / 60 ) * 60 ) )
return month…"m "…day…"d "…hour…"h "…min…"m "…sec…“s”
end

function ASA.BanPlayer( targ, admin, time, reason )
local reason = reason or “No reason”
local bantime = os.time() + ( time * 60 )
local txt = "Banned for "…ASA.FormatBanTime( bantime )

sql.Query( "INSERT INTO asa_ban_test ( uid, name, time, adminbanned, timebanned, reason, adminuid ) VALUES ( "..targ:UniqueID()..", "..SQLStr(targ:Nick())..", "..bantime..", "..SQLStr(admin:Nick())..", "..os.time()..", "..SQLStr(reason)..", "..admin:UniqueID().." )" )
game.ConsoleCommand( "kickid "..targ:UserID().." \""..txt.."\"" )

end
[/lua]

[lua]local SECONDS_IN_A_MONTH = 2629743.83;
local SECONDS_IN_A_DAY = 86400;
local SECONDS_IN_A_HOUR = 3600;
local SECONDS_IN_A_MINUTE = 60;

local function FormatSeconds( s )

local mod;
local months = math.floor( s / SECONDS_IN_A_MONTH );
mod = s % SECONDS_IN_A_MONTH;
local days = math.floor( mod / SECONDS_IN_A_DAY );
mod = mod % SECONDS_IN_A_DAY;
local hours = math.floor( mod / SECONDS_IN_A_HOUR );
mod = mod % SECONDS_IN_A_HOUR;
local minutes = math.floor( mod / SECONDS_IN_A_MINUTE );
mod = mod % SECONDS_IN_A_MINUTE;
local seconds = math.floor( mod );

return months.."months " .. days .. "days " .. hours .. "hours " .. minutes .. "minutes " .. seconds .. "seconds";

end[/lua]

Hmm, interesting. I don’t quite understand how you did that, but that’s alright! THANKS A TON!!

No problem.

Modulus is great for these kinds of things, you should read up on the % to figure out what he did and why it worked.

What is modulus? I think you’re talking about the modules folder?

:doh:

I’m a freshman in college taking calculus and I’ve never heard of this. :lol:

It gets the remainder from integer division.

Why are you only just doing calculus when you are a freshman? I’ve done some aspects of it and I’m years off Uni / College.

Well some people aren’t as smart as you :saddowns:

I did GCSE and A-Level mathematics, and am now doing a maths module in my university degree, I have never come across modulus in any of them, but that isn’t an excuse not to know what it is when you are a programmer.

Modulus is a simple concept anyway. The wiki overcomplicates it.

Indeed, modulus is just the same as doing this:
[lua]function modulus(a, b)
return b * ((a / b) - math.floor(a / b))
end[/lua]
which can be rewritten as:
[lua]function modulus(a, b)
return a - b * math.floor(a / b)
end[/lua]

It’s even more simply described as the remainder after division, go back to pre decimal point days, remember your childhood mathematics classes, how dvision was an arcane art which required knowledge of the future. There is where the remainder is from, it lives in your childhood and will be with you for the rest of your life.

I only found out about modulus because of programming languages. Otherwise I wouldn’t have known it yet.

I saw it used in discrete mathematics (A “how to do proofs formally” kind of class), but I knew about it for a long time from programming.