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

Get real time?

Started by Xelostar, 08 April 2018 - 12:12 PM
Xelostar #1
Posted 08 April 2018 - 02:12 PM
Hi. I know it's late, but I needed to do this myself just now and the link Lyqyd gave doesn't work for me.
I just used Google and it seems to work fine, though it's sketchy, I thought I'd share my code for the people who want to have the real time as well and stumble upon this thread using Google or something =P


function getRealTime()
	local page = http.get("http://www.google.nl/search?&q=time")
	local raw = page:readAll()
	local raw2 = ""
	for a in raw:gmatch(">%d?%d:%d%d</div>") do raw2 = a end
	local time = ""
	for a in raw2:gmatch("%d?%d:%d%d") do time = a end
	return time
end

[Split from here. -L]
Edited on 11 April 2018 - 10:52 PM
Luca_S #2
Posted 08 April 2018 - 07:00 PM
If we are necroing this:

In the newest pre releases which can be found here there's a new function to the os api:

os.epoch
This will get you the UTC/local time in the unix epoch format which you can then format.

EDIT: Also this is how you would use it in code:

local UTC = os.epoch("utc") --'Mind the lowercase utc here, I created a pull request to make it case-insensitive, but it's not yet merged
local localTime = os.epoch("local")
Edited on 08 April 2018 - 05:02 PM
Xelostar #3
Posted 09 April 2018 - 02:48 PM
Ah cool, though I don't think you'll soon be able to do that if you want to use an emulator :P/>
Also: for the piece of code I sent it's ofcourse smart to check if page not nil to make sure you won't get an error (when you don't have an internet connection at a certain moment for example)

EDIT: and it doesn't have to be google.nl, google.com would be fine too, but my browser turned it into .nl because I'm from the Netherlands =P
Edited on 09 April 2018 - 12:49 PM
Xelostar #4
Posted 12 April 2018 - 10:00 AM
It seems to have been split. There's a small problem. If someone Googles "computercraft get real time" or something along those lines, this thread won't be easily found =/