Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local monitorSide = "left"
- local refreshInterval = 5 --seconds
- function secondsToHMS(seconds, leadingZero)
- local h = math.floor(seconds / 3600)
- local m = math.floor(seconds % 3600 / 60)
- local s = seconds % 60
- if leadingZero then
- h = tostring(h)
- m = tostring(m)
- s = tostring(s)
- if string.len(h) < 2 then
- h = "0"..h
- end
- if string.len(m) < 2 then
- m = "0"..m
- end
- if string.len(s) < 2 then
- s = "0"..s
- end
- end
- return h, m, s
- end
- function getIngameEpochAlternative()
- --ingame days in hours plus ingame day hours converted ( * 3600 / 72 ) to real-world millis
- return ( os.time("ingame") + os.day("ingame") * 24 ) * 50 * 1000
- end
- term.clear()
- term.setCursorPos(1,1)
- local saveData = {}
- local utcStarted = os.epoch("utc")
- local ingameStarted = getIngameEpochAlternative()
- saveData["utcResumed"] = 0
- saveData["ingameResumed"] = 0
- local file = fs.open("timer.save","r")
- if file ~= nil then
- local data = file.readAll()
- file.close()
- saveData = textutils.unserialize(data)
- local h, m, s = secondsToHMS(math.floor(saveData["utcResumed"]/1000), true)
- print(h .. ":" .. m .. ":" .. s .. " since the timer was started, resuming...")
- else
- file = fs.open("timer.save","w")
- file.write(textutils.serialize(saveData))
- file.close()
- end
- local utcResumed = saveData["utcResumed"]
- local ingameResumed = saveData["ingameResumed"]
- term.setCursorPos(1,3)
- term.write("UTC:")
- term.setCursorPos(1,5)
- term.write("ingame:")
- term.setCursorPos(1,17)
- term.write("[R] - Reset")
- local monitor = peripheral.wrap(monitorSide)
- if monitor~=nil then
- monitor.setTextColor(colors.black)
- monitor.setBackgroundColor(colors.orange)
- end
- while true do
- --print time(s)
- local utc = os.epoch("utc") - utcStarted + utcResumed
- local ingame = getIngameEpochAlternative() - ingameStarted + ingameResumed
- term.setCursorPos(1,4)
- local h, m, s = secondsToHMS(math.floor(utc/1000), true)
- term.write(h .. ":" .. m .. ":" .. s)
- term.setCursorPos(1,6)
- local h, m, s = secondsToHMS(math.floor(ingame/1000), true)
- term.write(h .. ":" .. m .. ":" .. s)
- if monitor~=nil then
- monitor.clear()
- monitor.setTextScale(5)
- monitor.setCursorPos(1,1)
- monitor.write(h .. ":" .. m)
- end
- --update saveData
- saveData["utcResumed"] = utc
- saveData["ingameResumed"] = ingame
- file = fs.open("timer.save","w")
- file.write(textutils.serialize(saveData))
- file.close()
- --start refresh timer
- local timer = os.startTimer(refreshInterval)
- local event, b, c, d = os.pullEvent()
- os.cancelTimer(timer)
- --listen for events (timer, key press, ...)
- if event == "key" then
- if keys.getName(b) == "r" then
- utcStarted = os.epoch("utc")
- ingameStarted = getIngameEpochAlternative()
- utcResumed = 0
- ingameResumed = 0
- term.setCursorPos(1,1)
- term.clearLine()
- end
- end
- end
Add Comment
Please, Sign In to add comment