Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
Nickoakz 231 Moderation Voter
9 years ago

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/

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? BlueTaslem 18071 — 9y
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. Nickoakz 231 — 9y
0
What do you plan to do with the rest of the day? BlueTaslem 18071 — 9y
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. Nickoakz 231 — 9y

1 answer

Log in to vote
4
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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
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/ Nickoakz 231 — 9y
0
That graph doesn't make sense. You want to plot game day VS real day -- there will be one line. BlueTaslem 18071 — 9y
0
My bad, I thought this would involve sine for the formula. Answer approved. Nickoakz 231 — 9y
Ad

Answer this question