Module:Date

د ويکيپېډيا، وړیا پوهنغونډ له خوا
و اصلی برخی ته ورشی د پلټنې ځای ته ورټوپ کړی

لاسوند لپاره ددې موډيول کېدای سی په Module:Date/لاسوند کي وي

--
-- Written by Baloch for pswikinews.
-- Version 1.0 (May 11 2017)
-- 

local p = {}

p.weekdays1 = {}
p.weekdays1['en'] = {"Sunday","Monday","Tuesday","Wednday","Thursday","Friday","Saturday"}
p.weekdays1['ps'] = {"یکشنبه","دوشنبه","سه‌شنبه","چهارشنبه","پنج‌شنبه","جمعه","شنبه"}

p.months1 = {}
p.months1['gregorian'] = {}
p.months1['gregorian']['en'] = {"January","February","March","April","May","June","July","August","September","October","November","December"}
p.months1['gregorian']['ps'] = {"جنوري","فبروري","مارچ","اپریل","می","جون","جولای","اګست","سپټمبر","اکتوبر","نومبر","ډسمبر"}

p.months1['jalali'] = {} 
p.months1['jalali']['en'] = {"Wri","Ghwayay","Ghbargolay","Chongash","Zmaray","Wazhy","Tala","Ladam","Lindi","Marghomay","Salwaghah","Kab"}
p.months1['jalali']['ps'] = {"وری","غويی","غبرګولی","چنګاښ","زمری","وږی","تله","لړم","ليندۍ","مرغومی","سلواغه","کب"}
p.am_pm1 = {}
p.am_pm1['en'] = {"AM","PM"}
p.am_pm1['ps'] = {"ق.ظ.","ب.ظ."}

p.am_pm2 = {}
p.am_pm2['en'] = {"am","pm"}
p.am_pm2['ps'] = {"ق.ظ.","ب.ظ."}

p.nums = {}
p.nums['ps'] = {_0="۰",_1="۱",_2="۲",_3="۳",_4="۴",_5="۵",_6="۶",_7="۷",_8="۸",_9="۹"}

p.timezones = {}
p.timezones['utc'] = {0,0,0,0,"UTC","UTC"}
p.timezones['asia/kabul'] = {12600,16200,80,234,"AFG","AFGHA"}

function p.date(frame)
    local jformat = frame.args[1]
    local jtype = frame.args[2]
    local timezone = frame.args[3]
    local jlang = frame.args[4]
    local timestamp = frame.args[5]
    local out = ''
    if jtype == nil then
        jtype = 'gregorian'
    end
    if timezone == nil then
        timezone = 'UTC'
    end
    if jlang == nil then
        jlang = 'en'
    end
    if timestamp == nil then
        timestamp = os.time()
    end
    local abbr = timezone
    local offset = ""
    local diff_greenwich = ""
    local diff_greenwich_colon = ""
    local jday_of_year = math.floor(os.date('%j',timestamp))
    local tz_data = p.fix_timezone(timestamp,jday_of_year,timezone)
    timestamp = tz_data[1]
    abbr = tz_data[2]
    offset = tz_data[3]
    diff_greenwich = tz_data[4]
    diff_greenwich_colon = tz_data[5]
    local jdate = mw.text.split(os.date('%Y %m %d %w %r %j',timestamp)," ")
    local jyear = jdate[1]
    local jmonth = jdate[2]
    local jday = jdate[3]
    local jweekday = jdate[4]
    local jtime = mw.text.split(jdate[5],":")
    local j_am_pm = jdate[6]
    local jday_of_year = math.floor(jdate[7])
    local jhour = math.floor(jtime[1])
    local jhour2 = jhour
    if j_am_pm == "PM" then
        jhour2 = jhour2 + 12
    elseif jhour2 == 12 then
        jhour2 = 0
    end
    local jmin = jtime[2]
    local jsec = jtime[3]
    if jtype == 'jalali' then
        jdate = p.gregorian_to_jalali(jyear,jmonth,jday)
        jyear = jdate[1]
        jmonth = jdate[2]
        jday = jdate[3]
    end
    local ignore = false
    for i = 1, string.len(jformat) do
        local c = string.sub(jformat, i, i)
        if ignore == true then
            out = out .. c
            ignore = false
        elseif c == "Y" then
            out = out .. jyear
        elseif c == "F" then
            out = out .. p.month_to_name1(jmonth,jtype,jlang)
        elseif c == "j" then
            out = out .. math.floor(jday)
        elseif c == "l" then
            out = out .. p.weekday_to_name1(jweekday,jlang)
        elseif c == "g" then
            out = out .. math.floor(jhour)
        elseif c == "i" then
            out = out .. p.padzero(jmin)
        elseif c == "s" then
            out = out .. p.padzero(jsec)
        elseif c == "A" then
            out = out .. p.localize_am_pm1(j_am_pm,jlang)
        elseif c == "a" then
            out = out .. p.localize_am_pm2(j_am_pm,jlang)
        elseif c == "z" then
            out = out .. jday_of_year
        elseif c == "N" then
            out = out .. jweekday
        elseif c == "w" then
            out = out .. jweekday - 1
        elseif c == "d" then
            out = out .. p.padzero(jday)
        elseif c == "n" then
            out = out .. math.floor(jmonth)
        elseif c == "m" then
            out = out .. p.padzero(jmonth)
        elseif c == "h" then
            out = out .. p.padzero(jhour)
        elseif c == "G" then
            out = out .. math.floor(jhour2)
        elseif c == "H" then
            out = out .. p.padzero(jhour2)
        elseif c == "T" then
            out = out .. abbr
        elseif c == "Z" then
            out = out .. offset
        elseif c == "O" then
            out = out .. diff_greenwich
        elseif c == "P" then
            out = out .. diff_greenwich_colon
        elseif c == "\\" then
            ignore = true
        else
            out = out .. c
        end
    end
    return p.localize_nums(out,jlang)
