Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getDate()
- local result = http.get("http://worldtimeapi.org/api/timezone/America/New_York.txt").readAll() -- This is a synchronous request. Seems easier. Change to async if necessary
- print(result)
- -- Third line...
- lines = {}
- for s in result.:gmatch("[^\r\n]+") do
- table.insert(lines, s)
- end
- local date = lines[2]
- -- Now we have: datetime: 2020-07-21T00:07:29.748572-04:00
- datePieces = {}
- for s in result.readAll():gmatch("[^\r\n]+") do
- table.insert(datePieces, s)
- end
- return datePieces[1]; -- Should just get the ISO date bit
- end
- -- This is super simple and had 0 upvotes, but, I don't need much. I don't care about timezone or anything.
- -- Parse date given in any of supported forms.
- -- Note! For unrecognised format will return now.
- -- @param str ISO date. Formats:
- -- Y-m-d
- -- Y-m -- this will assume January
- -- Y -- this will assume 1st January
- -- We're looking for: datetime: 2020-07-20T23:50:33.619682-04:00
- function parseDate(str)
- local y, m, d, hour, minute, second, ms = str:match("(%d%d%d%d)-?(%d?%d?)-?(%d?%d?)%a(%d%d):(%d%d):(%d%d).(%d%d%d)")
- -- Lua doesn't do ms... but we can make it work
- -- fallback to now
- if y == nil then
- return os.time()
- end
- -- defaults
- if m == '' then
- m = 1
- end
- if d == '' then
- d = 1
- end
- -- create time
- return os.time{year=y, month=m, day=d, hour=hour, min = minute, sec = second},ms -- Returning a datetime and a number for the ms
- end
- rednet.open("front")
- targetid = 3 -- You still gotta set this if you move.
- ticks = 0
- ISODate = getDate()
- t, ms = parseDate(ISODate)
- starttime = t
- startms = ms
- endtime = starttime -- set this as we go
- endms = ms
- ratio = 1
- while true do
- ticks = ticks + 1
- if ticks == 10 then -- Every 10 ticks, twice per second, is probably OK
- ISODate = getDate()
- print("1"..ISODate)
- t, ms = parseDate(ISODate)
- print("2"..t)
- endtime = t
- endms = ms
- -- Easier said than done.
- -- I think we have to wait for an event
- -- But, our URL can maybe be http://worldtimeapi.org/api/timezone/America/New_York.txt
- -- Find the third line, datetime: 2020-07-20T23:50:33.619682-04:00
- -- Can lua parse that?
- second = os.difftime(endtime,starttime)
- second = second + (endms - startms)/1000
- tps = ticks/second
- -- Find ratio of this to the expected 20
- -- We want where 10 tps / 20expected = 0.5 speed (because of the way speed works, it's kinda backwards)
- ratio = tps/20
- print(ratio)
- -- Then send this new ratio out on rednet
- rednet.send(targetid,"speed " .. ratio)
- -- Reset
- ticks = 0
- starttime = endtime
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment