Jump to content

Module:Sandbox/R1F4T

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by R1F4T (talk | contribs) at 05:29, 23 April 2025. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}

-- Check for leap year
local function isLeapYear(y)
	return (y % 4 == 0 and y % 100 ~= 0) or (y % 400 == 0)
end

local monthDays = {
	{31,28,31,30,31,30,31,31,30,31,30,31}, -- Non-leap
	{31,29,31,30,31,30,31,31,30,31,30,31}  -- Leap
}

function p.unixToDate(timestamp)
	local secondsInDay = 86400
	local days = math.floor(timestamp / secondsInDay)

	local year = 1970

	while true do
		local leap = isLeapYear(year)
		local yearDays = leap and 366 or 365
		if days < yearDays then break end
		days = days - yearDays
		year = year + 1
	end

	local leap = isLeapYear(year) and 2 or 1
	local month = 1
	while days >= monthDays[leap][month] do
		days = days - monthDays[leap][month]
		month = month + 1
	end

	local day = days + 1

	return string.format("%02d-%02d-%d", day, month, year) -- e.g., 23-04-2025
end

return p