Module:Calendar widget: Difference between revisions

Content deleted Content added
No edit summary
cleanup my messes;
Line 20:
dayabbr[i] = v:sub(1, 2)
end
--local monthname = {
-- "January", "February", "March", "April", "May", "June",
-- "July", "August", "September", "October", "November", "December"
--}
--local monthabbr = {}
--for i, v in ipairs(monthname) do
-- monthabbr[i] = v:sub(1, 3)
--end
 
local monthname = {}
Line 37 ⟶ 29:
end
end
 
--local monthnumber = {}
--for i, v in ipairs(monthabbr) do
-- monthnumber[v] = i
--end
--local thisyear = tonumber( lang_obj:formatDate ("Y") )
--local thismonth = tonumber( lang_obj:formatDate ("n") )
 
local function isleap(year)
return '1' == lang_obj:formatDate ('L', tostring(year));
-- year = tonumber(year) or 1
-- return year % 4 == 0
-- and year % 100 ~= 0
-- or year % 1000 == 0
end
 
--[[
returns 1 to 7; 1 == Sunday; TODO: error check inputs
Sakamoto's method: 1 <= month <= 12; year is Gregorian
dayofweek returns 1 to 7 or nil if bad arguments
--]]
local function dayofweek(year, month, day)
return lang_obj:formatDate ('w', year .. '-' .. month .. '-' .. day) + 1;
-- local y = tonumber(year)
-- local m = tonumber(month)
-- local d = tonumber(day)
-- if not (y and m and d) then return nil end
-- local t = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4}
-- if m < 3 then y = y - 1 end
-- return (y + math.floor(y/4) - math.floor(y/100) + math.floor(y/400) + t[m] + d) % 7 + 1
end
 
Line 77 ⟶ 50:
local year = props.year
if isleap(year) then daysinmonth[2] = 29 end
local firstday = monthstartdayofweek (year, mnum, 1); -- get first day number of the first day of the month; 1 == Sunday
local monthcal = {}
local hdr_year = props.show_year and year or '';
table.insert(monthcal, '<table class="mcal">')
table.insert(monthcal, '<tr class="mcalhdr">')
-- table.insert(monthcal, '<th class="mcal" colspan="7">' .. monthname[mnum] .. ' ' .. year .. '</th>')
table.insert(monthcal, '<th class="mcal" colspan="7">' .. monthname[mnum] .. ' ' .. hdr_year .. '</th>')
table.insert(monthcal, '</tr>')
Line 136 ⟶ 108:
 
local function calendar(props)
-- local year = args.year or thisyear
-- local year = args.year
-- local month = props.month
-- if month then
-- local mnum = tonumber(month)
-- if not mnum then
-- if month == "current" then
-- mnum = thismonth
-- year = thisyear
-- elseif month == "last" then
-- mnum = thismonth - 1
-- if mnum == 0 then
-- mnum = 12
-- year = thisyear - 1
-- end
-- elseif month == "next" then
-- mnum = thismonth + 1
-- if mnum == 13 then
-- mnum = 1
-- year = thisyear + 1
-- end
-- else
-- mnum = monthnumber[month:sub(1,3)] or thismonth
-- end
-- end
-- args.year = year
if props.month then
props.show_year = true; -- show year in individual month calendars
-- if mnum > 0 and mnum <13 then
return displaymonth(props, props.month);
props.show_year = true; -- show year in individual month calendars
return displaymonth(props, props.month);
-- return displaymonth(props, mnum)
-- else
-- return displayyear(props)
-- end
else
-- args.year = year
return displayyear(props)
end
Line 202 ⟶ 142:
--]]
function p.calendar(frame)
-- local args = {}
-- for k, v in pairs(frame:getParent().args) do
-- if v ~= "" then args[k] = v end
-- end
-- for k, v in pairs(frame.args) do
-- if v ~= "" then args[k] = v end
-- end
local args=getArgs (frame);
local cal_props = {}; -- separate calendar properties table to preserve arguments as originally provided
Line 220 ⟶ 153:
if not mnum then -- month provided as some sort of text string
if args.month == "current" then
-- mnum = thismonth
-- year = thisyear
cal_props.month = this_month_num
cal_props.year = this_year_num
Line 227 ⟶ 158:
mnum = this_month_num - 1
if mnum == 0 then
-- mnum = 12
-- year = thisyear - 1
cal_props.month = 12 -- december last year
cal_props.year = this_year_num - 1 -- last year
Line 237 ⟶ 166:
mnum = this_month_num + 1
if mnum == 13 then
-- mnum = 1
-- year = thisyear + 1
cal_props.month = 1 -- january next year
cal_props.year = this_year_num + 1 -- next year
Line 245 ⟶ 172:
end
else
-- mnum = monthnumber[month:sub(1,3)] or thismonth
local good
good, cal_props.month = pcall (lang_obj.formatDate, lang_obj, 'n', args.month);
Line 259 ⟶ 185:
end
end
-- TODO: add all other args{} from template or invoke to cal_props{} modified as appropriate
 
return calendar(cal_props)
end