
clock
the time, on a monitor
> examples clock > edit clock
Shows the time of the Minecraft world, like 07:30, refreshed every second: on the terminal, on a monitor touching the computer, and on anything a Create Display Link can fill. Put one in every station of your railway.
What to build
Nothing is required: the time shows in the terminal. To see it in the world:
- place a CRT Monitor or an LCD Monitor against the computer: it shows the terminal;
- or point a Create Display Link at the computer, with a Display Board, Nixie Tubes or a sign as its target: it shows the two lines
Timeand07:30.
How it works
local t = (os.day_time() + 6000) % 24000
local h = math.floor(t / 1000)
local m = math.floor(t % 1000 * 60 / 1000)
local text = string.format("%02d:%02d", h, m)os.day_time() gives the time of day of the world in ticks, from 0 to 23,999. A Minecraft day lasts 24,000 ticks and starts at 0 with sunrise, which is 6 in the morning: adding 6,000 and keeping the remainder of a division by 24,000 (%) turns it into ticks since midnight. One hour is 1,000 ticks, so h is the number of whole thousands, and the rest, out of 1,000, makes the minutes. string.format() writes both with two digits: %02d pads 7 into 07.
Here is the same computation on a few times of day:
local function clock(day_time)
local t = (day_time + 6000) % 24000
local h = math.floor(t / 1000)
local m = math.floor(t % 1000 * 60 / 1000)
return string.format("%02d:%02d", h, m)
end
print(clock(0)) -- sunrise
print(clock(6000)) -- noon
print(clock(13000)) -- sunset
print(clock(18000)) -- midnight
print(clock(1500))06:00 12:00 19:00 00:00 07:30
term.clear()
term.set_cursor(1, 1)
print(text)
display.set({"Time", text})
sleep(1)Each second the screen is cleared and the time written at the top left (term.clear(), term.set_cursor()). display.set() hands the lines to the Display Links pointed at the computer: one string per line. A Minecraft minute lasts less than a second of real time (an hour is 50 seconds), so the minutes jump by one or two at each refresh.
Ideas
- A 12-hour clock:
local suffix = h < 12 and "AM" or "PM", then show(h + 11) % 12 + 1. - Show the part of the day:
if t >= 6000 and t < 19000 thenit is day, otherwise night. - Light the street at night:
rs.set("top", os.day_time() >= 12500 and os.day_time() < 23500), or go further with the day and night lights recipe. - Big digits drawn with
gfxon a wall of monitors, from the Transistor Mainframe on:gfx.text()takes a size. - Ring a bell every morning: when
hbecomes 6, pulse a note block.
The program
-- Shows the time of day. Place a monitor against the computer
-- to see it big, or point a Create Display Link at the computer.
while true do
local t = (os.day_time() + 6000) % 24000 -- 0 is 6 am
local h = math.floor(t / 1000)
local m = math.floor(t % 1000 * 60 / 1000)
local text = string.format("%02d:%02d", h, m)
term.clear()
term.set_cursor(1, 1)
print(text)
display.set({"Time", text})
sleep(1)
end