Advertisement
VADemon

GetTime script for ComputerCraft

Apr 4th, 2013
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.52 KB | None | 0 0
  1. print("Sending an HTTP request...")
  2. local getres = http.get("http://www.worldtimeserver.com/current_time_in_DE.aspx?city=Berlin")
  3.  
  4. local readRes = getres.readAll()
  5.  
  6. local unfilteredTime = string.sub(readRes,string.find(readRes, "(%d+:%d+) %aM")) --find time with AP/PM
  7. local s, e = string.find(readRes, "<strong>%s+%u(.+)%d?</strong>")
  8. s, e = s + 49, e - 9
  9. local worldDate = string.sub( readRes, s, e)
  10. --print( unfilteredTime )
  11.  
  12. local unfHour = string.sub(string.sub(unfilteredTime, string.find( unfilteredTime, "%d+:" ) ), 1, -2)
  13. local unfMinute = string.sub(string.sub(unfilteredTime, string.find( unfilteredTime, ":%d+" ) ), 2, 4)
  14. --print("Its "..unfHour..":"..unfMinute)
  15. local apm = string.lower(string.sub(unfilteredTime, -2)) --find out PM or AM
  16. --print(apm)
  17.  
  18. if apm == "pm" then
  19.     hour = unfHour + 12
  20. elseif apm == "am" then
  21.     if unfHour < 10 then
  22.         hour = "0"..unfHour
  23.     end
  24. else
  25.     errorCode = "Couldn't get AM/PM!"
  26. end
  27. -- <strong>
  28. --                                        Thursday, April 04, 2013</strong>
  29. local worldTime = hour..":"..unfMinute
  30. print("Current time: "..worldTime.." on "..worldDate)
  31.  
  32. function writeToFile(fileName)
  33.     local absPath = shell.resolve( fileName )
  34.     local file
  35.     if not fs.exists( absPath ) then
  36.         print("Creating a new time log file with name \""..fileName.."\"!")
  37.         file = fs.open( absPath, "w" )
  38.     else
  39.         file = fs.open( absPath, "a" ) -- w+ aka writeUpdate mode
  40.     end
  41.    
  42.     file.write("Current time: "..worldTime.." on "..worldDate.."\n")
  43.     file.close()
  44. end
  45.  
  46. writeToFile("time.log")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement