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 06:25, 23 April 2025. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}

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},
	{31,29,31,30,31,30,31,31,30,31,30,31}
}

local function toBengaliNumber(num)
	local bnDigits = {'০','১','২','৩','৪','৫','৬','৭','৮','৯'}
	return tostring(num):gsub("%d", function(d) return bnDigits[tonumber(d)+1] end)
end

function p.unixToBengaliDate(frame)
	local timestamp = tonumber(frame.args[1]) + 21600
	local second = 86400
	local days = math.floor(timestamp / second)

	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("%s-%s-%s", toBengaliNumber(day), toBengaliNumber(month), toBengaliNumber(year))
end

return p