Dimencia

Clock4

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