Module:LatestUpdate: Difference between revisionsGive feedback
No edit summary |
No edit summary |
||
| Line 35: | Line 35: | ||
local function getMonthNumber(monthName) | local function getMonthNumber(monthName) | ||
return month_map[monthName] | return month_map[monthName] | ||
end | end | ||
| Line 64: | Line 52: | ||
}) | }) | ||
local now = | local now = os.time() | ||
local diff_seconds = os.difftime(now, update_time) | local diff_seconds = os.difftime(now, update_time) | ||
| Line 79: | Line 67: | ||
function p.days_since_text() | function p.days_since_text() | ||
local days = p.days_since() | local days = p.days_since() | ||
return days .. (days == 1 and " day ago" or " days ago") | return days .. (days == 1 and " day ago" or " days ago") | ||
end | end | ||
return p | return p | ||
Revision as of 13:28, 10 April 2026
Overview
This module calculates the days since the last update stored in Data:LatestUpdate.json and sends to Module:UpdateExcerpt to be displayed by Template:Deadlock Wiki/UpdateExcerpt. It can also be called to calculate the time since any given date, in days.
Examples
days_since_date
This function can be called to calculate the days passed from a given date, using the format "Month/Day/Year". "Month" has to be in numbers.
For days since January 30 2026:
{{#invoke:LatestUpdate|days_since_date|1|30|2026}}
Script error: The function "days_since_date" does not exist.
days_since
This is an automated function that calculates the days since the latest update stored in Data:LatestUpdate.json.
days_since_text
This is an automated function that wraps the "days_since" data in a localized text template to be used by Module:UpdateExcerpt.
local p = {}
-- Load JSON content from a page
local data = mw.loadJsonData("Data:LatestUpdate.json")
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()
return update.month or ""
end
function p.day()
return update.day or ""
end
function p.year()
return update.year or ""
end
local function getMonthNumber(monthName)
return month_map[monthName]
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 = os.time()
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
return p