Dimencia

Clock5

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