This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
chaospepsi's profile picture

Clocks

Started by chaospepsi, 11 July 2013 - 02:57 AM
chaospepsi #1
Posted 11 July 2013 - 04:57 AM
Hi, I have a little LUA experience, and I'd like to make a computer that displays the built-in clock function on a monitor. I can get this to do it once, but I'd like it to update every second, so that the clock functions normally and displays the ingame time. Any suggestions?
Lyqyd #2
Posted 11 July 2013 - 01:37 PM
Split into new topic.

Please post your current code and any errors that you receive while running it.
Molinko #3
Posted 11 July 2013 - 06:32 PM

local time = ""
 while true do
  time = textutils.formatTime(os.clock,false)
 term.setCursorPos(1,1)
 print(time)
 sleep(.5)
 end
H4X0RZ #4
Posted 11 July 2013 - 07:20 PM

local time = ""
 while true do
  time = textutils.formatTime(os.clock,false)
 term.setCursorPos(1,1)
 print(time)
 sleep(.5)
 end
More little:

while true do
term.clear()
term.setCursorPos(1,1)
write(textutils.formatTime(os.time(),false))
sleep(.8) --Because a minecraft second is 0.8 real seconds long
end
albrat #5
Posted 12 July 2013 - 08:20 AM
I have monitor = peripheral.wrap(monitorside) – where monitor side is your monitors contact side.

varibles and string split function that seems to work very well for many things… (used in my time function)

-- local variable
local daytext = { "Sunday", "Saturday", "Friday", "Thursday", "Wednesday", "Tuesday", "Monday" }
local monitor = peripheral.wrap("front")  -- My monitor is on the front of the computer.
monitor.setTextScale(1)
local t = os.time()
local cpx = 1
local cpy = 1
-- Split script
function string:split( inSplitPattern, outResults )
   if not outResults then
	  outResults = { }
   end
   local theStart = 1
   local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
   while theSplitStart do
	  table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
	  theStart = theSplitEnd + 1
	  theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
   end
   table.insert( outResults, string.sub( self, theStart ) )
   return outResults
end

Time display code, outputs Time, week number, text day. eg Sunday


-- Time display maybe
function time()
cpx, cpy = monitor.getCursorPos()
monx, mony = monitor.getSize()
monitor.setCursorPos((monx/2)-7,2)
monitor.clearLine()
local t = os.time()
local d = os.day()
local dmath = d/7
local weeks = string.split(dmath, "%.")
local week = tonumber(weeks[1])
local weekday = (7-(d - (weeks[1]*7)))

if weekday == 0 then weekday = 7 end
monitor.write("Time : "..textutils.formatTime(t,false))
monitor.setCursorPos((monx/2)-7,3)
monitor.clearLine()
monitor.write("Week : "..week)
monitor.setCursorPos((monx/2)-6,4)
monitor.clearLine()
monitor.write("Day : "..daytext[weekday])
monitor.setCursorPos(cpx, cpy)
end

The function is called with time()

I have this function sat in my server's listening system, it listens to rednet.receive(0.8) (the 0.8 is the time for a minecraft minute)
theoriginalbit #6
Posted 12 July 2013 - 08:36 AM

-- Split script
function string:split( inSplitPattern, outResults )
   if not outResults then
	  outResults = { }
   end
   local theStart = 1
   local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
   while theSplitStart do
	  table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
	  theStart = theSplitEnd + 1
	  theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
   end
   table.insert( outResults, string.sub( self, theStart ) )
   return outResults
end

A better split function

local function split( str, patt )
  local t = {} --# table of the output from splitting
  for s in str:gmatch("[^"..patt.."]+") do
    t[#t+1] = s
  end
  return t
end
Also there is some slightly better math you can use to calculate day, week, month, year (including leap years), these can be found in CCalendar.
Engineer #7
Posted 12 July 2013 - 10:14 AM
A better split function

local function split( str, patt )
  local t = {} --# table of the output from splitting
  for s in str:gmatch("[^"..patt.."]+") do
	t[#t+1] = s
  end
  return t
end

This is a better split function :P/>

local split = function( s, sPat )
	local captures = {}
	for capture in s:gmatch( "[^" .. ( 
		function( str )
			local safePat = ""
			local mChars = {
				["*"] = true; ["+"] = true; ["-"] = true;
				["["] = true; ["]"] = true; ["."] = true;
				["("] = true; [")"] = true; ["%"] = true;
				["$"] = true; ["^"] = true; ["?"] = true;
			}

			for i = 1, str:len() do
				if mChars[str:sub( i, i )] then
					safePat = safePat .. "%" .. str:sub( i, i )
				else
					safePat = safePat .. str:sub( i, i )
				end
			end
			return safePat
		end 
	)( sPat ) .. "]+" ) do
		table.insert( captures, capture )
	end
	return captures
end
theoriginalbit #8
Posted 12 July 2013 - 05:34 PM
This is a better split function :P/>

Not so much better, more like better defined but with more bugs…
Unlike changing from albrat's solution to the gmatch version which has several improvements, this just assumes that the passed in string is not pattern safe, and makes it safe, but what happens if I already give it a safe pattern? i.e. "%$6" … well it would break, it would then supply anything matching "%$6" as your anonymous function would make it "%%%$6"… So the idea behind making the pattern supplied safe is nice, it can be very problematic!
Engineer #9
Posted 12 July 2013 - 06:04 PM
Well, with this you can simply do a plain string. Which is in my opinion better. But like you have said, it could be problematic.
Zudo #10
Posted 13 July 2013 - 02:00 AM
Wow, that escalated quickly!