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 05:59, 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
	local e_month
	local e_day
	
	
	-- 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
	

	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}

	
	return bs[0][2]
	--return "date is ok. It is in range."
end





function p.nep_to_eng(yy, mm, dd)

            local a
            local day
            local m
            local y
            local i
            local j
            local k
            local numDay
            local month
            local lmonth
            
            local def_eyy = 1943
            local def_emm = 4
            local def_edd = 14 - 1   --  english dates
            
            local def_nyy = 2000
            local def_nmm = 1
            local def_ndd = 1         -- equivalent nepali dates
            -- initializate
            local total_eDays = 0
            local total_nDays = 0
            local a = 0
            local day = 4 - 1      
            local m = 0
            local y = 0
            local i = 0
            local k = 0
            local numDay = 0
            
            month = Array(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
            lmonth = Array(0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) --leap year
            
            --count total days in-terms of years
                for i = 0, yy - def_nyy - 1 do
                    for j = 1, 12 do
                        total_nDays = total_nDays + bs(k)(j)
                    end
                    k = k + 1
                end
           -- count total days in-terms of months
                for j = 1, mm - 1 do
                    total_nDays = total_nDays + bs[k][j]
                end
                
            --count total days in-terms of days
                total_nDays = total_nDays + dd
                
            --calculation of equivalent english date--
                total_eDays = def_edd
                m = def_emm
                y = def_eyy
                while (total_nDays ~= 0)
                do
                    if (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
                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.