وحدة:Hijri

local cfg= mw.loadData('Module:Hijri/Configuration')local core = require 'Module:Hijri/core'--[[ ----------------- I M P O R T --------------]]gregorian2jd = core.gregorian2jdjd2hijri = core.jd2hijrihijri2jd = core.hijri2jdhijri_days_in_month = core.hijri_days_in_monthhijri_isleap = core.hijri_isleaphijri_yday = core.hijri_ydayjd2unix = core.jd2unixunix2jd = core.unix2jdjd2wday = core.jd2wdayjd2gregorian = core.jd2gregoriangre_isleap = core.gre_isleapgre_yday = core.gre_ydaygre_days_in_month = core.gre_days_in_monthjd2julian = core.jd2julianjulian2jd = core.julian2jdjulian_isleap = core.julian_isleapjulian_yday = core.julian_ydayjulian_days_in_month = core.julian_days_in_monthgregorian2hijri = core.gregorian2hijrihijri2gregorian = core.hijri2gregorianDate = core.Datehijri_check = core.hijri_checkstring = mw.ustringlocal str_hijri_mode = select(cfg.hijri_mode + 1, 'hijri_tabular', 'hijri_adjusted_umalqura', 'hijri_umalqura')--[[---------------------- utilty function ----------------- ]]local gtonumber = tonumberlocal function tonumber(str)if not str thenreturn nilendlocal thenumber = gtonumber(str) or mw.language.getContentLanguage():parseFormattedNumber(str)if not thenumber thenreturn nilendreturn math.floor(thenumber)endlocal function substitute( msg, args )return args and mw.message.newRawMessage( msg, args ):plain() or msg;endlocal function error( msg, args )return substitute( cfg.presentation.error, substitute( msg, args ) );endlocal function towdigit(number)return ((number<10) and '0' or '') .. numberendlocal function hijri_month_name(index)if cfg.hijri_months thenreturn cfg.hijri_months[index]elsereturn mw.message.new('hijri-calendar-m' .. index):plain()endendlocal function wday_name(index)if cfg.wday_name  thenreturn cfg.wday_name[index]elsereturn mw.message.new(select(index +1 ,'Sunday','Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday')):plain()endendlocal function julian_month_name(index)if cfg.gregorian_months  thenreturn cfg.gregorian_months[index]elsereturn mw.message.new(select(index, 'january', 'february', 'march', 'april', 'may_long', 'june', 'july', 'august', 'september', 'october', 'november', 'december')):plain()endendlocal format_func = {towdigit = towdigit,hijri_month_name = hijri_month_name,wday_name = wday_name,julian_month_name = julian_month_name}local function substitute( msg, args )return args and mw.message.newRawMessage( msg, args ):plain() or msg;endlocal function error( msg, args )return substitute( cfg.presentation.error, substitute( msg, args ) );endlocal function argument_wrapper(arg)local nilargs = {}return setmetatable({},{__index = function ( tbl, k )local v = rawget(tbl,k)if v thenreturn velseif nilargs[k] thenreturn nilendlocal list = cfg.aliases[k];for _,arglist in ipairs(arg) doif type( list ) == 'table' thenfor _, alias_key in ipairs( list ) doif arglist[alias_key] thenv = arglist[alias_key]break;endendelseif list ~= nil thenv = arglist[list]endif v thenbreak;endendif v == nil thennilargs[k] = trueelserawset( tbl, k, v )endreturn vend,});endlocal function get_value_from_aliaes(aliases_tbl, arg_value)if arg_value  and aliases_tbl thenfor k, v_list in pairs(aliases_tbl) doif type(v_list) ~= "table" thenif arg_value == v_list thenreturn kendelsefor _, v_value in ipairs(v_list) doif arg_value == v_value thenreturn kendendendendendend--[[ -------------------- frame functions ---------------------------------------]]local function test(frame)return mw.language.getContentLanguage():formatDate("Y n j", '@-86400' ,true)endfunction get_gregorian_from_hijri(frame)return mw.text.listToText({ hijri2gregorian(tonumber(frame.args[1]),tonumber(frame.args[2]),tonumber(frame.args[3])) },"-","-")endfunction get_hijri_from_gregorian(frame)d= Date(str_hijri_mode)d:from_gregorian(tonumber(frame.args[1]),tonumber(frame.args[2]),tonumber(frame.args[3]))return dendlocal function lua_format_date(A)local theformat = A.format or 'xmj xmF xmY'local cDateif A.year and A.day and A.month thenlocal year, month, day = tonumber(A.year), tonumber(A.month), tonumber(A.day)local source_cal = get_value_from_aliaes(cfg.cal_aliases, A.source_cal)if not source_cal thenif day>30 or year > 1500 thensource_cal = 'gregorian'elsesource_cal='hijri'endendlocal wday = get_value_from_aliaes(cfg.wday_aliases, A.wday)if source_cal == 'hijri' thencDate = Date(str_hijri_mode)if wday thencDate:from_hijri(year,month,day,wday,cfg.hijri_mode)elsecDate:set_date(year,month,day,true)endelsecDate = Date(source_cal)if string.sub(source_cal,1,5) == 'hijri' and wday thencDate:from_hijri(year,month,day,wday)elsecDate:set_date(year,month,day)endendelseif A.date thenlocal tmpdate = mw.language.getContentLanguage():formatDate('Y/n/j', A.date)if tmpdate thenlocal gre = mw.text.split(tmpdate,'/')cDate = Date('gregorian',tonumber(gre[1]), tonumber(gre[2]), tonumber(gre[3]))elsecDate = Date('gregorian')endelsecDate = Date('gregorian')endif string.find(theformat, 'x[muwg][jFntYyzdm]') or string.find(theformat, '~[هميغ][يشرسلةع]0?') thenlocal lc_hijri_mode = get_value_from_aliaes(cfg.cal_aliases, A.hijri_cal_type) or str_hijri_mode-- date replaceslocal symbols = cfg.symbol_replace.symbolslocal dr = cfg.symbol_replace.calendarslocal pat, cal_typefor drk,drv in pairs(dr) docal_type = (drk == 'hijri') and lc_hijri_mode or drv.typefor sym_pre_i, sym_pre in ipairs(drv.sym_pre) doif drv.symbols_pattern thenpat = drv.symbols_pattern[sym_pre_i]elsepat = sym_pre .. cfg.symbol_replace.default_symbol_pattern[sym_pre_i]endif string.find(theformat,pat) thencDate:set_type(cal_type)local tempformat,sy = theformat,''local ss,se = string.find(tempformat,pat)theformat = ''while ss dosy = string.sub(tempformat,ss,se)if ss > 1 thentheformat = theformat .. string.sub(tempformat,1,ss-1)endif se < #tempformat thentempformat = string.sub(tempformat,se+1)elsetempformat = ''endfor sym, symv in pairs(symbols[sym_pre_i]) doif sy == sym_pre .. sym thenif symv.func thenif format_func[symv.func] thentheformat = theformat .. format_func[symv.func](cDate[symv.rep])elseif format_func[drv.func[symv.func]] thentheformat = theformat .. format_func[drv.func[symv.func]](cDate[symv.rep])elseerror('Can`t find fromating function')endelsetheformat = theformat .. cDate[symv.rep]endendendss,se = string.find(tempformat,pat)endtheformat = theformat .. tempformatendendendendif string.find(theformat,"[a-zA-Z]") and (not A.pass_mw_time or A.pass_mw_time == '1' or A.pass_mw_time == 'نعم')  thencDate:set_type('gregorian')return  mw.language.getContentLanguage():formatDate(theformat, cDate.timestamp and ("@" .. cDate.timestamp) or cDate ,true)elsereturn theformatendendlocal function format_date(frame)local A = argument_wrapper({frame:getParent().args, frame.args})return lua_format_date(A)endreturn { test = test,get_gregorian_from_hijri = get_gregorian_from_hijri,get_hijri_from_gregorian =get_hijri_from_gregorian,format_date = format_date,lua_format_date=lua_format_date,julian_month_name = julian_month_name,hijri_month_name = hijri_month_name}