Advertisement
Guest User

getTime

a guest
Mar 1st, 2015
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.31 KB | None | 0 0
  1. -- get the web page with the correct time on it - change as appropriate
  2. local file = http.get("http://www.timeanddate.com/worldclock/uk/london")
  3.  
  4. -- define patterns to locate date/time in web page
  5. local timematch = "<span id=ct class=h1>"
  6. local datematch = "<span id=ctdat>"
  7. local endmatch = "</span>"
  8.  
  9. if file == nil then
  10.   -- if web page cannot be opened, display error
  11.   print("Can't open webpage.")
  12. else
  13.   -- loop through each line of web page
  14.   while true do
  15.     -- read line
  16.     local ver = file.readLine()
  17.     -- if nil then at the end of the page, break out of loop
  18.     if ver == nil then break end
  19.     -- find the time pattern
  20.     p = string.find(ver,timematch)
  21.     -- if found,
  22.     if p ~= nil then
  23.       -- get the position of the end pattern
  24.       q = string.find(ver,endmatch,p)
  25.       -- get the string inbetween
  26.       t = string.sub(ver,p+string.len(timematch),q-1)
  27.       -- output time
  28.       write("Time: ")
  29.       print(t)
  30.     end
  31.     -- find the date pattern
  32.     p = string.find(ver,datematch)
  33.     -- if found,
  34.     if p ~= nil then
  35.       -- get the position of the end pattern
  36.       q = string.find(ver,endmatch,p)
  37.       -- get the string inbetween
  38.       d = string.sub(ver,p+string.len(datematch),q-1)
  39.       -- output date
  40.       write("Date: ")
  41.       print(d)
  42.     end
  43.   end
  44. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement