Posted 01 March 2015 - 12:33 PM
I was looking for a simple way to get the real world time and date so that I could use it for a log system for my reactor/turbine program but I couldn't see one.
So I wrote my own. It's really just a pattern matching routine, so you could use it to extract any sort of data from a web page.
http://pastebin.com/vFMvqQ97
So I wrote my own. It's really just a pattern matching routine, so you could use it to extract any sort of data from a web page.
http://pastebin.com/vFMvqQ97
-- get the web page with the correct time on it - change as appropriate
local file = http.get("http://www.timeanddate.com/worldclock/uk/london")
-- define patterns to locate date/time in web page
local timematch = "<span id="ct" class="h1">"
local datematch = "<span id="ctdat">"
local endmatch = "</span>"
if file == nil then
-- if web page cannot be opened, display error
print("Can't open webpage.")
else
-- loop through each line of web page
while true do
-- read line
local ver = file.readLine()
-- if nil then at the end of the page, break out of loop
if ver == nil then break end
-- find the time pattern
p = string.find(ver,timematch)
-- if found,
if p ~= nil then
-- get the position of the end pattern
q = string.find(ver,endmatch,p)
-- get the string inbetween
t = string.sub(ver,p+string.len(timematch),q-1)
-- output time
write("Time: ")
print(t)
end
-- find the date pattern
p = string.find(ver,datematch)
-- if found,
if p ~= nil then
-- get the position of the end pattern
q = string.find(ver,endmatch,p)
-- get the string inbetween
d = string.sub(ver,p+string.len(datematch),q-1)
-- output date
write("Date: ")
print(d)
end
end
end