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/
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