Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Making a formula to calculate converting real time to ingame time?

I am generating a schedule for a school that requires automatically setting the ingame time as real time continues. So far, I have reached a wall on how to find the formula. Period 1 in real time starts at 4:30pm, and the ingame time has to be 7:00am. Period 8 in real time starts at 7:53pm, and the ingame time has to be 3:00pm. By making a script convert current time (0430P) to military time (1630)

function ConStM(timee) --Convert from Standard to Military
	local addtime=false
	if timee:sub(5):lower()=="p" then
		addtime=true
	end
	timee=tonumber(timee:sub(1,4))
	if addtime==true then
		timee=timee+1200
	end
	return timee
end 
print(ConStM("0430P"))

It returns 1630. So 1630=0700 and 1900=1500. I get stuck there, how am I going to find the function to that?
1630x=700 and 1900x=1500,
x=700-1630,
x=-930,
1900(-930)=1500,
-1767000=1500
That's not true.. What am I doing?

For example. If it's 4:30pm, it will be 7:00am ingame. If it's 6:13pm, it will be 11:00am ingame. If it's 8:15pm, it will be 3:00pm ingame. If it's earlier than 4:30pm, then it will be near 7:00am. If it's later than 8:15pm, then it will be near 3:00pm. Graph Example http://postimg.org/image/4klzbhim3/

4 comments

0

Are you just asking how to take advantage of times that look like "4:30p"? Are you asking about how to translate real world time into a faster moving game time?

0

I am trying to convert the time between 4:30pm and 8:15pm turn into 7:00am and 3:00pm ingame for a realistic school day.

0

What do you plan to do with the rest of the day?

0

For example, if the military time was before 1630, then it would be a very small linear line. I'll try to make the formula to make the two lines before and after touch. Just finding the formula for the ingame time to pair with real time I'll need.

1 answer

✓ Accepted answer

First: you don't want to store military time. You want to store minutes-since-midnight.

Thus 1:00am is 60, 2:00am is 120, 12 noon is 720, 4p is 960, etc. This is easy to write:

-- Tolerant of many different time formats:
-- 1:00am
-- 1:00a
-- 100a
-- 100am
-- 13:00
-- 1:00p
-- 1:00p
-- 13:00a

-- Formula is governed by hours * 60 + minutes, just lots of work to
-- accept various forms of time formats
function convertMinutes(stamp)
	if stamp:sub(-1):lower() == "m" then
		return convertMinutes(stamp:sub(1, -2)) -- allow "pm" or "am" instead of just "p" or "a"
	end
	local half = stamp:sub(-1):lower()
	if half == "p" then -- strip off
		return 12 * 60 + convertMinutes(stamp:sub(1, -2))
	elseif half == "a" then
		return convertMinutes(stamp:sub(1, -2))
	end
	local hour = stamp:sub(1, -3):gsub("%D+", "")
	local minute = stamp:sub(-2, -1)
	return hour * 60 + minute
end

print(convertMinutes("1:00a"))
print(convertMinutes("01:00am"))
print(convertMinutes("1:00p"))
print(convertMinutes("1300"))

Unlike military time, this convention means that when one minute increases, the number will always increase by one, rather than by either 1 or 41.

We can compute a simple linear function:

local realStart = convertMinutes("4:30pm")
local realEnd = convertMinutes("8:00pm")

local gameStart = convertMinutes("7:00am")
local gameEnd = convertMinutes("3:30pm")

local realNow = -- (Get number of minutes since midnight)

local sinceRealStart = realNow - realStart
local dayProgress = sinceRealStart / (realEnd - realStart)
local gameNow = dayProgress * (gameEnd - gameStart) + gameStart

-- or all together:
local gameNow = (realNow - realStart) / (realEnd - realStart) * (gameEnd - gameStart) + gameStart

3 comments

0

I do like your request about using minutes after midnight and the linear function, but I'll still need how you would make this. I have handwritten a graph on postimg for an example. postimg.org/image/4klzbhim3/

0

That graph doesn't make sense. You want to plot game day VS real day -- there will be one line.

0

My bad, I thought this would involve sine for the formula. Answer approved.

This discussion is preserved as it was. New answers, comments, and votes are closed.