Lua: Calendar

Page 1 of 1
3 posts
Lua: Calendar

Nikolay
MMEX Developer

Posts: 2270
Joined: Sat Dec 06, 2008 8:27 am
Location: Sankt-Petersburg, Russia
Code: Select all
-- returns the day of week integer and the name of the week
-- Compatible with Lua 5.0 and 5.1.
-- from sam_lie
function get_day_of_week(dd, mm, yy)
  local days = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }

  local mmx = mm

  if (mm == 1) then  mmx = 13; yy = yy-1  end
  if (mm == 2) then  mmx = 14; yy = yy-1  end

  local val8 = dd + (mmx*2) +  math.floor(((mmx+1)*3)/5)   + yy + math.floor(yy/4)  - math.floor(yy/100)  + math.floor(yy/400) + 2
  local val9 = math.floor(val8/7)
  local dw = val8-(val9*7)

  if (dw == 0) then
    dw = 7
  end

  return dw, days[dw]
end

-- alternate version - returns the day of week integer and the name of the week
-- Compatible with Lua 5.0 and 5.1.
-- from http://richard.warburton.it
function get_day_of_week(dd, mm, yy)
  dw=os.date('*t',os.time{year=yy,month=mm,day=dd})['wday']
  return dw,({"Sun","Mon","Tue","Wed","Thu","Fri","Sat" })[dw]
end

-- given a string date of '2006-12-31' breaks it down to integer yy, mm and dd
-- Compatible with Lua 5.0 and 5.1.
-- from sam_lie
function get_date_parts(date_str)
  local iyy, imm, idd

  if (date_str) then
    x = string.gsub(date_str, "(%d+)-(%d+)-(%d+)", function (yy,mm,dd)   
        iyy = tonumber(yy)
        imm = tonumber(mm)
        idd = tonumber(dd)
      end)
  end

  return iyy, imm, idd
end

-- alternate version - given a string date of '2006-12-31' breaks it down to integer yy, mm and dd
-- Compatible with Lua 5.0 and 5.1.
-- from http://richard.warburton.it
function get_date_parts(date_str)
  _,_,y,m,d=string.find(date_str, "(%d+)-(%d+)-(%d+)")
  return tonumber(y),tonumber(m),tonumber(d)
end

function get_days_in_month(mnth, yr)
  return os.date('*t',os.time{year=yr,month=mnth+1,day=0})['day']
end

-- based on original code from sam_lie
function show_calendar(cdate)
  local yy, mm, dd = get_date_parts(cdate)
  local month_days = get_days_in_month(mm, yy)
  local day_week = get_day_of_week(1, mm, yy)

        --- day in which the calendar day start.. 1=Sunday, 2="Monday"
  local day_start = 2

  local days_of_week = {{ "Sun", 1 }, { "Mon", 2 } , { "Tue", 3 }, { "Wed", 4 }, { "Thu", 5 }, { "Fri", 6 }, { "Sat", 7 }}
  local days_of_week_ordered = {}
 
  for k=1, 7 do
    p = k+day_start-1
    if (p>7) then
      p=p-7
    end
    v = {}
    v.dayname = days_of_week[p][1]
    v.daynum = days_of_week[p][2]
    table.insert(days_of_week_ordered, v)
  end

  local out = "<h3>" .. cdate .. "</h3>"
  out = out .. "<table border='1' width='80%' cellspacing='2' cellpadding='5'>"

  out = out .. "<tr>"
  for i,v in ipairs(days_of_week_ordered) do
    out = out .. "<td>" .. v.dayname .. "</td>"

    if (day_week == v.daynum) then
      d = - i + 2
    end
  end
  out = out .. "</tr>"

  while (d < month_days) do
    out = out .. "<tr>"
    for i,v in ipairs(days_of_week) do
      if (d>=1 and d <=month_days) then
        if (d==dd) then
          out = out .. "<td bgcolor='#FFFF99'>" .. d .. "</td>"
        else
          out = out .. "<td>" .. d .. "</td>"
        end
      else
        out = out .. "<td> </td>"
      end

      d = d + 1
    end
    out = out .. "</tr>"
  end

  out = out .. "</table>"

  return out
end

--main
local date = os.date
return (show_calendar(date("%Y")..date("-%m")..date("-%d")))
Re: Lua: Calendar

johnm
New MMEX User

Posts: 6
Joined: Wed Jan 23, 2013 6:41 am
Nikolay,
first of all, thank you for sharing your report!
However, it did not work in 0.9.9.0 version :(

Error: SQLITE_ERROR[1]: near "function": syntax error
------------------------------------------------------------------------------
Re: Lua: Calendar

Nikolay
MMEX Developer

Posts: 2270
Joined: Sat Dec 06, 2008 8:27 am
Location: Sankt-Petersburg, Russia
johnm wrote:Nikolay,
first of all, thank you for sharing your report!
However, it did not work in 0.9.9.0 version :(

Error: SQLITE_ERROR[1]: near "function": syntax error

That only for mmex 0.9.9.2
Page 1 of 1
Who is online

Users browsing this forum: No registered users and 1 guest

cron