mickael49300_fr

horloge paris v5.5

Dec 17th, 2023
642
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.65 KB | None | 0 0
  1. -- Récupère l'heure et la date de Paris en temps réel
  2. function getParisDateTime()
  3.   local url = "http://worldtimeapi.org/api/timezone/Europe/Paris"
  4.   local response = http.get(url)
  5.   local data = response.readAll()
  6.   response.close()
  7.   local time = textutils.unserializeJSON(data)
  8.   local date = string.sub(time.datetime, 9, 10) .. "/" .. string.sub(time.datetime, 6, 7) .. "/" .. string.sub(time.datetime, 1, 4)
  9.   local time = string.sub(time.datetime, 12, 19)
  10.   return date, time
  11. end
  12.  
  13. -- Fonction pour ajuster la taille d'affichage en fonction du nombre de moniteurs
  14. function adjustDisplaySize(monitor)
  15.   local size = #peripheral.getNames("monitor")
  16.   if size == 0 then
  17.     size = 1
  18.   end
  19.   monitor.setTextScale(math.floor(4 / size))
  20. end
  21.  
  22. -- Initialise le moniteur
  23. local monitor = peripheral.wrap("top")
  24. adjustDisplaySize(monitor)
  25. monitor.setTextColor(colors.white)
  26.  
  27. -- Boucle principale : affiche la date et l'heure de Paris en temps réel sur le moniteur sur la même ligne qui alterne toute les 5 seconde
  28. while true do
  29.   local date, time = getParisDateTime()
  30.   local monitorWidth = monitor.getSize()
  31.   local textWidth = string.len(time,date)
  32.   local xPos = math.floor((monitorWidth - textWidth) / 2) + 1
  33.   monitor.clear()
  34.   monitor.setCursorPos(xPos, 1)
  35.   monitor.write(time)
  36.   sleep(1)
  37.   local date, time = getParisDateTime()
  38.   local monitorWidth = monitor.getSize()
  39.   local textWidth = string.len(time,date)
  40.   local xPos = math.floor((monitorWidth - textWidth) / 2) + 1
  41.   monitor.clear()
  42.   monitor.setCursorPos(xPos, 1)
  43.   monitor.write(time)
  44.   sleep(1)
  45.   local date, time = getParisDateTime()
  46.   local monitorWidth = monitor.getSize()
  47.   local textWidth = string.len(time,date)
  48.   local xPos = math.floor((monitorWidth - textWidth) / 2) + 1
  49.   monitor.clear()
  50.   monitor.setCursorPos(xPos, 1)
  51.   monitor.write(time)
  52.   sleep(1)
  53.   local date, time = getParisDateTime()
  54.   local monitorWidth = monitor.getSize()
  55.   local textWidth = string.len(time,date)
  56.   local xPos = math.floor((monitorWidth - textWidth) / 2) + 1
  57.   monitor.clear()
  58.  monitor.setCursorPos(xPos, 1)
  59.   monitor.write(time)
  60.   sleep(1)
  61.   local date, time = getParisDateTime()
  62.   local monitorWidth = monitor.getSize()
  63.   local textWidth = string.len(time,date)
  64.   local xPos = math.floor((monitorWidth - textWidth) / 2) + 1
  65.   monitor.clear()
  66.   monitor.setCursorPos(xPos, 1)
  67.   monitor.write(time)
  68.   sleep(1)
  69.   local date, time = getParisDateTime()
  70.   local monitorWidth = monitor.getSize()
  71.   local textWidth = string.len(time,date)
  72.   local xPos = math.floor((monitorWidth - textWidth) / 2) + 1
  73.   monitor.clear()
  74.   monitor.setCursorPos(xPos, 1)
  75.   monitor.write(date)
  76.   sleep(5)
  77. end
Advertisement
Add Comment
Please, Sign In to add comment