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

os.clock() returning wonky times?

Started by Tassyr, 03 April 2013 - 11:46 AM
Tassyr #1
Posted 03 April 2013 - 01:46 PM
So i tried to set up a circuit using



local x = os.clock()
print (textutils.formatTime(x))


to output the real world time when something tripped.
Catch is? It's given me six answers and none close to the right one. at the most extreme I got 5:05AM and 12:58 PM about a minute apart from eachother.

What's going on? XD

For reference when this clicked on, it was 4:50 PM where I am.
Bubba #2
Posted 03 April 2013 - 01:55 PM
Heh, os.clock returns the minecraft time, not your actual local time. You'd have to use some http magic to do that.
Pinkishu #3
Posted 03 April 2013 - 01:57 PM
"Returns the amount of time the Computer has been running, note that this may not be the amount of time the program has been running. the function os.clock() is useful for debugging, see the example."

Time the computer (the ingame CC computer has been running (in seconds); it doesn't say anything about giving you any kind of real world time

Heh, os.clock returns the minecraft time, not your actual local time. You'd have to use some http magic to do that.

os.time returns MC time ;)/>
Tassyr #4
Posted 03 April 2013 - 01:59 PM
Oh, damn. Guess I misunderstood. Is there an easy way to get realside time ingame? XD
Bubba #5
Posted 03 April 2013 - 01:59 PM
"Returns the amount of time the Computer has been running, note that this may not be the amount of time the program has been running. the function os.clock() is useful for debugging, see the example."

Time the computer (the ingame CC computer has been running (in seconds); it doesn't say anything about giving you any kind of real world time

Heh, os.clock returns the minecraft time, not your actual local time. You'd have to use some http magic to do that.

os.time returns MC time ;)/>

Oops. Well anyway, it's both Minecraft tick related regardless.

Oh, damn. Guess I misunderstood. Is there an easy way to get realside time ingame? XD

Use the http api. I wrote a tutorial on it earlier today (check the tutorial section). Try to get the text from this website. If you need any help just ask :)/>
Tassyr #6
Posted 03 April 2013 - 02:08 PM
Use the http api. I wrote a tutorial on it earlier today (check the tutorial section). Try to get the text from this website. If you need any help just ask :)/>

Oh good lord it's like looking at chinese. XD Yeah I'm very stuck. I don't even know where to begin.
Bubba #7
Posted 03 April 2013 - 02:11 PM
Use the http api. I wrote a tutorial on it earlier today (check the tutorial section). Try to get the text from this website. If you need any help just ask :)/>

Oh good lord it's like looking at chinese. XD Yeah I'm very stuck. I don't even know where to begin.

Heh that's alright. Well I'm looking into it right now. I'll write something up and then post it.
Bubba #8
Posted 03 April 2013 - 02:17 PM
Okay well it's not elegant, but it gets the job done. Replace the url to whatever timezone you're in, but make sure it uses the same site.

local time_url = "http://www.worldtimeserver.com/current_time_in_US-IN.aspx" --Replace this
local function getTime()
	local request = http.get(time_url) --Gets the data from the requested site
	local time_content = request.readAll()
	request.close()
	local search_text = [[<div id="analog%-digital">.-<span class="font7">(.-)</span>]] --The text that it needs to search for
	local _,_, global_time = time_content:find(search_text) --Actually search for it.
	local global_time = global_time:gsub("%s", "") --Strip any whitespace
	return global_time --Returns a string of the current time
end

print("The time is: "..getTime().. " in Indiana")

Pastebin if you prefer it.

Oh yeah, and http needs to be enabled in the ComputerCraft cfg file.
Tassyr #9
Posted 03 April 2013 - 02:25 PM
testing momentarily. For now, this is what it looks like:




local time_url = "http://www.worldtimeserver.com/current_time_in_US-CA.aspx" --Replace this
rs.setOutput("back",true)
while true do
sleep (5)
c = rs.getInput("bottom")
if c == true then
rs.setOutput("back", false)
print ("Circuit Tripped at ")
getTime()
sleep (3600)
rs.setOutput("back", true)
end
end

local function getTime()
		local request = http.get(time_url)
		local time_content = request.readAll()
		request.close()
		local search_text = [[<div id="analog%-digital">.-<span class="font7">(.-)</span>]]
		local _,_, global_time = time_content:find(search_text)
		local global_time = global_time:gsub("%s", "")
		print(global_time)
end

Bubba #10
Posted 03 April 2013 - 02:30 PM
testing momentarily. For now, this is what it looks like:




local time_url = "http://www.worldtimeserver.com/current_time_in_US-CA.aspx" --Replace this
rs.setOutput("back",true)
while true do
sleep (5)
c = rs.getInput("bottom")
if c == true then
rs.setOutput("back", false)
local x = getTime()
print ("Circuit Tripped at "..(x))
sleep (3600)
rs.setOutput("back", true)
end
end

local function getTime()
   local request = http.get(time_url)
   local time_content = request.readAll()
   request.close()
   local search_text = [[<div id="analog%-digital">.-<span class="font7">(.-)</span>]]
   local _,_, global_time = time_content:find(search_text)
   local global_time = global_time:gsub("%s", "")
   print(global_time)
end


Better yet, replace that sleep(5) with os.pullEvent("redstone") and you'll get the exact time.

local time_url = "http://www.worldtimeserver.com/current_time_in_US-CA.aspx" --Replace this

