os.time

From ComputerCraft Wiki
Jump to: navigation, search


Grid Redstone.png  Function os.time
Returns the current in-game time.
Syntax os.time()
Returns number the current in-game time
Part of ComputerCraft
API OS

Contents

Examples

Grid paper.png  Example
Prints current time in 12 hour format (with AM and PM).
Code
local time = os.time()
local formattedTime = textutils.formatTime(time, false)

print("The time is " .. formattedTime)

Additional Notes


Using the returned value

os.time() will return a value between 0.000 and 23.999. This corresponds to the day-night cycle of the server. Midnight is at 0.000 (when the cycle resets), while midday is at 12.000. 6.000 is the morning, when the player will wake up if they slept in a bed. The player can first get into bed for the night at 18.541.

Each full day/night cycle corresponds to 20 minutes (1,200 seconds) in real-life, or 24 'hours' on the os.time scale. Each 'hour' on the os.time scale is 50 seconds in real-life. After the decimal place, tenths represent 5 seconds, hundredths represent 0.5 seconds, and thousandths represent 0.05 seconds (one tick).

Conversion

Minecraft ticks Real-time Equivalent Elapsed time os.time()
0 6:00 0:00 6.000
1000 7:00 0:50 7.000
2000 8:00 1:40 8.000
3000 9:00 2:30 9.000
4000 10:00 3:20 10.000
5000 11:00 4:10 11.000
6000 12:00 5:00 12.000
7000 13:00 5:50 13.000
8000 14:00 6:40 14.000
9000 15:00 7:30 15.000
10000 16:00 8:20 16.000
11000 17:00 9:10 17.000
12000 18:00 10:00 18.000
13000 19:00 10:50 19.000
14000 20:00 11:40 20.000
15000 21:00 12:30 21.000
16000 22:00 13:20 22.000
17000 23:00 14:10 23.000
18000 0:00 15:00 0.000
19000 1:00 15:50 1.000
20000 2:00 16:40 2.000
21000 3:00 17:30 3.000
22000 4:00 18:20 4.000
23000 5:00 19:10 5.000

Minecraft ticks can be used in commands to set server time. For example, you can set the time to midnight by typing in /time set 18000 . The real-time equivalent shows what the time would look like if it were in real-life. Elapsed time indicates how many seconds would have elapsed from the point that your character gets of bed in the morning.


To convert os.time() to real-life seconds since the in-game midnight, you can multiply it by 50. Example:

local secondsSinceMidnight = os.time() * 50

To convert os.time() to minecraft ticks, multiply by 1,000, add 18,000, and then use a modulo (%) to ensure the value is within 0 and 23999.

local MCTicks = (os.time() * 1000 + 18000)%24000

See also

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox