יחידה:תאריך: הבדלים בין גרסאות בדף
מראה
מאין תקציר עריכה |
מ מניעת חישוב כשאין מספיק פרטים |
||
| שורה 99: | שורה 99: | ||
local origMonth=month1 | local origMonth=month1 | ||
--assign default value | --assign default value | ||
day1 = day1 or 1 | day1 = day1 or 1 | ||
local t2 | local t2 | ||
| שורה 119: | שורה 114: | ||
end | end | ||
day2 = day2 or 1 | day2 = day2 or 1 | ||
t2 = os.time({ | |||
else | |||
t2 = os.time() | |||
end | |||
if ((month1 and not month2 and not t2) or (not month1 and month2)) then | |||
return '' -- Ambiguous date range | |||
end | |||
month1 = month1 or 1 | |||
month2 = month2 or 1 | |||
local t1 = os.time({ | |||
year = year1, | |||
month = month1, | |||
day = day1 | |||
}) | |||
t2=t2 or os.time({ | |||
year = year2, | year = year2, | ||
month = month2, | month = month2, | ||
day = day2 | day = day2 | ||
}) | }) | ||
local dif=os.difftime (t2, t1) | local dif=os.difftime (t2, t1) | ||
if inclusive then | if inclusive then | ||
| שורה 215: | שורה 222: | ||
else | else | ||
return frame.args['error'] or '<span class="scribunto-error">שגיאת תאריך: '..res..'</span>[[קטגוריה:דפים עם שגיאות בתאריך]]' | return frame.args['error'] or '<span class="scribunto-error">שגיאת תאריך: '..res..'</span>[[קטגוריה:דפים עם שגיאות בתאריך]]' | ||
end | end | ||
end | end | ||
גרסה מ־22:33, 19 בספטמבר 2014
יחידה זו מיועדת לביצוע פעולות נפוצות על תאריכים.
- #חשב - מקבלת תאריך טקסטואלי ופורמט ומחלצת את מהתאריך את הפורמט הרצוי
- #חשב טווח - מקבלת טווח תאריכים ומחשבת את ההפרש ביניהם
חשב
| פורמט | דוגמה | תוצאה |
|---|---|---|
| יום | {{#invoke:תאריך|חשב|[[3 בפברואר]] [[2013]]|יום}} | 3 |
| יום | {{#invoke:תאריך|חשב|[[3 בפברואר]] [[2013]]{{הערה|א}}|יום}} | שגיאת תאריך: יחידה:תאריך:31: Unexpected format |
| חודש | {{#invoke:תאריך|חשב|[[3 בפברואר]] [[2013]]|חודש}} | 2 |
| שנה | {{#invoke:תאריך|חשב|[[3 בפברואר]] [[2013]]|שנה}} | 2013 |
| שנה | {{#invoke:תאריך|חשב|[[פברואר]] [[2013]]|שנה}} | 2013 |
| TS | {{#invoke:תאריך|חשב|[[3 בפברואר]] [[2013]]|TS}} | 2013-2-3 |
הערות:
- פורמט TS ניתן להעביר לפונקציות זמן של הוראות תנאי.
- ההמרה מתעלמת מסוגריים מרובעות של קישורים
- ניתן להוסיף פרמטר error והתוכן שלו יוצג במקרה של שגיאה בתאריך. (רצוי להכניס במקרה כזה את הדף לקטגוריה כלשהי שתאפשר מעקב אחרי דפים לא תקינים. ברירת המחדל היא קטגוריה:דפים עם שגיאות בתאריך)
חשב טווח
| דוגמה | תוצאה | |
|---|---|---|
| פורמט ברירת מחדל: אוטומטי | {{#invoke:תאריך|חשב טווח|[[3 בפברואר]] [[1947]] - [[15 בספטמבר]] [[2013]]}} | |
| פורמט ברירת מחדל: אוטומטי (דוגמה עם שנים וסימן מינוס) | {{#invoke:תאריך|חשב טווח|1947 - 2013}} | שגיאת תאריך: mw.lua:143: field 'day' missing in date table |
| פורמט ברירת מחדל: אוטומטי (דוגמה עם שנים וסימן קו מפריד) | {{#invoke:תאריך|חשב טווח|1947–2013}} | שגיאת תאריך: יחידה:תאריך:66: attempt to compare nil with number |
| שנים | {{#invoke:תאריך|חשב טווח|[[3 בפברואר]] [[1947]] - [[15 בספטמבר]] [[2013]]|שנים}} | |
| גיל | {{#invoke:תאריך|חשב טווח|1 במרץ 1922 - 4 בנובמבר 1995|גיל}} | |
| גיל | {{#invoke:תאריך|חשב טווח|1 במרץ 1922{{הערה|ב}} - 4 בנובמבר 1995|גיל}} | שגיאת תאריך: יחידה:תאריך:31: Unexpected format |
| גיל | {{#invoke:תאריך|חשב טווח|1 במרץ 1994{{הערה|ב}} - 4 בנובמבר 1995|גיל}} | שגיאת תאריך: יחידה:תאריך:31: Unexpected format |
| גיל | {{#invoke:תאריך|חשב טווח|1 במרץ 1993{{הערה|ב}} - 4 בנובמבר 1995|גיל}} | שגיאת תאריך: יחידה:תאריך:31: Unexpected format |
| מספר | {{#invoke:תאריך|חשב טווח|1 במרץ 1922 - 4 בנובמבר 1995|מספר}} | |
| מספר | {{#invoke:תאריך|חשב טווח|1 במרץ 1922{{הערה|ב}} - 4 בנובמבר 1995|מספר}} | שגיאת תאריך: יחידה:תאריך:31: Unexpected format |
| מספר | {{#invoke:תאריך|חשב טווח|1 במרץ 1994{{הערה|ב}} - 4 בנובמבר 1995|מספר}} | שגיאת תאריך: יחידה:תאריך:31: Unexpected format |
| מספר | {{#invoke:תאריך|חשב טווח|1 במרץ 1993{{הערה|ב}} - 4 בנובמבר 1995|מספר}} | שגיאת תאריך: יחידה:תאריך:31: Unexpected format |
| שנים לפנה"ס | {{#invoke:תאריך|חשב טווח|[[3 בפברואר]] 1900 לפנה"ס - [[15 בספטמבר]] [[2013]]|שנים}} | |
| ימים | {{#invoke:תאריך|חשב טווח|[[3 בפברואר]] [[1947]] - [[15 בספטמבר]] [[2013]]|ימים}} | |
| הפרש | {{#invoke:תאריך|חשב טווח|[[3 בפברואר]] [[1947]] - [[15 בספטמבר]] [[2013]]|הפרש}} | |
| ללא תאריך יעד | {{#invoke:תאריך|חשב טווח|[[3 בפברואר]] [[1947]]|שנים}} | 79 שנים |
| לא כולל היום האחרון | {{#invoke:תאריך|חשב טווח|5 ביוני 1967 - 10 ביוני 1967}} | |
| כולל היום האחרון | {{#invoke:תאריך|חשב טווח|5 ביוני 1967 - 10 ביוני 1967|כולל=כן}} | |
| כולל היום האחרון | {{#invoke:תאריך|חשב טווח|14 ספטמבר 2014 - 14 ספטמבר 2014|כולל=כן}} | |
| שנה ויום | {{#invoke:תאריך|חשב טווח|14 ספטמבר 2014 - 15 ספטמבר 2015|כולל=כן}} |
הערות:
- כאשר לא ניתן תאריך יעד תאריך היעד נקבע כזמן הנוכחי
- הפרש - מציין את הפרש הזמנים בשניות וללא מילים. ניתן להשתמש בערך המוחזר בחלוקה מתאימה לקבלת יחידות זמן אחרות.
- ניתן להוסיף פרמטר כולל=כן, ואז נלקח בחשבון גם היום האחרון. כשמדובר על טווח זמן ארוך והפורמט נבחר אוטומטי או שמכיל שנים, מתקבלת תוצאה מקורבת.
- ניתן להוסיף פרמטר error והתוכן שלו יוצג במקרה של שגיאה בטווח התאריכים. (רצוי להכניס במקרה כזה את הדף לקטגוריה כלשהי שתאפשר מעקב אחרי דפים לא תקינים. ברירת המחדל היא קטגוריה:דפים עם שגיאות בתאריך)
- אם לא מצוין פורמט, הוא נבחר אוטומטית:
- עד ל-3 שבועות הוא מוצג בימים
- עד שנה הוא מוצג בימים ושבועות
- עד ל-10 שנים בשנים ושבועות
- מעבר לזה רק בשנים
בדיקות
דוגמאות לשימוש ביחידה ופלט לדוגמה מודגמים ביחידה:תאריך/בדיקות.
function parseStrDate(dateStr, dateType)
local dates = {
['ינואר']= 1,
['פברואר']= 2,
['מרץ']= 3,
['אפריל']= 4,
['מאי']= 5,
['יוני']= 6,
['יולי']= 7,
['אוגוסט']= 8,
['ספטמבר']= 9,
['אוקטובר']= 10,
['נובמבר']= 11,
['דצמבר']= 12
}
-- Remove instances of [ and ]
dateStr = mw.ustring.gsub( dateStr, "[%[%]]", "" )
-- Remove footnotes & directionality markers
dateStr = mw.ustring.gsub(dateStr, "(\127UNIQ[^\127]+QINU\127)", "")
dateStr = mw.ustring.gsub(dateStr, "‏","")
dateStr = mw.ustring.gsub(dateStr, "‎","")
mw.log('תאריך' .. ":" .. dateStr)
-- BC to minus
dateStr = mw.ustring.gsub( dateStr, "([0-9]+) לפנה\"ס" , "-%1")
-- replace month names to numbersץ The month number will be 10000001..10000012,
-- in order to facilitate testing later on.
for a in pairs(dates) do
dateStr = mw.ustring.gsub(dateStr, 'ב?'..a, dates[a]+1e7)
end
-- if there are alphabet chars return nil (unexpected character)
assert(not mw.ustring.find(dateStr, '%a'), "Unexpected format")
local parts = mw.text.split(dateStr,' ')
local res
if #parts==3 then -- DMY date
local ye=tonumber(parts[3])
local mo=tonumber(parts[2])-1e7
local da=tonumber(parts[1])
assert(ye<1e7, "Could not recognize year")
assert(mo<13 and mo>0, "Could not recognize month number")
assert(da<32 and da>0, "Wrong date format")
if dateType=='Y' then
res = ye
elseif dateType=='M' then
res = mo
elseif dateType=='D' then
res = da
elseif dateType == 'TS' then
res = ye.."-"..mo.."-"..da
end
elseif #parts==2 then -- MY date
local ye=tonumber(parts[2])
local mo=tonumber(parts[1]) - 1e7
assert(ye<1e7, "Could not recognize year")
assert(mo<13 and mo>0, "Could not recognize month number")
if dateType=='Y' then
res = ye
elseif dateType=='M' then
res = mo
elseif dateType == 'TS' then
res = ye.."-"..mo
end
elseif #parts==1 then --Y date
local ye=tonumber(parts[1])
assert(ye<1e7, "Could not recognize year")
if dateType=='Y' then
res = ye
elseif dateType == 'TS' then
res = ye
end
else
error("Unexpected date format")
end
return res
end
function parseDateRange(dateRangeStr, diffFormat, inclusive )
-- remove footnotes
dateRangeStr = mw.ustring.gsub(dateRangeStr, "(\127UNIQ[^\127]+QINU\127)", "")
dateRangeStr = mw.ustring.gsub(dateRangeStr, "‏","")
mw.log("טווח תאריכים:" .. dateRangeStr)
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
if mw.ustring.sub( res1, 0, 1 ) == '-' then
year1, month1, day1 = unpack(mw.text.split(mw.ustring.sub( res1, 2 ), '-'))
year1 = -(tonumber(year1))
else
year1, month1, day1 = unpack(mw.text.split( res1, '-'))
end
local origDay=day1
local origMonth=month1
--assign default value
day1 = day1 or 1
local t2
if #parts==2 then
local res2 = parseStrDate( parts[2], 'TS' )
local year2, month2, day2
if mw.ustring.sub( res2, 0, 1 ) == '-' then
year2, month2, day2 = unpack(mw.text.split(mw.ustring.sub( res2, 2 ), '-'))
year2 = -(tonumber(year2))
else
year2, month2, day2 = unpack(mw.text.split( res2, '-'))
end
day2 = day2 or 1
else
t2 = os.time()
end
if ((month1 and not month2 and not t2) or (not month1 and month2)) then
return '' -- Ambiguous date range
end
month1 = month1 or 1
month2 = month2 or 1
local t1 = os.time({
year = year1,
month = month1,
day = day1
})
t2=t2 or os.time({
year = year2,
month = month2,
day = day2
})
local dif=os.difftime (t2, t1)
if inclusive then
local hasYears = (diffFormat=='auto')
local Alang = mw.getContentLanguage()
local readableInterval = Alang:getDurationIntervals(dif, {'years', 'days'})
readableInterval['days'] = readableInterval['days'] or 0
readableInterval['years'] = readableInterval['years'] or 0
for i=1,#diffFormat do if (diffFormat[i]=='years') then hasYears=true end end
--for intervals of around year
if hasYears and readableInterval['years']>0 and readableInterval['days']<30 then
-- around
dif = dif-readableInterval['days']*(60*60*24)
origDay=nil
else
dif = dif+60*60*24 -- include last day
end
end
if diffFormat=="auto" then
local diffDays = dif/(60*60*24)
if diffDays<7*3 then diffFormat = { 'days' }
elseif diffDays<364 then diffFormat = {'weeks', 'days'}
elseif diffDays%365<7*3 and diffDays<10*365 then diffFormat = {'years', 'days'}
elseif diffDays<10*365 then diffFormat = {'years', 'weeks'}
else diffFormat = {'years'}
end
end
if diffFormat=="raw" then
return dif
else
local lang = mw.getContentLanguage()
return lang:formatDuration(dif, diffFormat)
end
end
function parseDateRangeSafe(frame)
local diffFormat = 'auto'
if frame.args[2] == 'ימים' then
diffFormat = {'days'}
elseif frame.args[2] == 'שנים' then
diffFormat = {'years'}
elseif frame.args[2] == "הפרש" then
diffFormat = "raw"
elseif frame.args[2] == "גיל" then
diffFormat = {'years'}
end
local inclusive= (frame.args["כולל"]=="כן")
local success, res = pcall(parseDateRange, frame.args[1], diffFormat, inclusive)
if success then
local str=res
-- the following translations are needed because the underline function
-- local format is wierd
str= mw.ustring.gsub(str,"וגם ([0-9])","ו-%1")
str= mw.ustring.gsub(str,"וגם ([א-ת])","ו%1");
if frame.args[2] == "גיל" then
str= mw.ustring.gsub(str," שנים","")
end
return str
else
return frame.args['error'] or '<span class="scribunto-error">שגיאת תאריך: '..res..'</span>[[קטגוריה:דפים עם שגיאות בתאריך]]'
end
end
function parseStrDateSafe(frame)
local dateType = frame.args[2]
if dateType =='שנה' then
dateType = 'Y'
elseif dateType=='חודש' then
dateType = 'M'
elseif dateType=='יום' then
dateType='D'
end
local success, res = pcall( parseStrDate, frame.args[1], dateType )
if success then
return res
else
return frame.args['error'] or '<span class="scribunto-error">שגיאת תאריך: '..res..'</span>[[קטגוריה:דפים עם שגיאות בתאריך]]'
end
end
return {
['חשב'] = parseStrDateSafe,
['חשב טווח'] = parseDateRangeSafe
}