יחידה:תאריך: הבדלים בין גרסאות בדף

יצירת דף עם התוכן "function parseStrDate(frame) local dateStr = frame.args[1] local dates = { ['ינואר']= 1, ['פברואר']= 2, ['מרץ']= 3, ['אפריל']= 4,..."
 
מאין תקציר עריכה
שורה 1: שורה 1:
function parseStrDate(frame)
function parseStrDate(dateStr, dateType)
local dateStr = frame.args[1]
--local dateStr = frame.args[1]
--local dateType = frame.args[2]
local dates = {
local dates = {
['ינואר']= 1,
['ינואר']= 1,
שורה 19: שורה 20:
-- replace month names to numbers
-- replace month names to numbers
for a in pairs(dates) do
for a in pairs(dates) do
dateStr = mw.ustring.gsub(dateStr, 'ב'..a, dates[a])  
dateStr = mw.ustring.gsub(dateStr, 'ב?'..a, dates[a])  
end
end
-- if there are alhpabet chars return nil (unexpected character)
-- if there are alhpabet chars return nil (unexpected character)
if mw.ustring.find(dateStr, '%a') then
assert(not mw.ustring.find(dateStr, '%a'), "Unexpected format")
return nil
end
 
 
local parts = mw.text.split(dateStr,' ')
local parts = mw.text.split(dateStr,' ')
local res
if dateType == 'TS' then
for i, part in pairs(parts) do
if not res then
res=part
else
res=part..'-'..res
end
end
else
-- dmy date
if #parts==3 then
if dateType=='Y' then
res = tonumber(parts[3])
elseif dateType=='M' then
res = tonumber(parts[2])
assert(res<13 and res>0, "Month must be in range 1-12")
elseif dateType=='D' then
res = tonumber(parts[1])
assert(res<32 and res>0, "Day must be in range 1-31")
end
elseif  #parts==2 then -- MY date
if dateType=='Y' then
res = tonumber(parts[2])
elseif dateType=='M' then
res = tonumber(parts[1])
assert(res<13 and res>0, "Month must be in range 1-12")
end
elseif #parts==1 then --Y date
if dateType=='Y' then
res = tonumber(parts[1])
end
else
error("Unexpected date format")
end
end
return res
end
function parseDateRange(dateRangeStr, diffFormat)
local parts = mw.text.split(dateRangeStr,' +- +')
assert(#parts==2 or #parts==1, "Date range expected format is from - to or from (e.g from - now)")
-- parse dates
local res1 = parseStrDate(parts[1], 'TS' )
local year1, month1, day1 = unpack(mw.text.split(res1, '-'))
--assign default value
month1 = month1 or 1
day1 = day1 or 1
local t1 = os.time({
year = year1,--year1,
month = month1,
day = day1
})
local t2
if #parts==2 then
local res2 = parseStrDate( parts[2], 'TS' )
local year2, month2, day2 = unpack(mw.text.split(res2, '-'))
month2 = month2 or 1
day2 = day2 or 1
t2 = os.time({
year = year2,
month = month2,
day = day2
})
else
t2 = os.time()
end
local lang = mw.getContentLanguage()
return lang:formatDuration(os.difftime (t2, t1), diffFormat)
end
function parseDateRangeSafe(frame)
local diffFormat = {'years', 'days'}
if frame.args[2] == 'ימים' then
diffFormat = {'days'}
elseif frame.args[2] == 'שנים' then
diffFormat = {'years'}
end
local success, res = pcall(parseDateRange, frame.args[1], diffFormat)
if success then
return res
else
return '<span class="scribunto-error">שגיאת תאריך: '..res..'</span>[[קטגוריה:דפים עם שגיאות טכניות]]'
end
end
function parseStrDateSafe(frame)
local dateType = frame.args[2]
local dateType = frame.args[2]
local res
if dateType =='שנה' then
if dateType=='שנה' then
dateType = 'Y'
res = parts[3]
elseif dateType=='חודש' then
elseif dateType=='חודש' then
res = parts[2]
dateType = 'M'
elseif dateType=='יום' then
elseif dateType=='יום' then
res = parts[1]
dateType='D'
end
local success, res = pcall( parseStrDate, frame.args[1], dateType )
if success then
return res
else
return '<span class="scribunto-error">שגיאת תאריך: '..res..'</span>[[קטגוריה:דפים עם שגיאות טכניות]]'
end
end
return res
end
end


return {
return {
['חשב'] = parseStrDate
['חשב'] = parseStrDateSafe,
['חשב טווח'] =  parseDateRangeSafe
}
}