Advertisement
billysback

digclock

Oct 17th, 2012
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.67 KB | None | 0 0
  1.  
  2. local function split(str, pat)
  3.     local t = {}  -- NOTE: use {n = 0} in Lua-5.0
  4.     if str ~= nil then
  5.        local fpat = "(.-)" .. pat
  6.        local last_end = 1
  7.        local s, e, cap = str:find(fpat, 1)
  8.        while s do
  9.           if s ~= 1 or cap ~= "" then
  10.          table.insert(t,cap)
  11.           end
  12.           last_end = e+1
  13.           s, e, cap = str:find(fpat, last_end)
  14.        end
  15.        if last_end <= #str then
  16.           cap = str:sub(last_end)
  17.           table.insert(t, cap)
  18.        end
  19.     else
  20.         print("##CC CHANNELS ERROR: FAILED TO SPLIT by:"..pat)
  21.     end
  22.     return t
  23. end
  24.  
  25. function time(offset) -- Get the time, offset to set the time off by bla bla hours
  26.       if not offset then error("Please enter a number to offset the time by. Use 0 if you dont want to offset it") end
  27.       local Days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
  28.       local Months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
  29.       local DaysInMonths = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
  30.       local res = http.get("http://nist.time.gov/timezone.cgi?UTC/s/0/")
  31.       local Res = {}
  32.       local DateFormat = ""
  33.       local bla = ""
  34.       local Date = ""
  35.       local time = ""
  36.       local BlaDate = ""
  37.       local BlaTime = ""
  38.       local BlaDays = ""
  39.       local BlaMonth = ""
  40.       if res then
  41.             while true do
  42.               bla = res.readLine()
  43.               if bla then
  44.                     Res[#Res+1] = bla
  45.               else
  46.                     break
  47.               end
  48.             end
  49.             res.close()
  50.             Time = string.sub(Res[49], 63, string.len(Res[49])-4)
  51.             Date = string.sub(Res[50], 53, string.len(Res[50])-4)
  52.             DateFormat = {Time}
  53.             for n=1,3 do
  54.               if string.find(Date, ", ") then
  55.                     local s,len = string.find(Date, "%, ")
  56.                     DateFormat[#DateFormat+1] = string.gsub(string.sub(Date, 1, s), "%,", "")
  57.                     Date = string.sub(Date, len+1, string.len(Date))
  58.               end
  59.             end
  60.             DateFormat[#DateFormat+1] = Date
  61.             if string.sub(DateFormat[3], string.len(DateFormat[3])-2, string.len(DateFormat[3])-2) == " " then
  62.               BlaDate = tonumber(string.sub(DateFormat[3], string.len(DateFormat[3])-1, string.len(DateFormat[3])))
  63.             else
  64.               BlaDate = tonumber(string.sub(DateFormat[3], string.len(DateFormat[3]), string.len(DateFormat[3])))
  65.             end
  66.             DateFormat[3] = string.gsub(DateFormat[3], BlaDate, "")
  67.             BlaTime = tonumber(string.sub(DateFormat[1], 1, 2))+tonumber(offset)
  68.             repeat
  69.               if BlaTime > 24 then
  70.                     BlaDate = BlaDate+1
  71.                     BlaTime = BlaTime-24
  72.               elseif BlaTime < 0 then
  73.                     BlaDate = BlaDate-1
  74.                     BlaTime = BlaTime+24
  75.               end
  76.             until BlaTime > 0 and BlaTime < 24
  77.             if string.len(BlaTime) == 1 then
  78.               BlaTime = "0"..BlaTime
  79.             end
  80.       end
  81.       DateFormat[4] = tonumber(DateFormat[4])
  82.       BlaMonth = string.gsub(DateFormat[3], " ", "")
  83.       TheDays = BlaDate
  84.       o = 1
  85.       repeat
  86.             if Months[o] == BlaMonth then
  87.               BlaDate = TheDays+BlaDate
  88.             else
  89.               BlaDate = BlaDate+DaysInMonths[o]
  90.             end
  91.             o = o+1
  92.       until Months[o] == BlaMonth
  93.       repeat
  94.             if BlaDate > 365 then
  95.               BlaDate = BlaDate-365
  96.               DateFormat[4] = DateFormat[4]+1
  97.             end
  98.       until BlaDate < 366
  99.       local o = 1
  100.       local TempNo = BlaDate
  101.       repeat -- Turn this into a 7 day
  102.             TempNo = TempNo-7
  103.       until TempNo < 8
  104.       DateFormat[2] = Days[TempNo]
  105.       repeat -- Get it into a format for the proper month
  106.             BlaDate = BlaDate-DaysInMonths[o]
  107.             o = o+1
  108.       until BlaDate <= DaysInMonths[o]
  109.       BlaMonth = Months[o]
  110.       DateFormat[3] = BlaMonth
  111.       DateFormat[1] = BlaTime..string.sub(DateFormat[1], 3, string.len(DateFormat[1]))
  112.       DateFormat[5] = BlaDate.."/"..o.."/"..DateFormat[4]
  113.       table.insert(DateFormat, 3, BlaDate)
  114.       return unpack(DateFormat)
  115. end
  116.  
  117. local zero =
  118. {" _____ ",
  119.  "|  _  |",
  120.  "| |/' |",
  121.  "|  /| |",
  122.  "\\ |_/ /",
  123.  " \\___/ "}
  124.  
  125.  local one =
  126. {" __  ",
  127.  "/  | ",
  128.  "-| | ",
  129.  " | | ",
  130.  "_| |_",
  131.  "\\___/"}
  132.  
  133.  local two =
  134. {" _____ ",
  135.  "/ __  \\",
  136.  "-' / /'",
  137.  "  / /  ",
  138.  "./ /___",
  139.  "\\_____/"}
  140.  
  141.  local three =
  142. {" _____ ",
  143.  "|____ |",
  144.  "    / /",
  145.  "    \\ \\",
  146.  ".___/ /",
  147.  "\\____/ "}
  148.  
  149.  local four =
  150. {"   ___ ",
  151.  "  /   |",
  152.  " / /| |",
  153.  "/ /_| |",
  154.  "\\___  |",
  155.  "    |_/"}
  156.  
  157.  local five =
  158. {" _____ ",
  159.  "|  ___|",
  160.  "|___ \\ ",
  161.  "    \\ \\",
  162.  "/\\__/ /",
  163.  "\\____/ "}
  164.  
  165.  local six =
  166. {"  ____ ",
  167.  " / ___|",
  168.  "/ /___ ",
  169.  "| ___ \\",
  170.  "| \\_/ |",
  171.  "\\_____/"}
  172.  
  173.  local seven =
  174. {" ______",
  175.  "|___  /",
  176.  "   / / ",
  177.  "  / /  ",
  178.  "./ /   ",
  179.  "\\_/    "}
  180.  
  181.  local eight =
  182. {" _____ ",
  183.  "|  _  |",
  184.  " \\ V / ",
  185.  " / _ \\ ",
  186.  "| |_| |",
  187.  "\\_____/"}
  188.  
  189.  local nine =
  190. {" _____ ",
  191.  "|  _  |",
  192.  "| |_| |",
  193.  "\\____ |",
  194.  ".___/ /",
  195.  "\\____/ "}
  196.  
  197.  local sep =
  198. {" _ ",
  199.  "(_)",
  200.  "   ",
  201.  " _ ",
  202.  "(_)",
  203.  "   "}
  204.  
  205.  local function join(lines, str)
  206.     local table = zero
  207.     if str == "0" then table = zero
  208.     elseif str == "1" then table = one
  209.     elseif str == "2" then table = two
  210.     elseif str == "3" then table = three
  211.     elseif str == "4" then table = four
  212.     elseif str == "5" then table = five
  213.     elseif str == "6" then table = six
  214.     elseif str == "7" then table = seven
  215.     elseif str == "8" then table = eight
  216.     elseif str == "9" then table = nine
  217.     elseif str == ":" then table = sep end
  218.     for i,v in ipairs(table) do
  219.         if lines[i] ~= nil then
  220.             lines[i] = lines[i].." "..v
  221.         end
  222.     end
  223.  end
  224.  
  225.  local function drawTime(hour, minute, sep)
  226.     term.clear();
  227.     term.setCursorPos(1,1);
  228.     print("");
  229.     local h = ""
  230.     local m = ""
  231.     if hour < 10 then
  232.         h = "0"..hour
  233.     else
  234.         h = tostring(hour)
  235.     end
  236.     if minute < 10 then
  237.         m = "0"..minute
  238.     else
  239.         m = tostring(minute)
  240.     end
  241.     local hm = ""
  242.     if sep then hm = h..":"..m
  243.     else hm = h.." "..m end
  244.     local lines = {"", "", "", "", "", ""}
  245.     for i=1,string.len(hm),1 do
  246.         local cur = string.sub(hm, i, i);
  247.         join(lines, cur)
  248.     end
  249.     for i,v in ipairs(lines) do
  250.         print(": "..v.."   :");
  251.     end
  252.  end
  253.  
  254.  local function getInput()
  255.     input = io.read()
  256.     while input == nil or input == "" or input == " " do
  257.         input = io.read()
  258.     end
  259.     return input
  260. end
  261.  
  262. print("Please state your UTC offset: ")
  263. local offset = tonumber(getInput())
  264.  
  265. local on = true
  266. local timer = os.startTimer(0)
  267.  
  268. local interval = 1
  269.  
  270. local ticks = 0
  271. local flash = true
  272.  
  273. while on do
  274.         local event, p1, p2 = os.pullEvent()
  275.         if event == "timer" and p1 == timer then
  276.                 timer = os.startTimer(interval)
  277.                 local time,Day,DayNo, Month,Year,FormattedDate = time(offset)
  278.                 local splt = split(time, ":")
  279.                 ticks = ticks + 1
  280.                 if ticks > 1 then
  281.                     ticks = 0
  282.                     if flash then flash = true else flash = false end
  283.                 end
  284.                 drawTime(tonumber(splt[1]), tonumber(splt[2]), flash)
  285.         elseif event == "key" then
  286.             local key = p1
  287.             if key == 1 then
  288.                 on = false
  289.             end
  290.         end
  291. end
  292.  
  293. term.clear()
  294. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement