Dimencia

Clock2

Jul 20th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1.  
  2.  
  3. function getDate()
  4. 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
  5. -- Third line...
  6. 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
  7. local date = lines[2]
  8. -- Now we have: datetime: 2020-07-21T00:07:29.748572-04:00
  9. local datePieces = split(date," ")
  10. return datePieces[1]; -- Should just get the ISO date bit
  11. end
  12.  
  13.  
  14. -- This is super simple and had 0 upvotes, but, I don't need much. I don't care about timezone or anything.
  15.  
  16. -- Parse date given in any of supported forms.
  17.  
  18. -- Note! For unrecognised format will return now.
  19.  
  20. -- @param str ISO date. Formats:
  21. -- Y-m-d
  22. -- Y-m -- this will assume January
  23. -- Y -- this will assume 1st January
  24.  
  25.  
  26.  
  27. -- We're looking for: datetime: 2020-07-20T23:50:33.619682-04:00
  28.  
  29. function parseDate(str)
  30. 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)")
  31. -- Lua doesn't do ms... but we can make it work
  32. -- fallback to now
  33. if y == nil then
  34. return os.time()
  35. end
  36. -- defaults
  37. if m == '' then
  38. m = 1
  39. end
  40. if d == '' then
  41. d = 1
  42. end
  43. -- create time
  44. 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
  45. end
  46.  
  47.  
  48.  
  49. rednet.open("front")
  50. targetid = 3 -- You still gotta set this if you move.
  51.  
  52. ticks = 0
  53.  
  54. ISODate = getDate()
  55. t, ms = parseDate(ISODate)
  56. starttime = t
  57. startms = ms
  58. endtime = starttime -- set this as we go
  59. endms = ms
  60. ratio = 1
  61.  
  62.  
  63.  
  64. while true do
  65. ticks = ticks + 1
  66. if ticks == 10 then -- Every 10 ticks, twice per second, is probably OK
  67. ISODate = getDate()
  68. t, ms = parseDate(ISODate)
  69. endtime = t
  70. endms = ms
  71. -- Easier said than done.
  72. -- I think we have to wait for an event
  73. -- But, our URL can maybe be http://worldtimeapi.org/api/timezone/America/New_York.txt
  74.  
  75. -- Find the third line, datetime: 2020-07-20T23:50:33.619682-04:00
  76. -- Can lua parse that?
  77.  
  78.  
  79. second = os.difftime(endtime,starttime)
  80. second = second + (endms - startms)/1000
  81. tps = ticks/second
  82. -- Find ratio of this to the expected 20
  83. -- We want where 10 tps / 20expected = 0.5 speed (because of the way speed works, it's kinda backwards)
  84. ratio = tps/20
  85. print(ratio)
  86. -- Then send this new ratio out on rednet
  87. rednet.send(targetid,"speed " .. ratio)
  88. -- Reset
  89. ticks = 0
  90. starttime = endtime
  91. end
  92. end
Advertisement
Add Comment
Please, Sign In to add comment