This program basically prompts you for your location and displays the current time on your screen if the location is supported.
Currently supported locations:
United States (PST, CST, EST)
Australia (CST, EST, CIT, LHST, NT, WST)
United Kingdom (BST)
China (CST)
Here is the code:
Spoiler
-- Real time clock using HTTP API by PaymentOption --
-- Freeware as long as proper credits are given --
-- Variables --
tTimeLinks = {
["Pacific Standard Time"] = "http://www.timeanddate.com/library/abbreviations/timezones/na/pst.html", -- United States
["Eastern Standard Time"] = "http://www.timeanddate.com/library/abbreviations/timezones/na/est.html", -- United States
["Central Standard Time"] = "http://www.timeanddate.com/library/abbreviations/timezones/na/cst.html", -- United States
["Chinese Standard Time"] = "http://www.timeanddate.com/library/abbreviations/timezones/asia/cst.html", -- China
["British Summer Time"] = "http://www.timeanddate.com/library/abbreviations/timezones/eu/bst.html", -- England, Wales, Scotland, Ireland (United Kingdom)
-- Australia --
["Central Standard Time AU"] = "http://www.timeanddate.com/library/abbreviations/timezones/au/cst.html",
["Christmas Island Time"] = "http://www.timeanddate.com/library/abbreviations/timezones/au/cxt.html",
["Eastern Standard Time AU"] = "http://www.timeanddate.com/library/abbreviations/timezones/au/est.html",
["Lord Howe Standard Time"] = "http://www.timeanddate.com/library/abbreviations/timezones/au/lhst.html",
["Norfolk Time"] = "http://www.timeanddate.com/library/abbreviations/timezones/au/nft.html",
["Western Standard Time"] = "http://www.timeanddate.com/library/abbreviations/timezones/au/wst.html"
}
tLocations = {
[1] = "United States",
[2] = "Australia",
[3] = "United Kingdom",
[4] = "China"
}
tSubLocations = {
tUnitedStates = {
[1] = "Pacific Standard Time",
[2] = "Eastern Standard Time",
[3] = "Central Standard Time"
},
tUnitedKingdom = {
[1] = "British Summer Time"
},
tAustralia = {
[1] = "Central Standard Time AU",
[2] = "Christmas Island TIme",
[3] = "Eastern Standard Time AU",
[4] = "Lord Howe Standard TIme",
[5] = "Norfolk Time",
[6] = "Western Standard Time"
},
tChina = {
[1] = "Chinese Standard Time"
}
}
sArea = "" -- United States, China, United Kingdom, Australia.
nScreenWidth, nScreenHeight = term.getSize()
---------------
-- Input gathering methods --
function GetOption_Number( nOptionList_Size )
term.setCursorBlink( true )
local nStartingPosX, nStartingPosY = term.getCursorPos()
local nInput = 0
local sEvent, sNum = os.pullEvent( "char" )
term.write( sNum )
local function ResetCursor()
term.setCursorPos( nStartingPosX, nStartingPosY )
term.write( string.rep( " ", string.len( tostring( nInput ) ) ) )
term.setCursorPos( nStartingPosX, nStartingPosY )
GetOption_Number( nOptionList_Size )
end
if type( tonumber( sNum ) ) == "string" then
ResetCursor()
else
nInput = tonumber( sNum )
end
if nInput <= 0 or nInput > nOptionList_Size then
ResetCursor()
else
term.setCursorBlink( false )
return nInput
end
end
function GetArea( tLocationTable )
term.setCursorPos( 2, 9 )
term.write( "Pick an option: " )
local nInput = GetOption_Number( #tLocationTable )
sArea = tLocationTable[nInput]
end
-----------------------------
-- GUI piece methods --
function PrintBorder()
term.setCursorPos( 1, 1 )
term.write( "+" .. string.rep( "-", nScreenWidth - 2 ) .. "+" )
for nHeight = 2, nScreenHeight - 1 do
term.setCursorPos( 1, nHeight )
term.write( "|" )
term.setCursorPos( nScreenWidth, nHeight )
term.write( "|" )
end
term.setCursorPos( 1, nScreenHeight )
term.write( "+" .. string.rep( "-", nScreenWidth - 2 ) .. "+" )
end
function PrintLocations( tLocationTable )
for nHeight = 2, #tLocationTable + 1 do
term.setCursorPos( 2, nHeight )
term.write( "[" .. nHeight - 1 .. "] " .. tLocationTable[nHeight - 1] )
end
end
function Clear()
term.clear()
term.setCursorPos( 1, 1 )
end
function PrintCentered( nHeight, sString )
term.setCursorPos( nScreenWidth/2 - string.len( sString )/2, nHeight )
term.write( sString )
end
------------------------
-- Menu methods --
function GlobalLocationSelection_Menu()
Clear()
PrintBorder()
PrintLocations( tLocations )
local nInput = GetArea( tLocations )
end
function SubSelectionMenu_UnitedStates()
Clear()
PrintBorder()
PrintLocations( tSubLocations.tUnitedStates )
local nInput = GetArea( tSubLocations.tUnitedStates )
end
function SubSelectionMenu_Australia()
Clear()
PrintBorder()
PrintLocations( tSubLocations.tAustralia)
local nInput = GetArea( tSubLocations.tAustralia )
end
function SubSelectionMenu_UnitedKingdom()
Clear()
PrintBorder()
PrintLocations( tSubLocations.tUnitedKingdom )
local nInput = GetArea( tSubLocations.tUnitedKingdom )
end
function SubSelectionMenu_China()
Clear()
PrintBorder()
PrintLocations( tSubLocations.tChina )
local nInput = GetArea( tSubLocations.tChina )
end
function DisplayTime()
Clear()
PrintBorder()
PrintCentered( 2, sArea )
term.setCursorPos( 2, nScreenHeight - 1 )
term.write( "[Change]" )
while true do
local sTime = GetTimeFromLine()
PrintCentered( 9, sTime )
os.startTimer( 0.3 )
local sEvent, nKey = os.pullEvent()
if sEvent == "key" and nKey == 28 then
GlobalLocationSelection_Menu()
CheckGlobalArea()
DisplayTime()
end
end
end
------------------
-- Area check methods --
function CheckGlobalArea()
if sArea == "United States" then
SubSelectionMenu_UnitedStates()
elseif sArea == "Australia" then
SubSelectionMenu_Australia()
elseif sArea == "United Kingdom" then
SubSelectionMenu_UnitedKingdom()
elseif sArea == "China" then
SubSelectionMenu_China()
end
end
------------------------
-- HTTP parsing methods --
function GetTimePage()
local sRespone = nil
local sLine = ""
sResponse = http.get( tTimeLinks[sArea] )
if sResponse then
for nLine = 1, 31 do
sLine = sResponse.readLine()
end
sResponse.close()
return true, sLine
else
return false, nil
end
end
function GetTimeFromLine()
local bSuccess, sLine = GetTimePage()
if bSuccess then
if sArea == "Pacific Standard Time" then
_, nTimePos = string.find( sLine, '<span id="ij0">' )
nEndTimePos = string.find( sLine, '</span>', nTimePos )
else
_, nTimePos = string.find( sLine, 'class="big">' )
nEndTimePos = string.find( sLine, '</span>', nTimePos )
end
local sTime = string.sub( sLine, nTimePos + 1, nEndTimePos - 1 )
return sTime
else
return nil
end
end
--------------------------
GlobalLocationSelection_Menu()
CheckGlobalArea()
DisplayTime()
Here is the pastebin link: http://pastebin.com/9Pmz0JXU
Here are some screenshots: