Module:Hebrew year
Appearance
Documentation for this module may be created at Module:Hebrew year/doc
-- Module:Hebrew year
-- Year pages of Chabadpedia (English): intro sentence, "Year Count" facts and a full-year calendar.
-- English counterpart of the Hebrew templates פתיח שנה, מניין השנה, כותרת לוח שנה עברי, MIHY, תחתית לוח שנה עברי.
local p = {}
local EPOCH = -1373427 -- R.D. of 1 Tishrei, year 1
local function fdiv(a, b) return math.floor(a / b) end
local function isLeap(y) return ((7 * y + 1) % 19) < 7 end
local function elapsed(y)
local months = fdiv(235 * y - 234, 19)
local parts = 12084 + 13753 * months
local days = 29 * months + fdiv(parts, 25920)
if (3 * (days + 1)) % 7 < 3 then days = days + 1 end
return days
end
local function delay(y)
local ny0, ny1, ny2 = elapsed(y - 1), elapsed(y), elapsed(y + 1)
if ny2 - ny1 == 356 then return 2 end
if ny1 - ny0 == 382 then return 1 end
return 0
end
local function newYear(y) return EPOCH + elapsed(y) + delay(y) end
local function yearLength(y) return newYear(y + 1) - newYear(y) end
-- Gregorian / Julian from R.D.
local function gregFromRD(rd)
local d0 = rd - 1
local n400 = fdiv(d0, 146097); local d1 = d0 % 146097
local n100 = fdiv(d1, 36524); local d2 = d1 % 36524
local n4 = fdiv(d2, 1461); local d3 = d2 % 1461
local n1 = fdiv(d3, 365)
local year = 400 * n400 + 100 * n100 + 4 * n4 + n1
if not (n100 == 4 or n1 == 4) then year = year + 1 end
local function gLeap(y) return (y % 4 == 0 and y % 100 ~= 0) or y % 400 == 0 end
local function gRD(y, m, d)
local r = 365 * (y - 1) + fdiv(y - 1, 4) - fdiv(y - 1, 100) + fdiv(y - 1, 400) + fdiv(367 * m - 362, 12)
if m > 2 then r = r + (gLeap(y) and -1 or -2) end
return r + d
end
local prior = rd - gRD(year, 1, 1)
local corr = 0
if rd >= gRD(year, 3, 1) then corr = gLeap(year) and 1 or 2 end
local month = fdiv(12 * (prior + corr) + 373, 367)
local day = rd - gRD(year, month, 1) + 1
return year, month, day
end
local function julFromRD(rd)
local JEPOCH = -1 -- R.D. of Julian Jan 1, 1 CE
local approx = fdiv(4 * (rd - JEPOCH) + 1464, 1461)
local year = approx
if approx <= 0 then year = approx - 1 end
local function jLeap(y) return y % 4 == (y > 0 and 0 or 3) end
local function jRD(y, m, d)
local yy = y < 0 and y + 1 or y
local r = JEPOCH - 1 + 365 * (yy - 1) + fdiv(yy - 1, 4) + fdiv(367 * m - 362, 12)
if m > 2 then r = r + (jLeap(y) and -1 or -2) end
return r + d
end
local prior = rd - jRD(year, 1, 1)
local corr = 0
if rd >= jRD(year, 3, 1) then corr = jLeap(year) and 1 or 2 end
local month = fdiv(12 * (prior + corr) + 373, 367)
local day = rd - jRD(year, month, 1) + 1
return year, month, day
end
local GREGORIAN_START = 577736 -- 15 October 1582
local function civil(rd)
if rd < GREGORIAN_START then return julFromRD(rd) end
return gregFromRD(rd)
end
local MONTHNAMES = { 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' }
local MONTHABBR = { 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' }
local WEEKDAYS = { 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Shabbos' }
local function civilText(rd)
local y, m, d = civil(rd)
local yt = y > 0 and tostring(y) or (tostring(1 - y) .. ' BCE')
return MONTHNAMES[m] .. ' ' .. d .. ', ' .. yt
end
local function weekday(rd) return rd % 7 end -- 0 = Sunday
-- months of a year, starting with Tishrei
local function months(y)
local len = yearLength(y)
local leap = isLeap(y)
local chesh = (len % 10 == 5) and 30 or 29
local kislev = (len % 10 == 3) and 29 or 30
local t = {
{ key = 'Tishrei', name = 'Tishrei', page = 'Tishrei', days = 30 },
{ key = 'Cheshvan', name = 'Cheshvan', page = 'Cheshvan', days = chesh },
{ key = 'Kislev', name = 'Kislev', page = 'Kislev', days = kislev },
{ key = 'Teves', name = 'Teves', page = 'Teves', days = 29 },
{ key = 'Shevat', name = 'Shevat', page = 'Month of Shvat', days = 30 },
}
if leap then
table.insert(t, { key = 'AdarI', name = 'Adar I', page = 'Adar', day = 'Adar', days = 30 })
table.insert(t, { key = 'Adar', name = 'Adar II', page = 'Adar', day = 'Adar', days = 29 })
else
table.insert(t, { key = 'Adar', name = 'Adar', page = 'Adar', day = 'Adar', days = 29 })
end
for _, m in ipairs({ { 'Nissan', 30 }, { 'Iyar', 29 }, { 'Sivan', 30 }, { 'Tammuz', 29 }, { 'Menachem Av', 30 }, { 'Elul', 29 } }) do
table.insert(t, { key = m[1], name = m[1], page = m[1], days = m[2] })
end
local rd = newYear(y)
for _, m in ipairs(t) do
m.day = m.day or m.name
m.start = rd
rd = rd + m.days
end
return t
end
-- Hebrew numerals
local function gematria(n)
local ones = { 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט' }
local tens = { 'י', 'כ', 'ל', 'מ', 'נ', 'ס', 'ע', 'פ', 'צ' }
local hundreds = { 'ק', 'ר', 'ש', 'ת' }
local s = ''
while n >= 400 do s = s .. 'ת'; n = n - 400 end
if n >= 100 then s = s .. hundreds[fdiv(n, 100)]; n = n % 100 end
if n == 15 then s = s .. 'טו'; n = 0
elseif n == 16 then s = s .. 'טז'; n = 0 end
if n >= 10 then s = s .. tens[fdiv(n, 10)]; n = n % 10 end
if n > 0 then s = s .. ones[n] end
return s
end
local function withMarks(s)
if mw.ustring.len(s) == 1 then return s .. '׳' end
return mw.ustring.sub(s, 1, -2) .. '״' .. mw.ustring.sub(s, -1)
end
local function hebrewYearText(y)
local th = fdiv(y, 1000)
local rest = y % 1000
local short = withMarks(gematria(rest))
local full = (th > 0 and (gematria(th) .. "'") or '') .. short
return full, short
end
local function currentYear()
local now = os.time()
local rd = fdiv(now, 86400) + 719163 -- R.D. of 1970-01-01 is 719163
local y = fdiv(rd - EPOCH, 366) + 1
while newYear(y + 1) <= rd do y = y + 1 end
while newYear(y) > rd do y = y - 1 end
return y
end
local function getYear(frame)
local a = frame.args[1] or (frame:getParent() and frame:getParent().args[1]) or ''
local y = tonumber(mw.text.trim(a))
if not y then
y = tonumber(mw.title.getCurrentTitle().text:match('^(%d%d%d%d)$'))
end
return y
end
local function link(page, text)
if text and text ~= page then return '[[' .. page .. '|' .. text .. ']]' end
return '[[' .. page .. ']]'
end
local function yearLink(y)
local t = mw.title.new(tostring(y))
if t and t.exists then return '[[' .. y .. ']]' end
return tostring(y)
end
-- keviah letters: day of Rosh Hashanah, length, day of Pesach
local DAYLETTER = { [0] = 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז' }
local function keviah(y)
local rh = weekday(newYear(y))
local len = yearLength(y)
local r = len % 10
local lenLetter = (r == 3) and 'ח' or ((r == 4) and 'כ' or 'ש')
local m = months(y)
local nissan
for _, mm in ipairs(m) do if mm.key == 'Nissan' then nissan = mm end end
local pesach = weekday(nissan.start + 14)
return DAYLETTER[rh] .. lenLetter .. DAYLETTER[pesach], rh, lenLetter, pesach
end
function p.intro(frame)
local y = getYear(frame)
if not y then return '' end
local full, short = hebrewYearText(y)
local cur = currentYear()
local startRD = newYear(y)
local endRD = newYear(y + 1) - 1
local julian = startRD < GREGORIAN_START
local nav = '<div class="hatnote" style="text-align:center;">« ' .. yearLink(y - 1) .. ' · ' .. "'''" .. y .. "'''" .. ' · ' .. yearLink(y + 1) .. ' »</div>\n'
local began = (y > cur) and 'will begin' or ((y == cur) and 'began' or 'began')
local ended = (y < cur) and 'ended' or 'will end'
local s = "'''" .. y .. "''' (" .. full .. ', abbreviated ' .. short .. ') is a Hebrew year that ' .. began
.. ' on ' .. link('1 Tishrei') .. ' (' .. civilText(startRD)
.. (julian and ', according to the [[Julian calendar]] then in use' or '') .. ') and ' .. ended
.. ' on ' .. link('29 Elul') .. ' (' .. civilText(endRD) .. ').'
return nav .. s
end
local ORDINAL = { 'first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh' }
local function nth(n)
local r100 = n % 100
if r100 >= 11 and r100 <= 13 then return n .. 'th' end
local r = n % 10
return n .. ((r == 1 and 'st') or (r == 2 and 'nd') or (r == 3 and 'rd') or 'th')
end
local ANNIV = {
{ 5458, 'the birth of [[The Baal Shem Tov]]' },
{ 5520, 'the passing of [[The Baal Shem Tov]]' },
{ 5532, 'the founding of [[Chassidus Chabad]]' },
{ 5533, 'the passing of [[The Maggid of Mezeritch]]' },
{ 5505, 'the birth of [[The Alter Rebbe]]' },
{ 5573, 'the passing of [[The Alter Rebbe]]' },
{ 5534, 'the birth of [[The Mitteler Rebbe]]' },
{ 5588, 'the passing of [[The Mitteler Rebbe]]' },
{ 5549, 'the birth of [[The Tzemach Tzedek]]' },
{ 5626, 'the passing of [[The Tzemach Tzedek]]' },
{ 5594, 'the birth of [[The Rebbe Maharash]]' },
{ 5643, 'the passing of [[The Rebbe Maharash]]' },
{ 5621, 'the birth of [[The Rebbe Rashab]]' },
{ 5680, 'the passing of [[The Rebbe Rashab]]' },
{ 5640, 'the birth of [[The Rebbe Rayatz]]' },
{ 5710, 'the passing of [[The Rebbe Rayatz]] and the beginning of [[The Rebbe]]\'s leadership' },
{ 5662, 'the birth of [[The Rebbe]]' },
{ 5689, 'the wedding of [[The Rebbe]]' },
{ 5711, 'the Rebbe\'s formal acceptance of the leadership ([[The Rebbe Accepting The Nesius|nesius]])' },
}
function p.count(frame)
local y = getYear(frame)
if not y then return '' end
local code, rh, lenLetter, pesach = keviah(y)
local out = {}
local function add(s) table.insert(out, '* ' .. s) end
if rh == 1 then
add('This year, [[Rosh Hashanah]] falls on Monday and Tuesday.')
elseif rh == 2 then
add('This year, [[Rosh Hashanah]] falls on Tuesday and Wednesday.')
elseif rh == 4 then
add('This year, [[Rosh Hashanah]] falls on Thursday and Friday, immediately followed by [[Shabbos]], so that there are three consecutive holy days.')
elseif rh == 6 then
add('This year, [[Rosh Hashanah]] falls on Shabbos and Sunday; when Rosh Hashanah falls on Shabbos, the [[Tekias Shofar|shofar]] is not blown.')
end
local lenText = (lenLetter == 'ח') and 'deficient (ח), so Cheshvan and Kislev both have 29 days'
or ((lenLetter == 'כ') and 'regular (כ), so Cheshvan has 29 days and Kislev has 30 days'
or 'complete (ש), so Cheshvan and Kislev both have 30 days')
local pesachDay = (pesach == 6) and 'on Shabbos' or ('on ' .. WEEKDAYS[pesach + 1])
add('The year\'s arrangement (kviyus) is ' .. withMarks(code) .. ': it begins on ' .. WEEKDAYS[rh + 1]
.. ' (' .. DAYLETTER[rh] .. '), it is ' .. lenText .. ', and [[Passover|Pesach]] falls ' .. pesachDay
.. ' (' .. DAYLETTER[pesach] .. ').')
local startRD, len = newYear(y), yearLength(y)
local firstSat = startRD + ((6 - weekday(startRD)) % 7)
local shabbosim = fdiv(startRD + len - 1 - firstSat, 7) + 1
if isLeap(y) then
add('This is a leap year (with two months of Adar); it is ' .. len .. ' days long, with ' .. shabbosim .. ' Shabbosim.')
else
add('This is a regular (non-leap) year; it is ' .. len .. ' days long, with ' .. shabbosim .. ' Shabbosim.')
end
local sh = y % 7
local cyc = ((y - 1) % 19) + 1
local cycNo = fdiv(y - 1, 19) + 1
local shText = (sh == 0) and 'This is a [[Shemitah]] year'
or ('This is the ' .. ORDINAL[sh] .. ' year of the [[Shemitah]] cycle')
add(shText .. ', and year ' .. cyc .. ' of 19 in the ' .. nth(cycNo) .. ' cycle of leap years.')
if code == 'זחא' or code == 'בשא' or code == 'החא' then
add('This year there is a [[Purim Meshulash]]: 15 Adar falls on Shabbos, so Purim in Jerusalem is celebrated over three days, from Friday, [[14 Adar]], until Sunday, [[16 Adar]].')
end
if y > 5716 and (code == 'בחג' or code == 'זשג' or code == 'השג' or code == 'זחג') then
add('This year, [[Lag BaOmer]] falls on Sunday; in such years a [[The Parade at the Rebbe\'s|parade]] was held at [[The Rebbe]]\'s.')
end
for _, a in ipairs(ANNIV) do
local d = y - a[1]
if d > 0 and (d % 10 == 0 or d % 50 == 25) then
add('This year marks ' .. d .. ' years since ' .. a[2] .. ', in ' .. yearLink(a[1]) .. '.')
end
end
return table.concat(out, '\n')
end
local function special(y, mkey, d, rd, mlist)
local wd = weekday(rd)
if mkey == 'Tishrei' then
if d == 1 or d == 2 or d == 10 or d == 15 or d == 16 or d == 22 or d == 23 then return 'yt' end
if d >= 17 and d <= 21 then return 'chm' end
if (d == 3 and wd ~= 6) or (d == 4 and wd == 0) then return 'fast' end
elseif mkey == 'Kislev' then
if d >= 25 then return 'chm' end
if d == 19 or d == 20 or d == 10 then return 'ch' end
elseif mkey == 'Teves' then
local kislevDays
for _, m in ipairs(mlist) do if m.key == 'Kislev' then kislevDays = m.days end end
local chanEnd = (kislevDays == 30) and 2 or 3
if d <= chanEnd then return 'chm' end
if d == 10 then return 'fast' end
elseif mkey == 'Shevat' then
if d == 15 then return 'minor' end
if d == 10 then return 'ch' end
elseif mkey == 'AdarI' then
if d == 14 or d == 15 then return 'minor' end
elseif mkey == 'Adar' then
if d == 14 or d == 15 then return 'chm' end
if (d == 13 and wd ~= 6) or (d == 11 and wd == 4) then return 'fast' end
elseif mkey == 'Nissan' then
if d == 15 or d == 16 or d == 21 or d == 22 then return 'yt' end
if d >= 17 and d <= 20 then return 'chm' end
if d == 11 then return 'ch' end
elseif mkey == 'Iyar' then
if d == 18 then return 'minor' end
elseif mkey == 'Sivan' then
if d == 6 or d == 7 then return 'yt' end
elseif mkey == 'Tammuz' then
if (d == 17 and wd ~= 6) or (d == 18 and wd == 0) then return 'fast' end
if d == 12 or d == 13 then return 'ch' end
elseif mkey == 'Menachem Av' then
if (d == 9 and wd ~= 6) or (d == 10 and wd == 0) then return 'fast' end
if d == 15 then return 'minor' end
elseif mkey == 'Elul' then
if d == 18 then return 'ch' end
end
if wd == 6 then return 'shabbos' end
return nil
end
local COLORS = {
yt = '#f6d78b', chm = '#fbe9b7', fast = '#dde3ea', minor = '#e6f2dc', ch = '#e3dcf2', shabbos = '#eef3f8',
}
function p.calendar(frame)
local y = getYear(frame)
if not y then return '' end
local mlist = months(y)
local wrap = mw.html.create('div'):css('display', 'flex'):css('flex-wrap', 'wrap'):css('gap', '0.8em'):css('margin', '0.5em 0')
for _, m in ipairs(mlist) do
local tbl = wrap:tag('table'):addClass('wikitable')
:css('margin', '0'):css('text-align', 'center'):css('font-size', '85%'):css('width', '15.5em')
local gy1, gm1 = civil(m.start)
local gy2, gm2 = civil(m.start + m.days - 1)
local range = MONTHABBR[gm1] .. (gm1 ~= gm2 and ('–' .. MONTHABBR[gm2]) or '') .. ' '
.. (gy1 == gy2 and tostring(gy1) or (gy1 .. '–' .. gy2))
tbl:tag('tr'):tag('th'):attr('colspan', 7):css('background', '#d6e4f0')
:wikitext(link(m.page, m.name) .. ' ' .. y .. '<br /><small>' .. range .. '</small>')
local hr = tbl:tag('tr')
for _, w in ipairs({ 'Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sh' }) do
hr:tag('th'):css('padding', '0 0.2em'):wikitext(w)
end
local row = tbl:tag('tr')
local col = weekday(m.start)
for _ = 1, col do row:tag('td') end
for d = 1, m.days do
local rd = m.start + d - 1
local _, _, gd = civil(rd)
local cell = row:tag('td'):css('padding', '0.1em 0.2em'):css('line-height', '1.1')
local k = special(y, m.key, d, rd, mlist)
if k then cell:css('background', COLORS[k]) end
cell:wikitext('[[' .. d .. ' ' .. m.day .. '|' .. d .. ']]<br /><small style="color:#666;">' .. gd .. '</small>')
col = col + 1
if col == 7 and d < m.days then
row = tbl:tag('tr'); col = 0
end
end
end
local legend = mw.html.create('div'):css('font-size', '85%'):css('margin-top', '0.3em')
local items = {
{ 'yt', 'Yom Tov' }, { 'chm', 'Chol HaMoed, Chanukah, Purim' }, { 'fast', 'Fast day' },
{ 'minor', 'Minor festival (Tu BiShvat, Purim Katan, Lag BaOmer, Tu B\'Av)' }, { 'ch', 'Chassidic festival' }, { 'shabbos', 'Shabbos' },
}
for _, it in ipairs(items) do
legend:tag('span'):css('display', 'inline-block'):css('margin-right', '1em')
:tag('span'):css('display', 'inline-block'):css('width', '1em'):css('height', '1em')
:css('background', COLORS[it[1]]):css('border', '1px solid #aaa'):css('vertical-align', 'middle'):done()
:wikitext(' ' .. it[2])
end
local note = '<div style="font-size:85%;">Each day links to its date page; the small number is the civil date'
.. ((newYear(y) < GREGORIAN_START) and ' (Julian calendar)' or '') .. '. Festivals are shown as observed outside Eretz Yisroel.</div>'
return tostring(wrap) .. tostring(legend) .. note
end
return p