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