local function getTime()
   local request = http.get(time_url)
   local time_content = request.readAll()
   request.close()
   local search_text = [[<div id="analog%-digital">.-<span class="font7">(.-)</span>]]
   local _,_, global_time = time_content:find(search_text)
   local global_time = global_time:gsub("%s", "")
   print(global_time)
end
while true do
  rs.setOutput("back", true)
  os.pullEvent("redstone") --Waits until redstone changes states
  if not rs.getInput("back") then
	print("Circuit tripped at: "..getTime())
  end
end
Tassyr #11
Posted 03 April 2013 - 02:36 PM
Okay, hang on, because yours is drastically different from mine. Lemme explain what I was trying to do; my system was set up to:
1) send out a redstone signal from the back plate of the computer in startup.
2) wait until it receives a signal from the bottom, an MFSU, indicating the MFSU is charged.
3) output the time at which this was tripped.
4) toggle OFF the redstone coming out of the back for a set amount of time (experimenting with that. An hour seemed to be about right.) To prevent it from drawing constant trickle power.
5) after the time is up, reset and send out the back plate redstone signal again.
Bubba #12
Posted 03 April 2013 - 02:38 PM
Okay, hang on, because yours is drastically different from mine. Lemme explain what I was trying to do; my system was set up to:
1) send out a redstone signal from the back plate of the computer in startup.
2) wait until it receives a signal from the bottom, an MFSU, indicating the MFSU is charged.
3) output the time at which this was tripped.
4) toggle OFF the redstone coming out of the back for a set amount of time (experimenting with that. An hour seemed to be about right.) To prevent it from drawing constant trickle power.
5) after the time is up, reset and send out the back plate redstone signal again.

Ah. Misunderstood what you were trying to do there. But anyway, all that you have to do if you wanted to use mine is add your hour long sleep/turning off redstone in after the print("Circuit tripped at") part. Oh and change the if not rs.getInput('back') to if not rs.getInput('bottom').
Tassyr #13
Posted 03 April 2013 - 02:47 PM
But wait. Wouldn't 'if not rs.getInput('bottom') basically tell it to run this when there -wasn't- a signal coming from there?
Bubba #14
Posted 03 April 2013 - 02:52 PM
But wait. Wouldn't 'if not rs.getInput('bottom') basically tell it to run this when there -wasn't- a signal coming from there?

Yup. My bad. Would have to change to 'if rs.getInput('bottom')' instead. My point though was that it's better to use os.pullEvent('redstone') as opposed to the sleep(5).
Tassyr #15
Posted 03 April 2013 - 02:52 PM
OH! Okay, sorry about that. Yeah, the sleep(5) comes from, well, me self-teaching myself this stuff. XD without it it errored out for some reason. Testing.
Bubba #16
Posted 03 April 2013 - 02:56 PM
OH! Okay, sorry about that. Yeah, the sleep(5) comes from, well, me self-teaching myself this stuff. XD without it it errored out for some reason. Testing.

Yeah, that's some java side code to prevent a person from having a continuous loop without yielding. Your method works, but it's more efficient to use os.pullEvent :)/>
Tassyr #17
Posted 03 April 2013 - 02:56 PM
Nope: "startup:16: Attempt to concentrate string and nil."

the call for your print("Circuit tripped at: "..getTime())
line makes it choke :(/>
Bubba #18
Posted 03 April 2013 - 02:59 PM
Nope: "startup:16: Attempt to concentrate string and nil."

the call for your print("Circuit tripped at: "..getTime())
line makes it choke :(/>
Oops. My function did not return the time value.


local function getTime()
   if not http then
	 print('HTTP is required')
	 return "Needs http to be enabled in the config"
   end
   local request = http.get(time_url)
   local time_content = request.readAll()
   request.close()
   local search_text = [[<div id="analog%-digital">.-<span class="font7">(.-)</span>]]
   local _,_, global_time = time_content:find(search_text)
   local global_time = global_time:gsub("%s", "")
   return global_time
end


Fixed.
Tassyr #19
Posted 03 April 2013 - 03:11 PM
That did it! You're amazing! I'm probably going to be up way too late tonight, ripping into this code and figuring out what other things I can do with http. This will either be amazing, or doom me. XD


thanks either way! :D/>
Tassyr #20
Posted 03 April 2013 - 03:13 PM
And just for the sake of wondering why I have this- I have my reactors set up to be 'on demand.' there's an MFSU attached to each bank of reactors, so when power is drawn out of it, the reactors turn on. This is to keep the farm, which uses a constant low trickle, from just running the reactor into the ground over about 100 EU/t. XD
Bubba #21
Posted 03 April 2013 - 03:51 PM
Glad I could help :)/> It sounds like a pretty cool system. I can't say that I've ever been very in to MFSU but that's mainly because it takes a lot of work to set those things up and I suck at it XD
theoriginalbit #22
Posted 03 April 2013 - 04:55 PM
"Returns the amount of time the Computer has been running, note that this may not be the amount of time the program has been running. the function os.clock() is useful for debugging, see the example."

Time the computer (the ingame CC computer has been running (in seconds); it doesn't say anything about giving you any kind of real world time

Heh, os.clock returns the minecraft time, not your actual local time. You'd have to use some http magic to do that.

os.time returns MC time ;)/>

Oops. Well anyway, it's both Minecraft tick related regardless.
Just to actually clarify…

os.clock() is actually seconds and milliseconds since the computer started… so using os.clock at the start of your code and the end of your code tells you how long it was running for, try this example

local c = os.clock()
sleep(1)
print( os.clock() - c )
this will print out 1.0 …

os.time() is the minecraft time… and using the textutils.formatTime will give it to you in a nice readable format…