Dimencia

Clock1

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