Advertisement
fatboychummy

clockyTesting.lua

Dec 2nd, 2018
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.04 KB | None | 0 0
  1. local mon = peripheral.find("monitor")
  2. local tzDif = -7
  3. local date = "????-??-??"
  4. local time = "??:?? ??"
  5. local slowWaitTime = 10
  6. local fastWaitTime = 3
  7. local cHour = 0
  8. local cMin = 0
  9. local hrsUntil = 0
  10. local DAY = 0
  11.  
  12. mon.clear()
  13. mon.setCursorPos(1,1)
  14. mon.setTextScale(4)
  15.  
  16.  
  17. os.loadAPI("json")
  18. local json = _G.json
  19. _G.json = nil
  20.  
  21.  
  22. local function redraw()
  23.   mon.clear()
  24.   mon.setCursorPos(1,1)
  25.   mon.write(date)
  26.   mon.setCursorPos(1,2)
  27.   mon.write(time)
  28.   mon.setCursorPos(1,5)
  29.   mon.write(tostring(hrsUntil) .. " hrs adv")
  30. end
  31.  
  32.  
  33. local function updateDate(yr,mn,d)
  34.   --"????-??-??"
  35.   -- 1234567890
  36.   if type(yr) == "string" then
  37.     date = yr .. date:sub(5)
  38.   end
  39.   if type(mn) == "string" then
  40.     date = date:sub(1,5) .. mn .. date:sub(8)
  41.   end
  42.   if type(d) == "string" then
  43.     if d:len() < 2 then
  44.       d = "0" .. d
  45.     end
  46.     date = date:sub(1,8) .. d
  47.   end
  48. end
  49.  
  50. local function updateTime(hr,mn)
  51.   --"??:?? ??"
  52.   -- 12345678
  53.   if type(hr) == "string" then
  54.     local ampm = "??"
  55.     local n = tonumber(hr) + tzDif
  56.     if n < 0 then
  57.       n = 24 + n
  58.       updateDate(nil,nil,tostring(DAY-1))
  59.     end
  60.     hrsUntil = 22 - n
  61.     if hrsUntil < 0 then hrsUntil = 24 + hrsUntil end
  62.     if n > 12 then
  63.       ampm = "PM"
  64.       n = n - 12
  65.     elseif n == 12 then
  66.       ampm = "PM"
  67.     elseif n < 12 then
  68.       ampm = "AM"
  69.       if n == 0 then
  70.         n = 12
  71.       end
  72.     end
  73.     n = tostring(n)
  74.     if n:len() < 2 then
  75.       n = "0" .. n
  76.     end
  77.     time = n .. time:sub(3,6) .. ampm
  78.   end
  79.   if type(mn) == "string" then
  80.     time = time:sub(1,3) .. mn .. time:sub(6)
  81.   end
  82. end
  83.  
  84. redraw()
  85.  
  86.  
  87. local function timeWatch()
  88.   while true do
  89.     if cHour == 22 and cMin == 0 then
  90.       mon.clear()
  91.       mon.setCursorPos(1,1)
  92.       mon.setTextScale(1)
  93.       shell.run("monitor right shell")
  94.       mon.setTextScale(4)
  95.     end
  96.     if cHour == 21 then
  97.       if cMin > 55 then
  98.         print()
  99.         mon.setCursorPos(1,3)
  100.         mon.write("ADVENT")
  101.         mon.setCursorPos(1,4)
  102.         mon.write("SOON")
  103.         os.sleep(fastWaitTime)
  104.       else
  105.         os.sleep(slowWaitTime)
  106.       end
  107.     else
  108.       os.sleep(slowWaitTime)
  109.     end
  110.     local h = {http.get("http://worldclockapi.com/api/json/utc/now")}
  111.  
  112.     if h[1] then
  113.       local data = json.decode(h[1].readAll())
  114.       h[1].close()
  115.       --Date / Time Format:
  116.       --[[
  117.         2018-12-02T06:13:Z
  118.       ]]
  119.       if type(data) == "table" then
  120.         if data["currentDateTime"] then
  121.           local year, month, day, hour, minute = string.match(data["currentDateTime"],"(%d+)-(%d+)-(%d+)T(%d+):(%d+)Z")
  122.           updateDate(year,month,day)
  123.           DAY = tonumber(day)
  124.           updateTime(hour,minute)
  125.           cHour = tonumber(hour)
  126.           cMin = tonumber(minute)
  127.         else
  128.           date = "????-??-?1"
  129.           time = "??:?? ??"
  130.         end
  131.       else
  132.         date = "????-??-?2"
  133.         time = "??:?? ??"
  134.       end
  135.     else
  136.       date = "????-??-?3"
  137.       time = "??:?? ??"
  138.     end
  139.     redraw()
  140.   end
  141. end
  142. timeWatch()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement