Advertisement
PaymentOption

Time

Sep 16th, 2012
695
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.49 KB | None | 0 0
  1. -- Real time clock using HTTP API by PaymentOption --
  2. -- Freeware as long as proper credits are given --
  3.  
  4. -- Variables --
  5. tTimeLinks = {
  6. ["Pacific Standard Time"] = "http://www.timeanddate.com/library/abbreviations/timezones/na/pst.html", -- United States
  7. ["Eastern Standard Time"] = "http://www.timeanddate.com/library/abbreviations/timezones/na/est.html", -- United States
  8. ["Central Standard Time"] = "http://www.timeanddate.com/library/abbreviations/timezones/na/cst.html", -- United States
  9. ["Chinese Standard Time"] = "http://www.timeanddate.com/library/abbreviations/timezones/asia/cst.html", -- China
  10. ["British Summer Time"] = "http://www.timeanddate.com/library/abbreviations/timezones/eu/bst.html", -- England, Wales, Scotland, Ireland (United Kingdom)
  11. -- Australia --
  12. ["Central Standard Time AU"] = "http://www.timeanddate.com/library/abbreviations/timezones/au/cst.html",
  13. ["Christmas Island Time"] = "http://www.timeanddate.com/library/abbreviations/timezones/au/cxt.html",
  14. ["Eastern Standard Time AU"] = "http://www.timeanddate.com/library/abbreviations/timezones/au/est.html",
  15. ["Lord Howe Standard Time"] = "http://www.timeanddate.com/library/abbreviations/timezones/au/lhst.html",
  16. ["Norfolk Time"] = "http://www.timeanddate.com/library/abbreviations/timezones/au/nft.html",
  17. ["Western Standard Time"] = "http://www.timeanddate.com/library/abbreviations/timezones/au/wst.html"
  18. }
  19.  
  20. tLocations = {
  21. [1] = "United States",
  22. [2] = "Australia",
  23. [3] = "United Kingdom",
  24. [4] = "China"
  25. }
  26.  
  27. tSubLocations = {
  28.     tUnitedStates = {
  29.         [1] = "Pacific Standard Time",
  30.         [2] = "Eastern Standard Time",
  31.         [3] = "Central Standard Time"
  32.     },
  33.     tUnitedKingdom = {
  34.         [1] = "British Summer Time"
  35.     },
  36.     tAustralia = {
  37.         [1] = "Central Standard Time AU",
  38.         [2] = "Christmas Island TIme",
  39.         [3] = "Eastern Standard Time AU",
  40.         [4] = "Lord Howe Standard TIme",
  41.         [5] = "Norfolk Time",
  42.         [6] = "Western Standard Time"
  43.     },
  44.     tChina = {
  45.         [1] = "Chinese Standard Time"
  46.     }
  47. }
  48.  
  49. sArea = "" -- United States, China, United Kingdom, Australia.
  50.  
  51. nScreenWidth, nScreenHeight = term.getSize()
  52. ---------------
  53.  
  54. -- Input gathering methods --
  55. function GetOption_Number( nOptionList_Size )
  56.     term.setCursorBlink( true )
  57.    
  58.     local nStartingPosX, nStartingPosY = term.getCursorPos()
  59.     local nInput = 0
  60.    
  61.     local sEvent, sNum = os.pullEvent( "char" )
  62.     term.write( sNum )
  63.    
  64.     local function ResetCursor()
  65.         term.setCursorPos( nStartingPosX, nStartingPosY )
  66.         term.write( string.rep( " ", string.len( tostring( nInput ) ) ) )
  67.        
  68.         term.setCursorPos( nStartingPosX, nStartingPosY )
  69.         GetOption_Number( nOptionList_Size )
  70.     end
  71.    
  72.     if type( tonumber( sNum ) ) == "string" then
  73.         ResetCursor()
  74.     else
  75.         nInput = tonumber( sNum )
  76.     end
  77.    
  78.     if nInput then
  79.         if nInput <= 0 or nInput > nOptionList_Size then
  80.             ResetCursor()
  81.         else
  82.             term.setCursorBlink( false )
  83.             return nInput
  84.         end
  85.     end
  86. end
  87.  
  88. function GetArea( tLocationTable )
  89.     term.setCursorPos( 2, 9 )
  90.     term.write( "Pick an option: " )
  91.    
  92.     local nInput = GetOption_Number( #tLocationTable )
  93.     sArea = tLocationTable[nInput]
  94. end
  95. -----------------------------
  96.  
  97. -- GUI piece methods --
  98. function PrintBorder()
  99.     term.setCursorPos( 1, 1 )
  100.     term.write( "+" .. string.rep( "-", nScreenWidth - 2 ) .. "+" )
  101.    
  102.     for nHeight = 2, nScreenHeight - 1 do
  103.         term.setCursorPos( 1, nHeight )
  104.         term.write( "|" )
  105.        
  106.         term.setCursorPos( nScreenWidth, nHeight )
  107.         term.write( "|" )
  108.     end
  109.    
  110.     term.setCursorPos( 1, nScreenHeight )
  111.     term.write( "+" .. string.rep( "-", nScreenWidth - 2 ) .. "+" )
  112. end
  113.  
  114. function PrintLocations( tLocationTable )
  115.     for nHeight = 2, #tLocationTable + 1 do
  116.         term.setCursorPos( 2, nHeight )
  117.         term.write( "[" .. nHeight - 1 .. "] " .. tLocationTable[nHeight - 1] )
  118.     end
  119. end
  120.  
  121. function Clear()
  122.     term.clear()
  123.     term.setCursorPos( 1, 1 )
  124. end
  125.  
  126. function PrintCentered( nHeight, sString )
  127.     term.setCursorPos( nScreenWidth/2 - string.len( sString )/2, nHeight )
  128.     term.write( sString )
  129. end
  130. ------------------------
  131.  
  132. -- Menu methods --
  133. function GlobalLocationSelection_Menu()
  134.     Clear()
  135.     PrintBorder()
  136.     PrintLocations( tLocations )
  137.    
  138.     local nInput = GetArea( tLocations )
  139. end
  140.  
  141. function SubSelectionMenu_UnitedStates()
  142.     Clear()
  143.     PrintBorder()
  144.     PrintLocations( tSubLocations.tUnitedStates )
  145.    
  146.     local nInput = GetArea( tSubLocations.tUnitedStates )
  147. end
  148.  
  149. function SubSelectionMenu_Australia()
  150.     Clear()
  151.     PrintBorder()
  152.     PrintLocations( tSubLocations.tAustralia)
  153.    
  154.     local nInput = GetArea( tSubLocations.tAustralia )
  155. end
  156.  
  157. function SubSelectionMenu_UnitedKingdom()
  158.     Clear()
  159.     PrintBorder()
  160.     PrintLocations( tSubLocations.tUnitedKingdom )
  161.    
  162.     local nInput = GetArea( tSubLocations.tUnitedKingdom )
  163. end
  164.  
  165. function SubSelectionMenu_China()
  166.     Clear()
  167.     PrintBorder()
  168.     PrintLocations( tSubLocations.tChina )
  169.    
  170.     local nInput = GetArea( tSubLocations.tChina )
  171. end
  172.  
  173. function DisplayTime()
  174.     Clear()
  175.     PrintBorder()
  176.     PrintCentered( 2, sArea )
  177.    
  178.     term.setCursorPos( 2, nScreenHeight - 1 )
  179.     term.write( "[Change]" )
  180.    
  181.     while true do
  182.         local sTime = GetTimeFromLine()
  183.         PrintCentered( 9, sTime )
  184.        
  185.         os.startTimer( 0.3 )
  186.        
  187.         local sEvent, nKey = os.pullEvent()
  188.         if sEvent == "key" and nKey == 28 then
  189.             GlobalLocationSelection_Menu()
  190.             CheckGlobalArea()
  191.             DisplayTime()
  192.         end
  193.     end
  194. end
  195. ------------------
  196.  
  197. -- Area check methods --
  198. function CheckGlobalArea()
  199.     if sArea == "United States" then
  200.         SubSelectionMenu_UnitedStates()
  201.     elseif sArea == "Australia" then
  202.         SubSelectionMenu_Australia()
  203.     elseif sArea == "United Kingdom" then
  204.         SubSelectionMenu_UnitedKingdom()
  205.     elseif sArea == "China" then
  206.         SubSelectionMenu_China()
  207.     end
  208. end
  209. ------------------------
  210.  
  211. -- HTTP parsing methods --
  212. function GetTimePage()
  213.     local sRespone = nil
  214.     local sLine = ""
  215.    
  216.     sResponse = http.get( tTimeLinks[sArea] )
  217.    
  218.     if sResponse then
  219.         for nLine = 1, 31 do
  220.             sLine = sResponse.readLine()
  221.         end
  222.         sResponse.close()
  223.        
  224.         return true, sLine
  225.     else
  226.         return false, nil
  227.     end
  228. end
  229.  
  230. function GetTimeFromLine()
  231.     local bSuccess, sLine = GetTimePage()
  232.    
  233.     if bSuccess then
  234.         if sArea == "Pacific Standard Time" then
  235.             _, nTimePos = string.find( sLine, '<span id="ij0">' )
  236.             nEndTimePos = string.find( sLine, '</span>', nTimePos )
  237.         else
  238.             _, nTimePos = string.find( sLine, 'class="big">' )
  239.             nEndTimePos = string.find( sLine, '</span>', nTimePos )
  240.         end
  241.        
  242.         local sTime = string.sub( sLine, nTimePos + 1, nEndTimePos - 1 )
  243.         return sTime
  244.     else
  245.         return nil
  246.     end
  247. end
  248. --------------------------
  249.  
  250. GlobalLocationSelection_Menu()
  251. CheckGlobalArea()
  252. DisplayTime()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement