DaikiKaminari

displayHour

May 10th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.47 KB | None | 0 0
  1. if not fs.exists("json") then
  2.     shell.run("pastebin get 4nRg9CHU json")
  3. end
  4. os.loadAPI("json")
  5.  
  6.  
  7. --- UTILS ----
  8. local function split(inputstr, sep)
  9.     if sep == nil then sep = "%s" end
  10.     local t={}
  11.     for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  12.             table.insert(t, str)
  13.     end
  14.     return t
  15. end
  16.  
  17. local function writeCenter(str, width, y)
  18.     local x =  (width - string.len(str) + 1) / 2
  19.     term.setCursorPos(x, y)
  20.     term.write(str)
  21. end
  22.  
  23. --- FUNCTIONS ---
  24. local function getDatetime()
  25.     local request = http.get("http://worldtimeapi.org/api/timezone/Europe/Paris.json")
  26.     if request == nil then
  27.         print("Not able to get datetime on the API.")
  28.         return nil
  29.     end
  30.     local text = request.readAll()
  31.     local jsonDatetime = json.decode(text)
  32.     local datetime = jsonDatetime.utc_datetime
  33.     local offset = jsonDatetime.utc_offset
  34.     local dayOfWeek = jsonDatetime.day_of_week
  35.  
  36.     local days = {
  37.         [0] = "dimanche",
  38.         [1] = "lundi",
  39.         [2] = "mardi",
  40.         [3] = "jeudi",
  41.         [4] = "vendredi",
  42.         [5] = "samedi",
  43.         [6] = "dimanche"
  44.     }
  45.  
  46.     local infos = {}
  47.     infos["dayOfWeek"] = days[dayOfWeek]
  48.     local date = split(split(datetime, "T")[1], "-")
  49.     infos["year"] = date[1]
  50.     infos["month"] = date[2]
  51.     infos["day"] = date[3]
  52.     local time = split(split(datetime, "T")[2], ":")
  53.     local offset = split(split(offset, ":")[1], "+")[1]
  54.     infos["hour"] = (tonumber(time[1]) + tonumber(offset)) % 24
  55.     infos["min"] = time[2]
  56.     infos["sec"] = split(time[3], ".")[1]
  57.  
  58.     return infos
  59. end
  60.  
  61. local function main()
  62.     local mon = peripheral.find("monitor")
  63.     if mon then
  64.         term.redirect(mon)
  65.         mon.setTextScale(2)
  66.     end
  67.     local w,h = term.getSize()
  68.  
  69.     local infos
  70.     while true do
  71.         infos = getDatetime()
  72.         if infos then
  73.             term.clear()
  74.             term.setTextColor(colors.blue)
  75.             writeCenter(string.upper(infos["dayOfWeek"]), w, 1)
  76.             writeCenter(infos["day"] .. "/" .. infos["month"] .. "/" .. infos["day"], w, 3)
  77.             writeCenter(infos["hour"] .. ":" .. infos["min"] .. ":" .. infos["sec"], w, 5)
  78.  
  79.             if infos["dayOfWeek"] == "dimanche" and infos["hour"] == "23" then
  80.                 term.setTextColor(colors.red)
  81.                 writeCenter("RESET", w, 2)
  82.                 writeCenter("BIENTOT", w, 4)
  83.             end
  84.         end
  85.         sleep(1)
  86.     end
  87. end
  88.  
  89. main()
Add Comment
Please, Sign In to add comment