Advertisement
dustojnikhummer

Computercraft datetime

Jan 11th, 2025
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.88 KB | Gaming | 0 0
  1. -- Wrap the monitor on the right side
  2. local monitor = peripheral.wrap("right")
  3.  
  4. -- Function to convert in-game days to a calendar date
  5. local function convertToDate(day)
  6.     local year = math.floor((day - 1) / 365) + 1
  7.     local dayOfYear = (day - 1) % 365 + 1
  8.  
  9.     local months = {
  10.         {name = "January", days = 31},
  11.         {name = "February", days = 28},
  12.         {name = "March", days = 31},
  13.         {name = "April", days = 30},
  14.         {name = "May", days = 31},
  15.         {name = "June", days = 30},
  16.         {name = "July", days = 31},
  17.         {name = "August", days = 31},
  18.         {name = "September", days = 30},
  19.         {name = "October", days = 31},
  20.         {name = "November", days = 30},
  21.         {name = "December", days = 31},
  22.     }
  23.  
  24.     local month = 1
  25.     local dayOfMonth = dayOfYear
  26.  
  27.     for i, m in ipairs(months) do
  28.         if dayOfMonth <= m.days then
  29.             month = i
  30.             break
  31.         end
  32.         dayOfMonth = dayOfMonth - m.days
  33.     end
  34.  
  35.     return dayOfMonth, month, year
  36. end
  37.  
  38. -- Function to print the current time and date
  39. local function printTimeAndDate()
  40.     -- Clear the monitor
  41.     monitor.clear()
  42.     monitor.setCursorPos(1, 1)
  43.  
  44.     -- Get the current time and date
  45.     local time = os.time()
  46.     local day = os.day()
  47.  
  48.     -- Convert the in-game day to a calendar date
  49.     local dayOfMonth, month, year = convertToDate(day)
  50.  
  51.     -- Format the time (in-game time from 0 to 24)
  52.     local hours = math.floor(time)
  53.     local minutes = math.floor((time - hours) * 60)
  54.  
  55.     -- Print the date and time on the monitor
  56.     monitor.write(string.format("Date: %02d/%02d/%03d", dayOfMonth, month, year))
  57.     monitor.setCursorPos(1, 2)
  58.     monitor.write(string.format("Time: %02d:%02d", hours, minutes))
  59. end
  60.  
  61. -- Main loop to update the time and date every second
  62. while true do
  63.     printTimeAndDate()
  64.     sleep(1)
  65. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement