Dimencia

Clock3

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