This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Grim Reaper's profile picture

Real Time

Started by Grim Reaper, 17 September 2012 - 12:55 AM
Grim Reaper #1
Posted 17 September 2012 - 02:55 AM
*HTTP API needs to be enabled!*

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:
Spoilerhttp://imgur.com/7TOZR
cant_delete_account #2
Posted 17 September 2012 - 03:51 AM
Ugh, timeandate doesn't even have an API, why not just use timeapi?:

print("Time Zone (E.G: PST):")
print(http.get("http://timeapi.org/"..read().."/now").readAll():sub(12,16))
Two lines of code.
Grim Reaper #3
Posted 17 September 2012 - 04:34 AM
Because I wanted to do it in my own way. If everyone were to only write things that were %100 original on this site then there would be about 20 posts. Nothing more. It's just a different way of approaching a problem.

You don't have to use it. I just wanted to release it because I was proud of it, no reason more.

Also, I wasn't aware that there was such a thing, so thanks for the information.

Plus, there is still a pretty big possibility for error in the two lines you wrote, but still, it's nothing that can't be fixed with minimal effort.
Strange, I don't see you posting in everyone's "door lock" topics that their solution is entirely superfluous.
Cranium #4
Posted 17 September 2012 - 06:24 PM
What, no love for Mountain time in the US?
ETHANATOR360 #5
Posted 17 September 2012 - 10:21 PM
Real time clock using HTTP API by PaymentOption
Freeware as long as proper credits are given

Because I wanted to do it in my own way. If everyone were to only write things that were %100 original on this site then there would be about 20 posts. Nothing more. It's just a different way of approaching a problem.

You don't have to use it. I just wanted to release it because I was proud of it, no reason more.

Also, I wasn't aware that there was such a thing, so thanks for the information.

Plus, there is still a pretty big possibility for error in the two lines you wrote, but still, it's nothing that can't be fixed with minimal effort.
Strange, I don't see you posting in everyone's "door lock" topics that their solution is entirely superfluous.
did you want to do it your own way? or did payment option want to do it his way?
MysticT #6
Posted 17 September 2012 - 10:21 PM
Real time clock using HTTP API by PaymentOption
Freeware as long as proper credits are given

Because I wanted to do it in my own way. If everyone were to only write things that were %100 original on this site then there would be about 20 posts. Nothing more. It's just a different way of approaching a problem.

You don't have to use it. I just wanted to release it because I was proud of it, no reason more.

Also, I wasn't aware that there was such a thing, so thanks for the information.

Plus, there is still a pretty big possibility for error in the two lines you wrote, but still, it's nothing that can't be fixed with minimal effort.
Strange, I don't see you posting in everyone's "door lock" topics that their solution is entirely superfluous.
did you want to do it your own way? or did payment option want to do it his way?
He is PaymentOption, just that he changed his forum name.
ETHANATOR360 #7
Posted 17 September 2012 - 10:36 PM
oh,ok
SimonPSeville #8
Posted 29 July 2014 - 07:52 AM
I'm getting an error where it says "clock2:242 attempt to perform arithmetic __add on nil and number"