Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- do --[[USAGE]]--
- --[[
- arg1 : set to 'true' to enable permanent day
- arg2 : set to 'local' to disable RealTime
- arg3 : set to 'true' to enable time messages
- ]]--
- end --[[END USAGE]]--
- --[[CONFIG]]--
- local nTimeZone = -5
- local nTimeFormat = 24
- --[[END CONFIG]]--
- local flipAMPM, split, getTime, formatTime, validateTime, timeServer
- do --[[TIME FUNCTIONS]]--
- -------------------------------------------------------------------------------------
- --Time API by Amarite1 - http://t3kbau5.tk/ --
- --Purpose: Allows the aquisition and use of real-world time --
- --Requirements: HTTP server hosting the time.php script that gives the current time--
- --Notes: The default value of timeServer is just a generic one that I host. Please --
- -- host your own, if possible, to reduce my bandwidth usage and
- -------------------------------------------------------------------------------------
- timeServer = "http://t3kbau5.tk/stuff/time.php" --change this to wherever the PHP script is saved (must be on a http server that supports PHP)
- -------------------------------------------------------------------------------------
- --flipAMPM --
- --Purpose: Changes from AM to PM and vise-versa. Internal ONLY --
- --usage: flipAMPM(ampm ['AM' or 'PM']) --
- -------------------------------------------------------------------------------------
- function flipAMPM(a)
- if a == "AM" or a == "am" then
- a = "PM"
- elseif a == "PM" or a == "pm" then
- a = "AM"
- else
- return "error"
- end
- return a
- end
- function split(str, pat)
- local t = {}
- local fpat = "(.-)" .. pat
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- return t
- end
- -------------------------------------------------------------------------------------
- --getTime --
- --Purpose: Gets the current real-world time from a server --
- --usage: getTime(timezone [ie. -5], format [12/24]) --
- -------------------------------------------------------------------------------------
- function getTime(timezone, frmt)
- local site = http.get(timeServer)
- local timeArray = split(site.readLine(), ":")
- site.close()
- local hours = tonumber(timeArray[1])
- local minutes = tonumber(timeArray[2])
- local seconds = tonumber(timeArray[3])
- local offset = tonumber(timeArray[4])
- local offset = offset/100
- local timezone = tonumber(timezone)
- local frmt = tonumber(frmt)
- local utchours = hours - offset
- local hours = utchours + timezone
- local meridiem
- if frmt == 12 then
- if hours > 12 and hours < 24 then
- hours = hours - 12
- meridiem = 1
- elseif hours > 24 then
- hours = hours - 24
- meridiem = 0
- else
- meridiem = 0
- end
- if hours == 0 then
- hours = 12
- end
- elseif frmt == 24 then
- --do nothing
- else
- return "Invalid format"
- end
- local finalTime = {}
- finalTime[1] = hours
- finalTime[2] = minutes
- finalTime[3] = seconds
- if frmt == 12 then
- if meridiem == 0 then
- finalTime[4] = "AM"
- else
- finalTime[4] = "PM"
- end
- end
- return finalTime
- end
- -------------------------------------------------------------------------------------
- --formatTime --
- --Purpose: Puts time into a formatted string (ie. 9:50:11) --
- --usage: formatTime(hours, minutes, seconds, format [12/24], validate [true/false])--
- -------------------------------------------------------------------------------------
- function formatTime(h, m, s, f, v, a)
- if v == true then
- local array = validateTime(h, m, s, f, a)
- h = array[1]
- m = array[2]
- s = array[3]
- end
- local formatted = tostring(h) .. ":"
- if m < 10 then
- formatted = formatted .. "0" .. tostring(m) .. ":"
- else
- formatted = formatted .. tostring(m) .. ":"
- end
- if s < 10 then
- formatted = formatted .. "0" .. tostring(s)
- else
- formatted = formatted .. tostring(s)
- end
- return formatted
- end
- -------------------------------------------------------------------------------------
- --validateTime --
- --Purpose: Makes sure that hours, minutes, and seconds are in the correct range --
- -- and adds the correct AM/PM if in 12 hour format --
- --usage: validateTime(hours, minutes, seconds, format [12/24], ampm ['AM' or 'PM'])--
- -------------------------------------------------------------------------------------
- function validateTime(h, m, s, f, a) --ensures that time
- if f == 12 and a == nil then
- return "please input am/pm"
- end
- if s > 59 then
- s = s - 60
- m = m + 1
- end
- if m > 59 then
- m = m - 60
- h = h + 1
- end
- if h > 12 and h < 24 and f == 12 then
- h = h - 12
- a = flipAMPM(a)
- elseif h > 23 then
- h = h - 24
- end
- local array = {}
- array[1] = h
- array[2] = m
- array[3] = s
- if f == 12 then
- array[4] = a
- end
- return array
- end
- end --[[END TIME FUCTIONS]]--
- local function fColor( _nColor, _sSide )
- if _sSide then
- local mon = peripheral.wrap( _sSide )
- if mon.isColor ~= nil then
- if mon.isColor() then
- mon.setTextColor( _nColor or colors.white )
- end --if mon.isColor()
- end --if mon.isColor ~= nil
- else --_sSide
- if term.isColor ~= nil then
- if term.isColor() then
- term.setTextColor( _nColor or colors.white )
- end --if term.isColor()
- end --if term.isColor ~= nil
- end --if _sSide
- end --local function fColor( _nColor, _sSide )
- local tArgs = { ... }
- local timeArray
- local hours
- local minutes
- local seconds
- local ampm
- local array
- term.clear()
- term.setCursorPos( 1, 1 )
- fColor( colors.lime )
- print( "Clock" )
- fColor( colors.white )
- if tArgs[2] == "true" then
- local timeArray = getTime( nTimeZone, nTimeFormat )
- hours = timeArray[1]
- minutes = timeArray[2]
- seconds = timeArray[3]
- ampm = timeArray[4]
- end --if tArgs[2] == "true"
- while true do
- if tArgs[2] == "true" then
- if math.random( 1, 1 ) == 1 then
- local timeArray = getTime( nTimeZone, nTimeFormat )
- hours = timeArray[1]
- minutes = timeArray[2]
- seconds = timeArray[3]
- assert( seconds )
- ampm = timeArray[4]
- end --if math.random( 1, 30 ) == 1
- assert( seconds )
- array = validateTime( hours, minutes, seconds, nTimeFormat, ampm )
- hours = array[1]
- minutes = array[2]
- seconds = array[3]
- ampm = array[4]
- if tArgs[3] == "true" then
- if ( minutes == 0 or minutes == 30 ) and seconds == 0 then
- local timeArray = getTime( nTimeZone, nTimeFormat )
- hours = timeArray[1]
- minutes = timeArray[2]
- seconds = timeArray[3]
- ampm = timeArray[4]
- if ( minutes == 0 or minutes == 30 ) and ( seconds == 0 or seconds == 1 ) then
- for _, side in ipairs( rs.getSides() ) do
- if peripheral.getType( side ) == "command" then
- local com = peripheral.wrap( side )
- if nTimeFormat == 24 then
- if hours > 12 or hours < 1 then
- com.setCommand( "/say [TIME] The time is " .. formatTime( hours, minutes, seconds, nTimeFormat, true, ampm ) .. " ( " .. formatTime( hours, minutes, seconds, 12, true, ampm .. " " .. ampm .. " )" ) )
- else --hours > 12 or hours < 1
- com.setCommand( "/say [TIME] The time is " .. formatTime( hours, minutes, seconds, nTimeFormat, true, ampm ) )
- end --if hours > 12 or hours < 1
- elseif nTimeFormat == 12 then
- com.setCommand( "/say [TIME] The time is " .. hours .. ":" .. ( minutes < 10 and "0" ) .. minutes .. " " .. ampm )
- end --if nTimeFormat == 24
- sleep( 0.1 )
- com.setCommand( "" )
- break --for _, side in ipairs( rs.getSides() )
- end --if peripheral.getType( side ) == "command"
- end --for _, side in ipairs( rs.getSides() )
- end --if ( minutes == 0 or minutes == 30 ) and ( seconds == 0 or seconds == 1 )
- end --if ( minutes == 0 or minutes == 30 ) and seconds == 0
- end --if tArgs[3] == "true"
- end --if tArgs[2] == "true"
- for _, side in ipairs( rs.getSides() ) do
- if peripheral.isPresent( side ) then
- if peripheral.getType( side ) == "monitor" then
- local mon = peripheral.wrap( side )
- mon.clear()
- mon.setCursorPos( 1, 1 )
- fColor( colors.yellow, side )
- mon.write( "Clock" )
- if tArgs[2] == "true" then
- mon.setCursorPos( 1, 2 )
- fColor( colors.lime, side )
- mon.write( "Real Time" )
- mon.setCursorPos( 1, 3 )
- fColor( colors.white, side )
- mon.write( formatTime( hours, minutes, seconds, nTimeFormat, true, ampm ) .. ( nTimeFormat == 12 and ( " " .. ampm ) or "" ) )
- end --if tArgs[2] == "true"
- mon.setCursorPos( 1, 4 )
- fColor( colors.lime, side )
- mon.write( "MC Time" )
- fColor( colors.white, side )
- mon.setCursorPos( 1, 5 )
- mon.write( textutils.formatTime( os.time(), true ) )
- end --if peripheral.getType( side ) == "monitor"
- end --if peripheral.isPresent( side )
- end --for i, v in ipairs( rs.getSides() )
- if tArgs[2] == "true" then
- seconds = seconds + 1
- end --if tArgs[2] == "true"
- if tArgs[1] == "true" and ( os.time() > 18 or os.time() < 6 ) then
- for _, side in ipairs( rs.getSides() ) do
- if peripheral.getType( side ) == "command" then
- local com = peripheral.wrap( side )
- com.setCommand( "/time set day" )
- sleep( 0.1 )
- com.setCommand( "" )
- break --for _, side in ipairs( rs.getSides() )
- end --if peripheral.getType( side ) == "command"
- end --for _, side in ipairs( rs.getSides() )
- end --if tArgs[1] == "true" and ( os.time() > 18 or os.time() < 6 )
- sleep( 1 )
- end --while true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement