Advertisement
theoriginalbit

CCalendar

Feb 3rd, 2013
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.56 KB | None | 0 0
  1. --[[
  2. Author: TheOriginalBIT
  3. Version: 1.1
  4. Created: 03 Feb 2013
  5. Last Update: 10 Mar 2013
  6.  
  7. License:
  8.  
  9. COPYRIGHT NOTICE
  10. Copyright © 2013 Joshua Asbury a.k.a TheOriginalBIT [theoriginalbit@gmail.com]
  11.  
  12. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  13. associated documentation files (the "Software"), to deal in the Software without restriction,
  14. including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  15. copies of the Software, and to permit persons to whom the Software is furnished to do so,
  16. subject to the following conditions:
  17.  
  18. -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  19. -Visible credit is given to the original author.
  20. -The software is distributed in a non-profit way.
  21.  
  22. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  23. WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  24. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  25. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. ]]--
  27.  
  28. if not os.day then
  29.   print("Sorry but you don't seem to be running the version of ComputerCraft that this program requires (1.48+)")
  30.   return
  31. end
  32.  
  33. local useShorthand = false
  34. local useAmPm = false
  35. local refreshRate = 0.05
  36.  
  37. -- argument override
  38. local argv = {...}
  39.  
  40. local ignoreNext = false
  41. for i = 1, #argv do
  42.   local v = argv[i]
  43.   if v == '-ds' then
  44.     useShorthand = true
  45.   elseif v == '-ts' then
  46.     useAmPm = true
  47.   elseif v == '-rr' then
  48.     refreshRate = tonumber(argv[i+1])
  49.     ignoreNext = true
  50.   elseif not ignoreNext then
  51.     printError("Error: Invalid runtime parameter, "..v)
  52.     error()
  53.   elseif ignoreNext then
  54.     ignoreNext = false
  55.   end
  56. end
  57.  
  58. -- initialise variables
  59. local currentYear = 1
  60. local currentMonth = 1
  61. local currentDay = 1
  62. local isLeapYear = (currentYear % 4 == 0 and (currentYear % 100 > 0 or  currentYear % 400 == 0))
  63. local yearPrefix = "AS (After Spawn)"
  64.  
  65. local monthsTable = {
  66.   {month="January", days=31, shorthand="JAN"},
  67.   {month="February", days=28, shorthand="FEB"},
  68.   {month="March", days=31, shorthand="MAR"},
  69.   {month="April", days=30, shorthand="APR"},
  70.   {month="May", days=31, shorthand="MAY"},
  71.   {month="June", days=30, shorthand="JUNE"},
  72.   {month="July", days=31, shorthand="JULY"},
  73.   {month="August", days=31, shorthand="AUG"},
  74.   {month="September", days=30, shorthand="SEPT"},
  75.   {month="October", days=31, shorthand="OCT"},
  76.   {month="November", days=30, shorthand="NOV"},
  77.   {month="December", days=31, shorthand="DEC"},
  78. }
  79.  
  80. local daysTable = {
  81.   {day="Monday", shorthand="MON"},
  82.   {day="Tuesday", shorthand="TUE"},
  83.   {day="Wednesday", shorthand="WED"},
  84.   {day="Thursday", shorthand="THU"},
  85.   {day="Friday", shorthand="FRI"},
  86.   {day="Saturday", shorthand="SAT"},
  87.   {day="Sunday", shorthand="SUN"},
  88. }
  89.  
  90. local function updateLeapYear(year)
  91.   isLeapYear = (currentYear % 4 == 0 and (currentYear % 100 > 0 or  currentYear % 400 == 0))
  92. end
  93.  
  94. local function updateYear(days)
  95.   currentYear = math.floor(days/(isLeapYear and 366 or 365)+1)
  96.   updateLeapYear(year)
  97. end
  98.  
  99. local function updateMonth(days)
  100.   local shiftedDays = days%366
  101.   local d = 0
  102.   for i = 1, #monthsTable do
  103.     local before = d
  104.     if i == 2 and isLeapYear then
  105.       d = d + 29
  106.     else
  107.       d = d + monthsTable[i].days
  108.     end
  109.     if shiftedDays >= before and shiftedDays <= d then
  110.       currentMonth = i
  111.       return
  112.     end
  113.   end
  114. end
  115.  
  116. local function updateDay(days)
  117.   local shiftedDays = days%366
  118.   for i = 1, currentMonth - 1 do
  119.     shiftedDays = shiftedDays - monthsTable[i].days
  120.   end
  121.   currentDay = shiftedDays
  122. end
  123.  
  124. local function hexToCol(hex)
  125.   local num = tonumber(hex, 16)
  126.   return num ~= nil and 2^num or nil
  127. end
  128.  
  129. local function cwrite(msg, y, offset)
  130.   local sw,sh=term.getSize()
  131.   term.setCursorPos(sw/2-#msg/2, y or sh/2 + offset or 0)
  132.   write(msg)
  133. end
  134.  
  135. while true do
  136.   term.clear()
  137.   local dayCount = os.day()
  138.   updateYear(dayCount)
  139.   updateMonth(dayCount)
  140.   updateDay(dayCount)
  141.   cwrite("The time is: "..textutils.formatTime(os.time(), not useAmPm),nil,-2)
  142.   cwrite("The date is: "..currentDay.." "..(useShorthand and monthsTable[currentMonth].shorthand or monthsTable[currentMonth].month).." "..string.format("%.4d",currentYear),nil,2)
  143.  
  144.   os.startTimer(refreshRate)
  145.   os.pullEvent("timer")
  146. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement