Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rednet.open("right")
- rednet.open("left")
- rednet.open("down")
- rednet.open("up")
- rednet.open("front")
- rednet.open("back")
- 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()
- t, ms = parseDate(ISODate)
- 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
- function getDate()
- local result = http.get(http://worldtimeapi.org/api/timezone/America/New_York.txt) -- This is a synchronous request. Seems easier. Change to async if necessary
- -- Third line...
- local lines = split(result.readAll(),"[^\r\n]+") -- Don't know if this works, can also try a literal newline inside the string, and one source said backslash before it
- local date = lines[2]
- -- Now we have: datetime: 2020-07-21T00:07:29.748572-04:00
- local datePieces = split(date," ")
- 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
- --[[
- --Tests:
- print( os.date( "%Y-%m-%d", parseDate("2019-12-28") ) )
- print( os.date( "%Y-%m-%d", parseDate("2019-12") ) )
- print( os.date( "%Y-%m-%d", parseDate("2019") ) )
- ]]
Advertisement
Add Comment
Please, Sign In to add comment