Module:LatestUpdate: Difference between revisionsGive feedback
Jump to navigation
Jump to search
created |
No edit summary |
||
| Line 3: | Line 3: | ||
-- Load JSON content from a page | -- Load JSON content from a page | ||
local data = mw.loadJsonData("Data:LatestUpdate.json") | local data = mw.loadJsonData("Data:LatestUpdate.json") | ||
local update = data.latest_update | local update = data.latest_update or {} | ||
-- Month name → number mapping | |||
local month_map = { | |||
January = 1, | |||
February = 2, | |||
March = 3, | |||
April = 4, | |||
May = 5, | |||
June = 6, | |||
July = 7, | |||
August = 8, | |||
September = 9, | |||
October = 10, | |||
November = 11, | |||
December = 12 | |||
} | |||
function p.month() | function p.month() | ||
return update.month | return update.month or "" | ||
end | end | ||
function p.day() | function p.day() | ||
return update.day | return update.day or "" | ||
end | end | ||
function p.year() | function p.year() | ||
return update.year | return update.year or "" | ||
end | |||
local function getMonthNumber(monthName) | |||
return month_map[monthName] | |||
end | |||
local function getCurrentTime() | |||
local frame = mw.getCurrentFrame() | |||
local now_str = frame:callParserFunction("CURRENTTIMESTAMP") | |||
return os.time({ | |||
year = tonumber(string.sub(now_str, 1, 4)), | |||
month = tonumber(string.sub(now_str, 5, 6)), | |||
day = tonumber(string.sub(now_str, 7, 8)), | |||
hour = 0 | |||
}) | |||
end | |||
-- Calculate days since last update | |||
function p.days_since() | |||
local month_number = getMonthNumber(update.month) | |||
if not month_number or not update.day or not update.year then | |||
return 0 | |||
end | |||
local update_time = os.time({ | |||
year = tonumber(update.year), | |||
month = month_number, | |||
day = tonumber(update.day), | |||
hour = 0 | |||
}) | |||
local now = getCurrentTime() | |||
local diff_seconds = os.difftime(now, update_time) | |||
local days = math.floor(diff_seconds / 86400) | |||
if days < 0 then | |||
return 0 | |||
end | |||
return days | |||
end | |||
-- Return formatted text (e.g. "1 day ago", "5 days ago") | |||
function p.days_since_text() | |||
local days = p.days_since() | |||
return days .. (days == 1 and " day ago" or " days ago") | |||
end | end | ||
return p | return p | ||