Jump to content

Module:NepaliDateConverter

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Nirmaljoshi (talk | contribs) at 07:23, 26 December 2021. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {};     --All lua modules on Wikipedia must begin by defining a variable 
                    --that will hold their externally accessible functions.
                    --Such variables can have whatever name you want and may 
                    --also contain various data as well as functions.
p.hello = function( frame )     --Add a function to "p".  
                                        --Such functions are callable in Wikipedia
                                        --via the #invoke command.
                                        --"frame" will contain the data that Wikipedia
                                        --sends this function when it runs. 
                                 -- 'Hello' is a name of your choice. The same name needs to be referred to when the module is used.
    
    local str = "Hello Nirmal!"  --Declare a local variable and set it equal to
                                --"Hello World!".  
    
    return str    --This tells us to quit this function and send the information in
                  --"str" back to Wikipedia.
    
end  -- end of the function "hello"
function p.hello_to(frame)		-- Add another function
	local name = frame.args[1]  -- To access arguments passed to a module, use `frame.args`
							    -- `frame.args[1]` refers to the first unnamed parameter
							    -- given to the module
	return "Hello, " .. name .. "!"  -- `..` concatenates strings. This will return a customized
									 -- greeting depending on the name given, such as "Hello, Fred!"
end
function p.count_fruit(frame)
	local num_bananas = frame.args.bananas -- Named arguments ({{#invoke:Example|count_fruit|foo=bar}}) are likewise 
	local num_apples = frame.args.apples   -- accessed by indexing `frame.args` by name (`frame.args["bananas"]`, or)
										   -- equivalently `frame.args.bananas`.
	return 'I have ' .. num_bananas .. ' bananas and ' .. num_apples .. ' apples'
										   -- Like above, concatenate a bunch of strings together to produce
										   -- a sentence based on the arguments given.
end
--This function converts nepali date to english date
-- The input should be either one of following format
-- See MOS for date format
--{{#invoke:moduleForNepaliDateConverter|returnEnglishDate|nepaiYear=1984|nepaliMonth=6|nepaliDay=21}}-->Retruns full date
--{{#invoke:moduleForNepaliDateConverter|returnEnglishDate|nepaiYear=1984}}-->Retruns years e.g. 2007-2008

function p.returnEnglishDate(frame)
	local n_year = tonumber(frame.args.nepaliYear) 
	local n_month = tonumber(frame.args.nepaliMonth) 
	local n_day = tonumber(frame.args.nepaliDay)    
	--define variables to hold converted date
	local e_year

	
	
	-- when nepali day or month is missing, retrun the year only
	-- or when date is out of range(2000-2089 BS), return year only
	local e_year1
	local e_year2
	
	if (n_month== nil or n_day==nil or n_year < 2000 or n_year > 2089) then
		e_year1=n_year-57
		e_year2=n_year-56
		e_year=n_year .. ' [[Bikram Sambat|BS]] ('..e_year1 .. '-' .. e_year2 .. ')'
		return e_year
	end
	-- when both month and day is given, and year is in range  and return compelte date
	--- calculate
	

	 e_year=p.nep_to_eng(n_year, n_month, n_day)
	return e_year
	--return "date is ok. It is in range."
end


function p.nep_to_eng(yy, mm, dd)

            
            local def_nyy = 2000
            local def_nmm = 1
            local def_ndd = 1         -- equivalent nepali dates
            -- initializate
            local total_nDays = 0
            local i = 0
            local k = 0
            local numDay = 0
            

            -- array for nepali calendar
            local bs={} --declear array to hold bikram sambat calendar and fill the array
			bs[0]={2000, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31}
			bs[1]={2001, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30}
			bs[2]={2002, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30}
			bs[3]={2003, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31}
			bs[4]={2004, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31}
			bs[5]={2005, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30}
			bs[6]={2006, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30}
			bs[7]={2007, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31}
			bs[8]={2008, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31}
			bs[9]={2009, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30}
			bs[10]={2010, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30}
			bs[11]={2011, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31}
			bs[12]={2012, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30}
			bs[13]={2013, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30}
			bs[14]={2014, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30}
			bs[15]={2015, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31}
			bs[16]={2016, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30}
			bs[17]={2017, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30}
			bs[18]={2018, 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30}
			bs[19]={2019, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31}
			bs[20]={2020, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30}
			bs[21]={2021, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30}
			bs[22]={2022, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30}
			bs[23]={2023, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31}
			bs[24]={2024, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30}
			bs[25]={2025, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30}
			bs[26]={2026, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31}
			bs[27]={2027, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31}
			bs[28]={2028, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30}
			bs[29]={2029, 31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30}
			bs[30]={2030, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31}
			bs[31]={2031, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31}
			bs[32]={2032, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30}
			bs[33]={2033, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30}
			bs[34]={2034, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31}
			bs[35]={2035, 30, 32, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31}
			bs[36]={2036, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30}
			bs[37]={2037, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30}
			bs[38]={2038, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31}
			bs[39]={2039, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30}
			bs[40]={2040, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30}
			bs[41]={2041, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30}
			bs[42]={2042, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31}
			bs[43]={2043, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30}
			bs[44]={2044, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30}
			bs[45]={2045, 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30}
			bs[46]={2046, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31}
			bs[47]={2047, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30}
			bs[48]={2048, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30}
			bs[49]={2049, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30}
			bs[50]={2050, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31}
			bs[51]={2051, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30}
			bs[52]={2052, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30}
			bs[53]={2053, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30}
			bs[54]={2054, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31}
			bs[55]={2055, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30}
			bs[56]={2056, 31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30}
			bs[57]={2057, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31}
			bs[58]={2058, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31}
			bs[59]={2059, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30}
			bs[60]={2060, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30}
			bs[61]={2061, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31}
			bs[62]={2062, 30, 32, 31, 32, 31, 31, 29, 30, 29, 30, 29, 31}
			bs[63]={2063, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30}
			bs[64]={2064, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30}
			bs[65]={2065, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31}
			bs[66]={2066, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31}
			bs[67]={2067, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30}
			bs[68]={2068, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30}
			bs[69]={2069, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31}
			bs[70]={2070, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30}
			bs[71]={2071, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30}
			bs[72]={2072, 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30}
			bs[73]={2073, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31}
			bs[74]={2074, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30}
			bs[75]={2075, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30}
			bs[76]={2076, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30}
			bs[77]={2077, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31}
			bs[78]={2078, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30}
			bs[79]={2079, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30}
			bs[80]={2080, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30}
			bs[81]={2081, 31, 31, 32, 32, 31, 30, 30, 30, 29, 30, 30, 30}
			bs[82]={2082, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30}
			bs[83]={2083, 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30}
			bs[84]={2084, 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30}
			bs[85]={2085, 31, 32, 31, 32, 30, 31, 30, 30, 29, 30, 30, 30}
			bs[86]={2086, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30}
			bs[87]={2087, 31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30}
			bs[88]={2088, 30, 31, 32, 32, 30, 31, 30, 30, 29, 30, 30, 30}
			bs[89]={2089, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30}
			bs[90]={2090, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30}


			-- if additional calander becomes available, please put here
            --count total days for upto given year
                for i = 0, yy - def_nyy-1 do
                    for j = 2, 13 do
                        total_nDays = total_nDays + bs[k][j]
                    end
                    k = k + 1
                end
           --count and add days for the months 
                for j = 1, mm - 1 do
                    total_nDays = total_nDays + bs[k][j]
                end
                
            --count and add total days within the month
                total_nDays = total_nDays + dd
                
            --=======================================
            ---calculation of equivalent english date--
            --========================================
            local def_eyy = 1943
            local def_emm = 4
            local def_edd = 14 - 1   -- 1 Baisakh 2000 BS=14 April , 1943 AD
            -- array for english calendar
            local month = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} --normal english year
            local lmonth = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} --leap english year
            local a = 0 -- to hold days of a month during calculation
            local total_eDays = def_edd
            local day = 4 - 1  
            
               local m = def_emm
               local y = def_eyy
                while (total_nDays ~= 0)
                do
                    if (p.is_leap_year(y)) then
                         a = lmonth[m]
                    else
                        a = month[m]
                    end
                    total_eDays = total_eDays + 1
                    day = day + 1
                    if (total_eDays > a) then
                        m = m + 1
                        total_eDays = 1
                        if (m > 12) then
                            y = y + 1
                            m = 1
                        end
                    end
                    if (day > 7) then 
                    	day = 1
                    end
                    total_nDays = total_nDays - 1
                end
                numDay = day
                
                returnVal=y..'-'..m..'-'..day
                --returnVal=y..m..day
                return returnVal
end

function p.is_leap_year(year)
	--this function checks if the given year is a leap year or not
	local returnVal
    local a = year
    if math.fmod(a,100) == 0 then
            if math.fmod(a,400) == 0 then
                returnVal = true
            else
                returnVal = false
            end
     else
            if math.fmod(a,4)==0 then
               returnVal = true
            else
                returnVal = false
            end
    end
    return returnVal
end
        
return p    --All modules end by returning the variable containing their functions to Wikipedia.

-- Now we can use this module by calling {{#invoke: Example | hello }},
-- {{#invoke: Example | hello_to | foo }}, or {{#invoke:Example|count_fruit|bananas=5|apples=6}}
-- Note that the first part of the invoke is the name of the Module's wikipage,
-- and the second part is the name of one of the functions attached to the 
-- variable that you returned.

-- The "print" function is not allowed in Wikipedia.  All output is accomplished
-- via strings "returned" to Wikipedia.