Bước tới nội dung

Mô đun:Âm lịch/sandbox

Bách khoa toàn thư mở Wikipedia
local p = {}-- Gọi module tính toán chínhlocal amlich = require('Module:Âm lịch') -- Hàm tính số tuần ISO (Tuần 01-53)local function get_week_number(d, m, y)    local t = os.time{year=y, month=m, day=d}    return os.date("%V", t)end-- Hàm lấy thứ (0=Thứ 2, ..., 6=Chủ nhật)local function get_day_of_week_monday_start(d, m, y)    local a = math.floor((14 - m) / 12)    local y_val = y + 4800 - a    local m_val = m + 12 * a - 3    local jd = d + math.floor((153 * m_val + 2) / 5) + 365 * y_val + math.floor(y_val / 4) - math.floor(y_val / 100) + math.floor(y_val / 400) - 32045        local dow_sunday_start = (jd + 1) % 7     local dow_monday_start = (dow_sunday_start + 6) % 7    return dow_monday_startendfunction p.month_table(frame)    local args = frame.args    local solar_month = tonumber(args.month) or tonumber(os.date("%m"))    local solar_year = tonumber(args.year) or tonumber(os.date("%Y"))    local float = args.float or "right"        -- Tham số highlight (mặc định là 'off')    local highlight = args.highlight or "off"     local today = os.date("*t") -- Lấy ngày hiện tại        local days_in_month = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}    -- Kiểm tra năm nhuận dương lịch    if (solar_year % 4 == 0 and solar_year % 100 ~= 0) or (solar_year % 400 == 0) then        days_in_month[2] = 29    end    local num_days = days_in_month[solar_month]        -- Tìm thứ của ngày mùng 1    local start_dow = get_day_of_week_monday_start(1, solar_month, solar_year)        local html = {}    -- Style max-width:100% để hỗ trợ responsive    table.insert(html, '{| class="toccolours float' .. float .. '" style="text-align:center; max-width:100%;" cellpadding="2" cellspacing="0"')        -- 1. Tiêu đề Tháng/Năm có link    local month_names = {"Tháng 1", "Tháng 2", "Tháng 3", "Tháng 4", "Tháng 5", "Tháng 6", "Tháng 7", "Tháng 8", "Tháng 9", "Tháng 10", "Tháng 11", "Tháng 12"}    table.insert(html, '|- class="calendar-purple"')    local title_str = string.format('[[%s]] năm [[%s]]', month_names[solar_month], solar_year)    table.insert(html, '!colspan="8" style="background:#ccccff; color:#000000; text-align: center;" | ' .. title_str)        -- 2. Hàng tiêu đề Tuần & Thứ    table.insert(html, '|-')    table.insert(html, '! style="background:#e6e6ff; border-right: 2px solid #fff;" | [[Tuần]]')        local days_display = {"T2", "T3", "T4", "T5", "T6", "T7", "CN"}    local days_links = {"Thứ Hai", "Thứ Ba", "Thứ Tư", "Thứ Năm", "Thứ Sáu", "Thứ Bảy", "Chủ Nhật"}        for i, d in ipairs(days_display) do        local style = "background:#e6e6ff;"        if d == "CN" then style = "background:#ffcccc;" end -- Tô đỏ tiêu đề CN        local link_str = string.format('[[%s|%s]]', days_links[i], d)        table.insert(html, '! style="' .. style .. '" | ' .. link_str)    end    table.insert(html, '|-')        -- Số tuần của hàng đầu tiên    local week_num = get_week_number(1, solar_month, solar_year)    table.insert(html, '| style="background:#f0f0f0; font-weight:bold; color:#777; border-right: 1px solid #aaa;" | ' .. week_num)    -- Vẽ ô trống đầu tháng    local current_dow = 0    for i = 0, start_dow - 1 do        table.insert(html, '| &nbsp;')        current_dow = current_dow + 1    end        -- Vòng lặp vẽ ngày    for day = 1, num_days do        -- Xuống dòng nếu hết tuần        if current_dow == 7 then            table.insert(html, '|-')            current_dow = 0            -- Thêm số tuần ở đầu dòng mới            week_num = get_week_number(day, solar_month, solar_year)            table.insert(html, '| style="background:#f0f0f0; font-weight:bold; color:#777; border-right: 1px solid #aaa;" | ' .. week_num)        end                -- Tính ngày âm lịch        local l_day, l_month, l_year, l_leap = amlich.Solar2Lunar(day, solar_month, solar_year, 7)        -- Sửa lỗi tháng 12 thành 0        if l_month == 0 then l_month = 12 end        local lunar_text = l_day        if l_day == 1 then            lunar_text = l_day .. "/" .. l_month            if l_leap == 1 then lunar_text = lunar_text .. "nh" end        end                -- Style mặc định        local cell_style = 'style="height:40px; vertical-align:top; border:1px solid #aaa;"'                -- Style cho Chủ nhật (cột cuối cùng)        if current_dow == 6 then              cell_style = 'style="height:40px; vertical-align:top; border:1px solid #aaa; background-color:#fff0f0;"'        end        -- Highlight ngày hiện tại        if highlight ~= "off" then            if (day == today.day) and (solar_month == today.month) and (solar_year == today.year) then                cell_style = 'style="height:40px; vertical-align:top; border: 2px solid #E00; background-color: #ffffcc;"'            end        end        -- Nội dung ô: Ngày dương (có link) + Ngày âm        local solar_link = string.format('[[%d tháng %d|%d]]', day, solar_month, day)        local content = string.format("'''%s'''<br/><span style='font-size:85%%; color:#555;'>%s</span>", solar_link, lunar_text)                table.insert(html, '| ' .. cell_style .. ' | ' .. content)        current_dow = current_dow + 1    end        -- 3. Xử lý ô trống cuối tháng (Gộp ô + Có viền)    if current_dow > 0 and current_dow < 7 then        local remaining_days = 7 - current_dow        local empty_style = string.format('colspan="%d" style="height:40px; vertical-align:top; border:1px solid #aaa; background-color:#fcfcfc;"', remaining_days)        table.insert(html, '| ' .. empty_style .. ' | &nbsp;')    end        table.insert(html, '|}')    return table.concat(html, "\n")endreturn p