انتقل إلى المحتوى

وحدة:HijriDate

من ويكي الاحرار

يمكن إنشاء صفحة توثيق الوحدة في وحدة:HijriDate/شرح

local p = {}

local months = {
	"محرم",
	"صفر",
	"ربيع الأول",
	"ربيع الآخر",
	"جمادى الأولى",
	"جمادى الآخرة",
	"رجب",
	"شعبان",
	"رمضان",
	"شوال",
	"ذو القعدة",
	"ذو الحجة"
}

-- Tabular Islamic calendar
local function gregorian_to_hijri(y, m, d)
	local jd = math.floor(
		(1461 * (y + 4800 + math.floor((m - 14) / 12))) / 4 +
		(367 * (m - 2 - 12 * math.floor((m - 14) / 12))) / 12 -
		(3 * math.floor((y + 4900 + math.floor((m - 14) / 12)) / 100)) / 4 +
		d - 32075
	)

	local l = jd - 1948440 + 10632
	local n = math.floor((l - 1) / 10631)
	l = l - 10631 * n + 354
	local j = (
		math.floor((10985 - l) / 5316) *
		math.floor((50 * l) / 17719) +
		math.floor(l / 5670) *
		math.floor((43 * l) / 15238)
	)
	l = l - (
		math.floor((30 - j) / 15) *
		math.floor((17719 * j) / 50) -
		math.floor(j / 16) *
		math.floor((15238 * j) / 43)
	) + 29

	local month = math.floor((24 * l) / 709)
	local day = l - math.floor((709 * month) / 24)
	local year = 30 * n + j - 30

	return day, month, year
end

function p.today()
	local t = os.date("*t")
	local d, m, y = gregorian_to_hijri(t.year, t.month, t.day)

	if not months[m] then
		return "خطأ في حساب التاريخ الهجري"
	end

	return d .. " " .. months[m] .. " " .. y .. " هـ"
end

return p