Calculate seconds correctly?

Simple question, I’m attempting to calculate seconds, but it converts to minutes instead for whatever reason, even if the number is 2.

local Years = Floor(Time / TimeManager.Settings.years.Time)
local Days = Floor(Fmod(Time, TimeManager.Settings.days.Time) / TimeManager.Settings.hours.Time)
local Hours = Floor(Fmod(Time, TimeManager.Settings.hours.Time) / TimeManager.Settings.minutes.Time)
local Minutes = Floor(Fmod(Time, TimeManager.Settings.minutes.Time) / TimeManager.Settings.seconds.Time)
local Seconds = Floor(Fmod(Time, TimeManager.Settings.seconds.Time))

Floor is math.floor and Fmod is math.fmod

TimeManager.Settings = {
	years = {
		Aliases 		= {'year', 'yrs', 'yr', 'y'},
		Time 			= 31536000
	},
	
	days 	= {
		Aliases 		= {'day', 'd'},
		Time 			= 86400
	},
	
	hours 	= {
		Aliases			= {'hour', 'hrs', 'hr'},
		Time			= 3600
	},
	
	minutes = {
		Aliases 		= {'minute', 'mins', 'min', 'm'},
		Time 			= 60
	},
	
	seconds = {
		Aliases 		= {'second', 'secs', 'sec', 's'},
		Time 			= 1
	}
}
2 Likes

If you want to get the amount of seconds that have passed of the current minute, I think you should use the amount of seconds in a minute in your fmod, like this:

local Seconds = Floor(Fmod(Time, TimeManager.Settings.**minutes**.Time))

But I am not entirely sure what you mean by “it converts to minutes instead for whatever reason, even if the number is 2”…

1 Like

No… It’s still registering it as minutes.
It should display in seconds if it’s under 60 seconds, but it registers 1 second as 1 minute, and so on.

Example: 90 seconds would display: 1 min, 30 secs

1 Like

The way I would do this is like this:

local Time = --whatever it is (in seconds)
local RunningTime = Time
local Years = math.floor(time/TimeManager.Settings.years.Time)
RunningTime = RunningTime % TimeManager.Settings.years

local Days = math.floor(time/TimeManager.Settings.days.Time)
RunningTime = RunningTime % TimeManager.Settings.years

local Hours = math.floor(time/TimeManager.Settings.hours.Time)
RunningTime = RunningTime % TimeManager.Settings.years

local Minutes = math.floor(time/TimeManager.Settings.minutes.Time)
RunningTime = RunningTime % TimeManager.Settings.years

local Seconds =  RunningTime
1 Like

Without an explanation, I wouldn’t even understand your way of doing it, how it should be calculated:

OsTime + (Floor(Length) * Multiplier) - OsTime

I’d like to hear if you want to explain your code though.

Anyways, the issue has been fixed, after some investigation, I realized I multiplied seconds by 60 instead of 1. In other words, every time table now has a Multiplier value and a Divider value.

for Index, Value in pairs(TimeManager.Settings) do
		local TypeLower = Type:lower()
		if (TypeLower == Index or table.find(Value.Aliases, TypeLower)) then
			Time = OsTime + (Floor(Length) * Value.Multiplier) - OsTime
			Passed = true
			break
		end
	end

May be a bit complex to see in person, but very efficient when it comes to execution.

1 Like

My code basically had a running total (RunningTime) that I could freely modify.
Then, I rounded down the number of years, based on what you had in your settings thingy.

After that, I used a modulus operator (%) on the running total. The modulus operator basically returns the remainder when RunningTotal is divided by the seconds in a year.

Then i repeated it for the other intervals

That works too, the only real advantage of fmod is that it has the ability to have floating points, which isn’t really needed here anyways.

And, math.floor isn’t needed either, because the time will never have any floating points, since both values will always be seconds.

Thanks for your explanation!

1 Like