Module:Sandbox/Ans/CS1Local

----< F O R W A R D  D E C L A R A T I O N S >--

local cfg;																		-- table of tables imported from slected Module:Citation/CS1/Configuration

local function _convert_year(val, local_digits) val = mw.ustring.gsub (val, '%d', local_digits);			-- translate 'local' digits to Western 0-9 if val:match("^%d+$") then val = val-543; -- convert year to 4 digits to prevent it being interpreted as day in en module if val<0 then return false; -- TODO: handle this --elseif val<1 then --	val='000'..val; elseif val<10 then -- this should be faster than formatDate? val='000'..val; elseif val<100 then val='00'..val; elseif val<1000 then val='0'..val; end return val; end return false; end

local en_month_name = {'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'}; local function _convert_local_date(val, date_names_local, local_digits) local dmy = mw.text.split(val, '%s+'); if #dmy == 3 then dmy[2] = date_names_local.long[dmy[2]] or date_names_local.short[dmy[2]]; if dmy[2] then --dmy[1] = tonumber(dmy[1]); if dmy[1]<10 then dmy[1]='0'..dmy[1]; end -- this should be faster than formatDate --dmy[2] = tonumber(dmy[2]); if dmy[2]<10 then dmy[2]='0'..dmy[2]; end --return dmy[3]-543 ..'-'..dmy[2]..'-'..dmy[1]; dmy[1] = mw.ustring.gsub (dmy[1], '%d', local_digits);			-- translate 'local' digits to Western 0-9 -- always interprete dmy[1] as day if dmy[1]:match("^%d%d?$") then dmy[1] = tonumber(dmy[1]); if dmy[1]>31 or dmy[1]<1 then return val; end dmy[3] = _convert_year(dmy[3], local_digits); if dmy[3] then return dmy[1]..' '..en_month_name[dmy[2]]..' '..dmy[3]; end return val; end return val; end return val; elseif #dmy == 2 then local dmy1 = date_names_local.long[dmy[1]] or date_names_local.short[dmy[1]]; if dmy1 then dmy[2] = _convert_year(dmy[2], local_digits); if dmy[2] then return en_month_name[dmy1]..' '..dmy[2]; end return val; end dmy[2] = date_names_local.long[dmy[2]] or date_names_local.short[dmy[2]]; if dmy[2] then dmy[1] = mw.ustring.gsub (dmy[1], '%d', local_digits);			-- translate 'local' digits to Western 0-9 -- always interprete dmy[1] as day if dmy[1]:match("^%d%d?$") then dmy[1] = tonumber(dmy[1]); if dmy[1]>31 or dmy[1]<1 then return val; end return dmy[1]..' '..en_month_name[dmy[2]]; end return val; end return val; elseif #dmy == 1 then dmy[1] = date_names_local.long[dmy[1]] or date_names_local.short[dmy[1]]; if dmy[1] then return en_month_name[dmy[1]]; end return val; else return val; end end

--[[

Some local wiki use non-Gregorian calendar. Use this function to convert local non-Gregorian calendar to Gregorian calendar

]]

local function convert_local_date(k, val) if 'year'==k then --return 2010; return val; --elseif 'date'==k or 'access-date'==k or 'archive-date'==k or 'doi-broken-date'==k or 'embargo'==k or 'lay-date'==k or 'publication-date'==k then --	return _convert_local_date(val, cfg.date_names['local'], cfg.date_names.local_digits); --elseif 'embargo'==k then --	return val; else mw.log(k, _convert_local_date(val, cfg.date_names['local'], cfg.date_names.local_digits)); return _convert_local_date(val, cfg.date_names['local'], cfg.date_names.local_digits); --return val; end --return '2010-04-30'; --return val; end

--[[--< S E T _ S E L E C T E D _ M O D U L E S >--

Sets local imported functions table to same (live or sandbox) as that used by the other modules.

]]

local function set_selected_modules (cfg_table_ptr, utilities_page_ptr) cfg = cfg_table_ptr;														-- import tables from selected Module:Citation/CS1/Configuration end

----< E X P O R T S >

return 	{ convert_local_date = convert_local_date, set_selected_modules = set_selected_modules, _convert_local_date = _convert_local_date, -- TODO: split to another separated module? _convert_local_year = _convert_year, -- TODO: split to another separated module? }