end

function p.time()
    return os.time()
end

function p.padzero(num)
    num = tostring(num)
    if string.len(num) == 1 then
        num = "0" .. num
    end
    return num
end

function p.fix_timezone(timestamp,day_of_year,timezone)
    local abbr = timezone
    local offset = 0
    local diff_greenwich = "+0000"
    local diff_greenwich_colon = "+00:00"
    if p.timezones[mw.ustring.lower(timezone)] ~= nil then
        local tz = p.timezones[mw.ustring.lower(timezone)]
        if day_of_year >= tz[3] and day_of_year <= tz[4] then
            timestamp = timestamp + tz[2]
            abbr = tz[5]
            offset = tz[2]
            diff_greenwich_data = p.make_diff_greenwich(tz[2])
            diff_greenwich = diff_greenwich_data[1]
            diff_greenwich_colon = diff_greenwich_data[2]
        else
            timestamp = timestamp + tz[1]
            abbr = tz[6]
            offset = tz[2]
            diff_greenwich_data = p.make_diff_greenwich(tz[1])
            diff_greenwich = diff_greenwich_data[1]
            diff_greenwich_colon = diff_greenwich_data[2]
        end
    end
    return {timestamp,abbr,offset,diff_greenwich,diff_greenwich_colon}
end

function p.make_diff_greenwich(diff_in_seconds)
    local diff_greenwich = ""
    local diff_greenwich_colon = ""
    if diff_in_seconds >= 0 then
        diff_greenwich = diff_greenwich .. "+"
        diff_greenwich_colon = diff_greenwich_colon .. "+"
    else
        diff_greenwich = diff_greenwich .. "-"
        diff_greenwich_colon = diff_greenwich_colon .. "-"
        diff_in_seconds = diff_in_seconds + (diff_in_seconds * 2)
    end
    local hour = p.padzero(math.floor(diff_in_seconds/3600))
    extra_minutes = p.padzero((diff_in_seconds - (math.floor(diff_in_seconds/3600) * 3600)) / 60)
    diff_greenwich = diff_greenwich .. hour .. extra_minutes
    diff_greenwich_colon = diff_greenwich_colon .. hour .. ":" .. extra_minutes
    return {diff_greenwich,diff_greenwich_colon}
end

function p.localize_nums(str,lang)
    local out = ''
    for i = 1, string.len(str) do
        local c = string.sub(str, i, i)
        if p.nums[lang] ~= nil and p.nums[lang]["_"..c] ~= nil then
            out = out .. p.nums[lang]["_"..c]
        else
            out = out .. c
        end
    end
    return out
end

function p.localize_am_pm1(str,lang)
    if str == "AM" then
        return p.am_pm1[lang][1]
    else
        return p.am_pm1[lang][2]
    end
end

function p.localize_am_pm2(str,lang)
    if str == "AM" then
        return p.am_pm2[lang][1]
    else
        return p.am_pm2[lang][2]
    end
end

function p.weekday_to_name1(day,lang)
    return p.weekdays1[lang][math.floor(day)+1]
end

function p.month_to_name1(month,jtype,lang)
    return p.months1[jtype][lang][math.floor(month)]
end

function p.gregorian_to_jalali(g_y,g_m,g_d,jmod)
    if jmod == nil then 
        jmod = ''
    end
    d_4 = g_y % 4
    g_a = {0,0,31,59,90,120,151,181,212,243,273,304,334}
    doy_g = g_a[math.floor(g_m)+1] + g_d
    if d_4 == 0 and g_m > 2 then
        doy_g = doy_g + 1
    end
    d_33 = math.floor(((g_y-16)%132)*0.0305)
    if d_33==3 or d_33 < (d_4 - 1) or d_4 == 0 then
        a = 286
    else
        a = 287
    end
    if (d_33 == 1 or d_33 == 2) and (d_33 == d_4 or d_4 == 1) then
        b = 78
    elseif (d_33==3 and d_4==0) then
        b = 80
    else
        b = 79
    end
    if math.floor((g_y-10)/63) == 30 then
        a = a - 1
        b = b + 1
    end
    if doy_g > b then
        jy= g_y - 621
        doy_j = doy_g - b
    else
        jy = g_y - 622
        doy_j = doy_g + a
    end
    if doy_j < 187 then
        jm = math.floor((doy_j-1)/31)
        jd = doy_j - (31 * jm)
        jm = jm + 1
    else
        jm = math.floor((doy_j-187)/30)
        jd = doy_j - 186 - (jm * 30)
        jm = jm + 7
    end
    if jmod == '' then
        return {jy,jm,jd}
    else
        return tostring(jy) .. tostring(mod) .. tostring(jm) .. tostring(mod) .. tostring(jd)
    end
end

return p