Advertisement
Anonomit

Clock

Jan 30th, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.14 KB | None | 0 0
  1.  
  2. do --[[USAGE]]--
  3. --[[
  4.    
  5.     arg1 : set to 'true' to enable permanent day
  6.     arg2 : set to 'local' to disable RealTime
  7.     arg3 : set to 'true' to enable time messages
  8.    
  9. ]]--
  10. end --[[END USAGE]]--
  11.  
  12.  
  13. --[[CONFIG]]--
  14.  
  15. local nTimeZone = -5
  16. local nTimeFormat = 24
  17.  
  18. --[[END CONFIG]]--
  19.  
  20.  
  21.  
  22. local flipAMPM, split, getTime, formatTime, validateTime, timeServer
  23.  
  24.  
  25. do --[[TIME FUNCTIONS]]--
  26.    
  27.     -------------------------------------------------------------------------------------
  28.     --Time API by Amarite1 - http://t3kbau5.tk/                                        --
  29.     --Purpose: Allows the aquisition and use of real-world time                        --
  30.     --Requirements: HTTP server hosting the time.php script that gives the current time--
  31.     --Notes: The default value of timeServer is just a generic one that I host. Please --
  32.     --       host your own, if possible, to reduce my bandwidth usage and
  33.     -------------------------------------------------------------------------------------
  34.    
  35.     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)
  36.    
  37.    
  38.     -------------------------------------------------------------------------------------
  39.     --flipAMPM                                                                         --
  40.     --Purpose: Changes from AM to PM and vise-versa. Internal ONLY                     --
  41.     --usage: flipAMPM(ampm ['AM' or 'PM'])                                             --
  42.     -------------------------------------------------------------------------------------
  43.    
  44.     function flipAMPM(a)
  45.         if a == "AM" or a == "am" then
  46.             a = "PM"
  47.         elseif a == "PM" or a == "pm" then
  48.             a = "AM"
  49.         else
  50.             return "error"
  51.         end
  52.         return a
  53.     end
  54.    
  55.     function split(str, pat)
  56.        local t = {}
  57.        local fpat = "(.-)" .. pat
  58.        local last_end = 1
  59.        local s, e, cap = str:find(fpat, 1)
  60.        while s do
  61.               if s ~= 1 or cap ~= "" then
  62.       table.insert(t,cap)
  63.               end
  64.               last_end = e+1
  65.               s, e, cap = str:find(fpat, last_end)
  66.        end
  67.        if last_end <= #str then
  68.               cap = str:sub(last_end)
  69.               table.insert(t, cap)
  70.        end
  71.        return t
  72.     end
  73.    
  74.     -------------------------------------------------------------------------------------
  75.     --getTime                                                                          --
  76.     --Purpose: Gets the current real-world time from a server                          --
  77.     --usage: getTime(timezone [ie. -5], format [12/24])                                --
  78.     -------------------------------------------------------------------------------------
  79.    
  80.     function getTime(timezone, frmt)
  81.         local site = http.get(timeServer)
  82.         local timeArray = split(site.readLine(), ":")
  83.         site.close()
  84.         local hours = tonumber(timeArray[1])
  85.         local minutes = tonumber(timeArray[2])
  86.         local seconds = tonumber(timeArray[3])
  87.         local offset = tonumber(timeArray[4])
  88.         local offset = offset/100
  89.        
  90.         local timezone = tonumber(timezone)
  91.         local frmt = tonumber(frmt)
  92.         local utchours = hours - offset
  93.         local hours = utchours + timezone
  94.         local meridiem
  95.        
  96.         if frmt == 12 then
  97.             if hours > 12 and hours < 24 then
  98.                 hours = hours - 12
  99.                 meridiem = 1
  100.             elseif hours > 24 then
  101.                 hours = hours - 24
  102.                 meridiem = 0
  103.             else
  104.                 meridiem = 0
  105.             end
  106.             if hours == 0 then
  107.                 hours = 12
  108.             end
  109.         elseif frmt == 24 then
  110.             --do nothing
  111.         else
  112.             return "Invalid format"
  113.         end
  114.  
  115.         local finalTime = {}
  116.         finalTime[1] = hours
  117.         finalTime[2] = minutes
  118.         finalTime[3] = seconds
  119.         if frmt == 12 then
  120.             if meridiem == 0 then
  121.                 finalTime[4] = "AM"
  122.             else
  123.                 finalTime[4] = "PM"
  124.             end
  125.         end
  126.         return finalTime
  127.     end
  128.    
  129.     -------------------------------------------------------------------------------------
  130.     --formatTime                                                                       --
  131.     --Purpose: Puts time into a formatted string (ie. 9:50:11)                         --
  132.     --usage: formatTime(hours, minutes, seconds, format [12/24], validate [true/false])--
  133.     -------------------------------------------------------------------------------------
  134.    
  135.     function formatTime(h, m, s, f, v, a)
  136.         if v == true then
  137.             local array = validateTime(h, m, s, f, a)
  138.             h = array[1]
  139.             m = array[2]
  140.             s = array[3]
  141.         end
  142.         local formatted = tostring(h) .. ":"
  143.         if m < 10 then
  144.             formatted = formatted .. "0" .. tostring(m) .. ":"
  145.         else
  146.             formatted = formatted .. tostring(m) .. ":"
  147.         end
  148.         if s < 10 then
  149.             formatted = formatted .. "0" .. tostring(s)
  150.         else
  151.             formatted = formatted .. tostring(s)
  152.         end
  153.        
  154.         return formatted
  155.     end
  156.    
  157.     -------------------------------------------------------------------------------------
  158.     --validateTime                                                                     --
  159.     --Purpose: Makes sure that hours, minutes, and seconds are in the correct range    --
  160.     --         and adds the correct AM/PM if in 12 hour format                         --
  161.     --usage: validateTime(hours, minutes, seconds, format [12/24], ampm ['AM' or 'PM'])--
  162.     -------------------------------------------------------------------------------------
  163.    
  164.     function validateTime(h, m, s, f, a) --ensures that time
  165.         if f == 12 and a == nil then
  166.             return "please input am/pm"
  167.         end
  168.         if s > 59 then
  169.             s = s - 60
  170.             m = m + 1
  171.         end
  172.  
  173.         if m > 59 then
  174.             m = m - 60
  175.             h = h + 1
  176.         end
  177.        
  178.         if h > 12 and h < 24 and f == 12 then
  179.             h = h - 12
  180.             a = flipAMPM(a)
  181.         elseif h > 23 then
  182.             h = h - 24
  183.         end
  184.        
  185.         local array = {}
  186.         array[1] = h
  187.         array[2] = m
  188.         array[3] = s
  189.         if f == 12 then
  190.             array[4] = a
  191.         end
  192.         return array
  193.     end
  194.    
  195.    
  196. end --[[END TIME FUCTIONS]]--
  197.  
  198.  
  199.  
  200. local function fColor( _nColor, _sSide )
  201.    
  202.     if _sSide then
  203.         local mon = peripheral.wrap( _sSide )
  204.         if mon.isColor ~= nil then
  205.             if mon.isColor() then
  206.                 mon.setTextColor( _nColor or colors.white )
  207.             end --if mon.isColor()
  208.         end --if mon.isColor ~= nil
  209.     else --_sSide
  210.         if term.isColor ~= nil then
  211.             if term.isColor() then
  212.                 term.setTextColor( _nColor or colors.white )
  213.             end --if term.isColor()
  214.         end --if term.isColor ~= nil
  215.     end --if _sSide
  216.    
  217. end --local function fColor( _nColor, _sSide )
  218.  
  219.  
  220. local tArgs = { ... }
  221.  
  222. local timeArray
  223. local hours
  224. local minutes
  225. local seconds
  226. local ampm
  227. local array
  228.  
  229. term.clear()
  230. term.setCursorPos( 1, 1 )
  231. fColor( colors.lime )
  232. print( "Clock" )
  233. fColor( colors.white )
  234.  
  235. if tArgs[2] == "true" then
  236.     local timeArray = getTime( nTimeZone, nTimeFormat )
  237.     hours = timeArray[1]
  238.     minutes = timeArray[2]
  239.     seconds = timeArray[3]
  240.     ampm = timeArray[4]
  241.    
  242. end --if tArgs[2] == "true"
  243.  
  244. while true do
  245.     if tArgs[2] == "true" then
  246.         if math.random( 1, 1 ) == 1 then
  247.             local timeArray = getTime( nTimeZone, nTimeFormat )
  248.             hours = timeArray[1]
  249.             minutes = timeArray[2]
  250.             seconds = timeArray[3]
  251.             assert( seconds )
  252.             ampm = timeArray[4]
  253.         end --if math.random( 1, 30 ) == 1
  254.         assert( seconds )
  255.         array = validateTime( hours, minutes, seconds, nTimeFormat, ampm )
  256.         hours = array[1]
  257.         minutes = array[2]
  258.         seconds = array[3]
  259.         ampm = array[4]
  260.         if tArgs[3] == "true" then
  261.             if ( minutes == 0 or minutes == 30 ) and seconds == 0 then
  262.                 local timeArray = getTime( nTimeZone, nTimeFormat )
  263.                 hours = timeArray[1]
  264.                 minutes = timeArray[2]
  265.                 seconds = timeArray[3]
  266.                 ampm = timeArray[4]
  267.                 if ( minutes == 0 or minutes == 30 ) and ( seconds == 0 or seconds == 1 ) then
  268.                     for _, side in ipairs( rs.getSides() ) do
  269.                         if peripheral.getType( side ) == "command" then
  270.                             local com = peripheral.wrap( side )
  271.                             if nTimeFormat == 24 then
  272.                                 if hours > 12 or hours < 1 then
  273.                                     com.setCommand( "/say [TIME] The time is " .. formatTime( hours, minutes, seconds, nTimeFormat, true, ampm ) .. " ( " .. formatTime( hours, minutes, seconds, 12, true, ampm .. " " .. ampm .. " )" ) )
  274.                                 else --hours > 12 or hours < 1
  275.                                     com.setCommand( "/say [TIME] The time is " .. formatTime( hours, minutes, seconds, nTimeFormat, true, ampm ) )
  276.                                 end --if hours > 12 or hours < 1
  277.                             elseif nTimeFormat == 12 then
  278.                                 com.setCommand( "/say [TIME] The time is " .. hours .. ":" .. ( minutes < 10 and "0" ) .. minutes .. " " .. ampm )
  279.                             end --if nTimeFormat == 24
  280.                             sleep( 0.1 )
  281.                             com.setCommand( "" )
  282.                             break --for _, side in ipairs( rs.getSides() )
  283.                         end --if peripheral.getType( side ) == "command"
  284.                     end --for _, side in ipairs( rs.getSides() )
  285.                 end --if ( minutes == 0 or minutes == 30 ) and ( seconds == 0 or seconds == 1 )
  286.             end --if ( minutes == 0 or minutes == 30 ) and seconds == 0
  287.         end --if tArgs[3] == "true"
  288.     end --if tArgs[2] == "true"
  289.     for _, side in ipairs( rs.getSides() ) do
  290.         if peripheral.isPresent( side ) then
  291.             if peripheral.getType( side ) == "monitor" then
  292.                 local mon = peripheral.wrap( side )
  293.                 mon.clear()
  294.                 mon.setCursorPos( 1, 1 )
  295.                 fColor( colors.yellow, side )
  296.                 mon.write( "Clock" )
  297.                 if tArgs[2] == "true" then
  298.                     mon.setCursorPos( 1, 2 )
  299.                     fColor( colors.lime, side )
  300.                     mon.write( "Real Time" )
  301.                     mon.setCursorPos( 1, 3 )
  302.                     fColor( colors.white, side )
  303.                     mon.write( formatTime( hours, minutes, seconds, nTimeFormat, true, ampm ) .. ( nTimeFormat == 12 and ( " " .. ampm ) or "" ) )
  304.                 end --if tArgs[2] == "true"
  305.                 mon.setCursorPos( 1, 4 )
  306.                 fColor( colors.lime, side )
  307.                 mon.write( "MC Time" )
  308.                 fColor( colors.white, side )
  309.                 mon.setCursorPos( 1, 5 )
  310.                 mon.write( textutils.formatTime( os.time(), true ) )
  311.             end --if peripheral.getType( side ) == "monitor"
  312.         end --if peripheral.isPresent( side )
  313.     end --for i, v in ipairs( rs.getSides() )
  314.     if tArgs[2] == "true" then
  315.         seconds = seconds + 1
  316.     end --if tArgs[2] == "true"
  317.     if tArgs[1] == "true" and ( os.time() > 18 or os.time() < 6 ) then
  318.         for _, side in ipairs( rs.getSides() ) do
  319.             if peripheral.getType( side ) == "command" then
  320.                 local com = peripheral.wrap( side )
  321.                 com.setCommand( "/time set day" )
  322.                 sleep( 0.1 )
  323.                 com.setCommand( "" )
  324.                 break --for _, side in ipairs( rs.getSides() )
  325.             end --if peripheral.getType( side ) == "command"
  326.         end --for _, side in ipairs( rs.getSides() )
  327.     end --if tArgs[1] == "true" and ( os.time() > 18 or os.time() < 6 )
  328.     sleep( 1 )
  329. end --while true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement