Bahaipedia
Bahaipedia
Menu
About Bahaipedia
Ask a question
General help
Random page
Recent changes
In other projects
Tools
What links here
Related changes
Upload file
Special pages
Printable version
Permanent link
Page information
Module
Discussion
View history
Talk
Contributions
Create account
Log in
Navigation
About Bahaipedia
Ask a question
General help
Random page
Recent changes
In other projects
Learn more
Core topics
Bahá’í Faith
Central Figures
Teachings
Practices
Tools
What links here
Related changes
Upload file
Special pages
Printable version
Permanent link
Page information
Translations

Module:BahaiDateTime

From Bahaipedia
Jump to:navigation, search
Module documentation[create] [purge]
You might want to create a documentation page for this Scribunto module.
Editors can experiment in this module's sandbox (create | mirror) and testcases (create) pages.
Add categories to the /doc subpage. Subpages of this module.
local module = {}

local bahaiMonthsNames_map = {
	[1]  = {arabic = "Bahá",       },
	[2]  = {arabic = "Jalál",      },
	[3]  = {arabic = "Jamál",      },
	[4]  = {arabic = "‘Aẓamat",    },
	[5]  = {arabic = "Núr",        },
	[6]  = {arabic = "Raḥmat",     },
	[7]  = {arabic = "Kalimát",    },
	[8]  = {arabic = "Kamál",      },
	[9]  = {arabic = "Asmá’",      },
	[10] = {arabic = "‘Izzat",     },
	[11] = {arabic = "Mashíyyat", },
	[12] = {arabic = "‘Ilm",       },
	[13] = {arabic = "Qudrat",     },
	[14] = {arabic = "Qawl",       },
	[15] = {arabic = "Masá’il",    },
	[16] = {arabic = "Sharaf",    },
	[17] = {arabic = "Sulṭán",     },
	[18] = {arabic = "Mulk",       },
	[19] = {arabic = "Ayyám-i-Há",       },
	[20] = {arabic = "‘Alá’",      },
}

local newYears_map = {
	[2015]=21 --[[March]],[2016]=20,[2017]=20,[2018]=21,[2019]=21,[2020]=20,[2021]=20,[2022]=21,[2023]=21,[2024]=20,[2025]=20,[2026]=21,[2027]=21,[2028]=20,[2029]=20,[2030]=20,[2031]=21,[2032]=20,[2033]=20,[2034]=20,[2035]=21,[2036]=20,[2037]=20,[2038]=20,[2039]=21,[2040]=20,[2041]=20,[2042]=20,[2043]=21,[2044]=20,[2045]=20,[2046]=20,[2047]=21,[2048]=20,[2049]=20,[2050]=20,[2051]=21,[2052]=20,[2053]=20,[2054]=20,[2055]=21,[2056]=20,[2057]=20,[2058]=20,[2059]=20,[2060]=20,[2061]=20,[2062]=20,[2063]=20,[2064]=20,
}

local interCalaryDays_map = {
	[2016]=4,[2017]=4,[2018]=5,[2019]=4,[2020]=4,[2021]=4,[2022]=5,[2023]=4,[2024]=4,[2025]=4,[2026]=5,[2027]=4,[2028]=4,[2029]=4,[2030]=4,[2031]=5,[2032]=4,[2033]=4,[2034]=4,[2035]=5,[2036]=4,[2037]=4,[2038]=4,[2039]=5,[2040]=4,[2041]=4,[2042]=4,[2043]=5,[2044]=4,[2045]=4,[2046]=4,[2047]=5,[2048]=4,[2049]=4,[2050]=4,[2051]=5,[2052]=4,[2053]=4,[2054]=4,[2055]=5,[2056]=4,[2057]=4,[2058]=4,[2059]=4,[2060]=5,[2061]=4,[2062]=4,[2063]=4,[2064]=5,[2065]=4,
}

--Example: {year=gregorianYear, month=3, day=newYears_map[gregorianYear], hour=0, sec=1} 
local function CreateDate(dateData)
    local dateStamp = os.time(dateData)
	return  os.date("*t", dateStamp)
end

local function GregorianDateToBahaiDate(gregorianCurrentYear, gregorianCurrentMonth, gregorianCurrentDay) 
	local currentDate = CreateDate({year=gregorianCurrentYear, month=gregorianCurrentMonth, day=gregorianCurrentDay, hour=0, sec=1} )
	
	--Assumption that it is already after new year
	local newYearDate = CreateDate({year=gregorianCurrentYear, month=3, day=newYears_map[gregorianCurrentYear], hour=0, sec=1} )
	local currentBahaiDayOfYear = currentDate.yday - newYearDate.yday + 1
	
	local bahaiYear = gregorianCurrentYear - 1843
	 
	--Current Bahai year started in previous gregorian year
	if currentDate.yday	< newYearDate.yday then
		bahaiYear = bahaiYear - 1
		newYearDate = CreateDate({year=gregorianCurrentYear-1, month=3, day=newYears_map[gregorianCurrentYear-1], hour=0, sec=1} )
		local lastDay = CreateDate({year=gregorianCurrentYear-1, month=12, day=31, hour=0, sec=1} ).yday
		local bahaiDaysInPreviousGregorianYear = lastDay - newYearDate.yday
		local bahaiDaysInCurrentGregorianYear = currentDate.yday
		currentBahaiDayOfYear = bahaiDaysInPreviousGregorianYear + bahaiDaysInCurrentGregorianYear + 1
	end
	
	local bahaiDay = 0
	local bahaiMonth = 19
	local lastMonthDays
	--Determining month
	for month=1, 20 do
		if month == 19 then
			lastMonthDays = interCalaryDays_map[gregorianCurrentYear]
		else
			lastMonthDays = 19 
		end
		
		bahaiDay = bahaiDay + lastMonthDays
		
		if bahaiDay >= currentBahaiDayOfYear then
			bahaiMonth = month
			break
		end
	end
	
	--Determining day of the month
	local bahaiDayOfMonth = currentBahaiDayOfYear - (bahaiDay - lastMonthDays)

	return {year = bahaiYear, month = bahaiMonth, day = bahaiDayOfMonth, yday = currentBahaiDayOfYear}
	--return "CE: " .. currentDate.year .. "-" .. currentDate.month .. "-" .. currentDate.day .. "    BE: Year:" .. bahaiYear  .. " Month:"..bahaiMonth .. " (" .. bahaiMonthsNames_map[bahaiMonth].arabic..") Day:" .. bahaiDayOfMonth .. " Day of year:" .. currentBahaiDayOfYear
end

local function Frame2Date(frame)
	local gregorianCurrentYear = tonumber(frame:getArgument(1):expand())
	local gregorianCurrentMonth = tonumber(frame:getArgument(2):expand())
	local gregorianCurrentDay = tonumber(frame:getArgument(3):expand())
	return gregorianCurrentYear, gregorianCurrentMonth, gregorianCurrentDay
end



local function FormatBahaiDate(bahaiDate, formatName)
	if formatName == "DayofMonth" then
		return 	bahaiDate.day .. " " .. bahaiMonthsNames_map[bahaiDate.month].arabic
	end
	
	if formatName == "Year" then
		return 	bahaiDate.year
	end
	
	if formatName == "FullDate" then
		return 	bahaiDate.day .. " " .. bahaiMonthsNames_map[bahaiDate.month].arabic .. ", " .. bahaiDate.year
	end

	return "BE: Year:" .. bahaiDate.year  .. " Month:"..bahaiDate.month .. " (" .. bahaiMonthsNames_map[bahaiDate.month].arabic..") Day:" .. bahaiDate.day .. " Day of year:" .. bahaiDate.yday
end

--Arguments: {{CURRENTYEAR}}|{{CURRENTMONTH1}}|{{CURRENTDAY}}
--Result: "18 ‘Alá’, 176"
function module.GregorianDateToBahaiDate(frame) 
	return FormatBahaiDate(GregorianDateToBahaiDate(Frame2Date(frame)) , "FullDate")
end

--Arguments: {{CURRENTYEAR}}|{{CURRENTMONTH1}}|{{CURRENTDAY}}
--Result: "18 ‘Alá’"
function module.GregorianDateToBahaiDayMonth(frame) 
	return FormatBahaiDate(GregorianDateToBahaiDate(Frame2Date(frame)) , "DayofMonth")
end

--Arguments: {{CURRENTYEAR}}|{{CURRENTMONTH1}}|{{CURRENTDAY}}
--Result: 176
function module.GregorianDateToBahaiYear(frame) 
	return FormatBahaiDate(GregorianDateToBahaiDate(Frame2Date(frame)) , "Year")
end

function module.DatesTest()
	local dateStamp = os.time({year=2016, month=1, day=1, hour=0, sec=1})
	local result = ""
	for i=1, 4400 do 
		local date = os.date("*t", dateStamp)
		local bahaiDate = GregorianDateToBahaiDate(date.year, date.month, date.day) 
		local bahaiDateText = "CE: " .. date.year .. "-" .. date.month .. "-" .. date.day .. "  " 
		.. FormatBahaiDate(bahaiDate)
		
		result = result .. bahaiDateText .. tostring(mw.html.create( 'div' ))
		dateStamp = dateStamp + 86400
	end

	return result
end

--Returns current date time in in Bahai format. Arguments: requestedDateDype
local function GetFormattedCurrentBahaiDate(requestedDateDype) 
	local date = os.date("*t")
	local bahaiDate = GregorianDateToBahaiDate(date.year, date.month, date.day) 
	return FormatBahaiDate(bahaiDate, requestedDateDype)
end

function module.DayofMonth()
	return GetFormattedCurrentBahaiDate("DayofMonth") 
end

function module.Year()
	return GetFormattedCurrentBahaiDate("Year") 
end


--[[
{{#invoke:BahaiDateTime|GregorianDateToBahaiDate|{{CURRENTYEAR}}|{{CURRENTMONTH1}}|{{CURRENTDAY}} }}

{{#invoke:BahaiDateTime|GregorianDateToBahaiDayMonth|{{CURRENTYEAR}}|{{CURRENTMONTH1}}|{{CURRENTDAY}} }}

{{#invoke:BahaiDateTime|GregorianDateToBahaiYear|{{CURRENTYEAR}}|{{CURRENTMONTH1}}|{{CURRENTDAY}} }}

]]

return module
Retrieved from "https://bahaipedia.org/index.php?title=Module:BahaiDateTime&oldid=85121"
This page was last edited on 2 October 2020, at 20:01.
Text is available under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License.
Privacy policy
About Bahaipedia
Disclaimers
Powered by MediaWiki