Advertisement
Guest User

clock.lua

a guest
Feb 5th, 2024
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.96 KB | None | 0 0
  1. local monitor = peripheral.find('monitor')
  2.  
  3. function getFormattedTime()
  4.     local now = os.time()
  5.     local hour = math.floor(now)
  6.     local minute = math.floor(now * 60) % 60
  7.     -- local second = math.floor(now * 60 * 60) % 60
  8.     return ("%02d:%02d"):format(hour, minute)
  9. end
  10.  
  11. function canSleep()
  12.     local now = os.time()
  13.     local tick = math.floor(((now + (24 - 6)) % 24) * 1000)
  14.     -- print(tick)
  15.     return 12542 <= tick and tick < 23460
  16. end
  17.  
  18. function setup()
  19.     monitor.setTextScale(1)
  20. end
  21.  
  22. function setColor()
  23.     if canSleep() then
  24.         monitor.setTextColor(colors.white)
  25.         monitor.setBackgroundColor(colors.black)
  26.     else
  27.         monitor.setTextColor(colors.black)
  28.         monitor.setBackgroundColor(colors.white)
  29.     end
  30. end
  31.  
  32. function update()
  33.     setColor()
  34.  
  35.     monitor.clear()
  36.     monitor.setCursorPos(2, 3)
  37.     monitor.write(getFormattedTime())
  38. end
  39.  
  40. setup()
  41.  
  42. while true do
  43.     update()
  44.     os.sleep()
  45. end
  46.  
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement