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:19, 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 = frame.args.nepaliYear 
	local n_month = frame.args.nepaliMonth  
	local n_day = 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, calculate and return compelte date
	if p.checkRange(n_year) then
			return "in range"
		else 
			return "not in range"
	end

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.