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

מתוך אוצר הספרים היהודי השיתופי
קפיצה לניווט קפיצה לחיפוש
אין תקציר עריכה
אין תקציר עריכה
שורה 16: שורה 16:
   local months =  (year - 1 - years) * 235 / 19 -- מספר החודשים בכל המחזורים מבריאת העולם
   local months =  (year - 1 - years) * 235 / 19 -- מספר החודשים בכל המחזורים מבריאת העולם
   for y = 1, years do -- הוספת החודשים של המחזור הנוכחי לסכום
   for y = 1, years do -- הוספת החודשים של המחזור הנוכחי לסכום
     if is_leap(y)
     if p.is_leap(y)
       then months = months + 13
       then months = months + 13
       else months = months + 12 end
       else months = months + 12 end
שורה 31: שורה 31:
function p.first_day(year)
function p.first_day(year)
   -- מקבלת שנה עברית בתור מספר ומחזירה את היום שבו יחול ראש השנה
   -- מקבלת שנה עברית בתור מספר ומחזירה את היום שבו יחול ראש השנה
   local molad = moladTishrei(year)
   local molad = p.moladTishrei(year)
   local day, hours, parts = molad[1], molad[2], molad[3]
   local day, hours, parts = molad[1], molad[2], molad[3]
   local old_molad = hours > 18 or (hours == 18 and parts > 0) -- מולד זקן
   local old_molad = hours > 18 or (hours == 18 and parts > 0) -- מולד זקן
   local g_t_rd = day == 3 and (hours > 9 or (hours == 9 and parts >= 204)) and not is_leap(year) -- ג'-ט'-ר"ד בשנה פשוטה
   local g_t_rd = day == 3 and (hours > 9 or (hours == 9 and parts >= 204)) and not p.is_leap(year) -- ג'-ט'-ר"ד בשנה פשוטה
   local b_tv_tkpt = is_leap(year - 1) and day == 2 and (hours > 15 or (hours == 15 and parts >= 589)) -- ב'-ט"ו-תקפ"ט אחר עיבור
   local b_tv_tkpt = p.is_leap(year - 1) and day == 2 and (hours > 15 or (hours == 15 and parts >= 589)) -- ב'-ט"ו-תקפ"ט אחר עיבור
   if old_molad or g_t_rd or b_tv_tkpt then day = day + 1 end
   if old_molad or g_t_rd or b_tv_tkpt then day = day + 1 end
   if day == 8 then day = 2 end
   if day == 8 then day = 2 end
שורה 44: שורה 44:
function p.year_type(year)
function p.year_type(year)
   -- מקבלת שנה עברית בתור מספר ומחזירה את הסוג של השנה כמחרוזת
   -- מקבלת שנה עברית בתור מספר ומחזירה את הסוג של השנה כמחרוזת
   local difference = (first_day(year + 1) + 7 - first_day(year)) % 7
   local difference = (p.first_day(year + 1) + 7 - p.first_day(year)) % 7
   if difference == 0 then difference = 7 end
   if difference == 0 then difference = 7 end
   if difference == 3 or (is_leap(year) and difference == 5) then return "חסרה" end
   if difference == 3 or (p.is_leap(year) and difference == 5) then return "חסרה" end
   if difference == 4 or difference == 6 then return "כסדרה" end
   if difference == 4 or difference == 6 then return "כסדרה" end
   if (difference == 5 and not is_leap(year)) or difference == 7 then return "שלמה" end
   if (difference == 5 and not p.is_leap(year)) or difference == 7 then return "שלמה" end
end
end


שורה 62: שורה 62:
   end
   end
   if month == "חשוון" then  
   if month == "חשוון" then  
     if year_type(year) == "שלמה" then return 30 else return 29 end
     if p.year_type(year) == "שלמה" then return 30 else return 29 end
   end
   end
   if month == "כסלו" then
   if month == "כסלו" then
     if year_type(year) == "חסרה" then return 29 else return 30 end
     if p.year_type(year) == "חסרה" then return 29 else return 30 end
   end
   end
end
end
שורה 72: שורה 72:
   -- מקבלת שנה עברית בתור מספר וחודש בתור מחרוזת ומחזירה את החודש הקודם או הבא
   -- מקבלת שנה עברית בתור מספר וחודש בתור מחרוזת ומחזירה את החודש הקודם או הבא
   local months = {"תשרי", "חשון", "כסלו", "טבת", "שבט", "אדר", "ניסן", "אייר", "סיון", "תמוז", "אב", "אלול"}
   local months = {"תשרי", "חשון", "כסלו", "טבת", "שבט", "אדר", "ניסן", "אייר", "סיון", "תמוז", "אב", "אלול"}
   if is_leap(year) then
   if p.is_leap(year) then
     months = {"תשרי", "חשון", "כסלו", "טבת", "שבט", "אדר א'", "אדר ב'", "ניסן", "אייר", "סיון", "תמוז", "אב", "אלול"}
     months = {"תשרי", "חשון", "כסלו", "טבת", "שבט", "אדר א'", "אדר ב'", "ניסן", "אייר", "סיון", "תמוז", "אב", "אלול"}
   end
   end
שורה 89: שורה 89:
   -- מקבלת שנה עברית בתור מספר וחודש בתור מחרוזת ויום בחודש בתור מספר ומחזירה את היום הקודם (ברירת מחדל) או הבא
   -- מקבלת שנה עברית בתור מספר וחודש בתור מחרוזת ויום בחודש בתור מספר ומחזירה את היום הקודם (ברירת מחדל) או הבא
   if previous then
   if previous then
     if day == 1 then return days_a_month(year, return_month(year, month, true)) else return day - 1 end
     if day == 1 then return p.days_a_month(year, p.return_month(year, month, true)) else return day - 1 end
     else if day == days_a_month(year, month) then return 1 else return day + 1 end
     else if day == p.days_a_month(year, month) then return 1 else return day + 1 end
   end
   end
   return error("שגיאת תאריך")
   return error("שגיאת תאריך")
שורה 97: שורה 97:
function p.next_month(year, month)
function p.next_month(year, month)
   -- מקבלת שנה עברית בתור מספר וחודש בתור מחרוזת ומחזירה טבלה עם החודש הבא
   -- מקבלת שנה עברית בתור מספר וחודש בתור מחרוזת ומחזירה טבלה עם החודש הבא
   month = return_month(year, month, false)
   month = p.return_month(year, month, false)
   if month == "תשרי" then year = year + 1 end
   if month == "תשרי" then year = year + 1 end
   return {year, month}
   return {year, month}
שורה 104: שורה 104:
function p.previous_month(year, month)
function p.previous_month(year, month)
   -- מקבלת שנה עברית בתור מספר וחודש בתור מחרוזת ומחזירה טבלה עם החודש הקודם
   -- מקבלת שנה עברית בתור מספר וחודש בתור מחרוזת ומחזירה טבלה עם החודש הקודם
   month = return_month(year, month, true)
   month = p.return_month(year, month, true)
   if month == "אלול" then year = year - 1 end
   if month == "אלול" then year = year - 1 end
   return {year, month}
   return {year, month}
שורה 111: שורה 111:
function p.next_date(year, month, day)
function p.next_date(year, month, day)
   -- מקבלת תאריך מלא ומחזירה טבלה עם התאריך המלא הבא
   -- מקבלת תאריך מלא ומחזירה טבלה עם התאריך המלא הבא
   day = p.return_day(year, month, day, false)
   day = p.p.return_day(year, month, day, false)
   if day == 1 then
   if day == 1 then
     month = return_month(year, month, false)
     month = p.return_month(year, month, false)
     if month == "תשרי" then year = year + 1 end
     if month == "תשרי" then year = year + 1 end
   end
   end
שורה 121: שורה 121:
function p.previous_date(year, month, day)
function p.previous_date(year, month, day)
   -- מקבלת תאריך מלא ומחזירה טבלה עם התאריך המלא הקודם
   -- מקבלת תאריך מלא ומחזירה טבלה עם התאריך המלא הקודם
   new_day = return_day(year, month, day, true)
   new_day = p.return_day(year, month, day, true)
   if day == 1 then
   if day == 1 then
     if month == "תשרי" then year = year - 1 end
     if month == "תשרי" then year = year - 1 end
     month = return_month(year, month, true)
     month = p.return_month(year, month, true)
   end
   end
   return {year, month, new_day}
   return {year, month, new_day}

גרסה מ־08:46, 5 ביולי 2024

היחידה מכילה פונקציות מועילות רבות לחישוב תאריכים עבריים, אבל היא מחזירה רק 4 פונקציות, שממומשות בתבניות {{חודש קודם}}, {{חודש הבא}}, {{תאריך קודם}}, {{תאריך הבא}}, ראו שם הסברים.

אם יש צורך בפונקציות נוספות כגון בדיקה האם שנה היא מעוברת, חישוב מולדות, בדיקת היום שבו חל ראש השנה, החזרת מספר הימים בחודש נתון, בדיקה מה הסוג של שנה נתונה - ניתן לבקש זאת ממשתמש:עמד.


local p = {}

function p.is_leap(year)
  -- הפונקציה מקבלת שנה עברית בתור מספר ומחזירה ערך בוליאני האם היא מעוברת
  local leap_year = {0, 3, 6, 8, 11, 14, 17, 19} -- השנים המעוברות בתוך מחזור של 19 שנה
  for _, value in pairs(leap_year) do
    if (year % 19) == value then
      return true end
  end
  return false
end

function p.moladTishrei(year)
  -- הפונקציה מקבלת שנה עברית בתור מספר ומחזירה את המולד של תשרי בתור טבלה
  local years = (year - 1) % 19 -- מספר השנים במחזור שלפני השנה הזו
  local months =  (year - 1 - years) * 235 / 19 -- מספר החודשים בכל המחזורים מבריאת העולם
  for y = 1, years do -- הוספת החודשים של המחזור הנוכחי לסכום
    if p.is_leap(y)
      then months = months + 13
      else months = months + 12 end
  end
  local all_parts = (months * 39673 + 57444) % 181440 -- הכפלת מספר החודשים בא'-י"ב-תשצ"ג, הוספת ב'-ה'-ר"ד וחלוקה במספר החלקים בשבעה ימים
  local parts = all_parts % 1080
  local all_hours = (all_parts - parts) / 1080
  local hours = all_hours % 24
  local days =  (all_hours - hours) / 24
  if days == 0 then days = 7 end
  return {days, hours, parts}
end

function p.first_day(year)
  -- מקבלת שנה עברית בתור מספר ומחזירה את היום שבו יחול ראש השנה
  local molad = p.moladTishrei(year)
  local day, hours, parts = molad[1], molad[2], molad[3]
  local old_molad = hours > 18 or (hours == 18 and parts > 0) -- מולד זקן
  local g_t_rd = day == 3 and (hours > 9 or (hours == 9 and parts >= 204)) and not p.is_leap(year) -- ג'-ט'-ר"ד בשנה פשוטה
  local b_tv_tkpt = p.is_leap(year - 1) and day == 2 and (hours > 15 or (hours == 15 and parts >= 589)) -- ב'-ט"ו-תקפ"ט אחר עיבור
  if old_molad or g_t_rd or b_tv_tkpt then day = day + 1 end
  if day == 8 then day = 2 end
  if day == 1 or day == 4 or day == 6 then day = day + 1 end -- לא אד"ו ראש
  return day
end

function p.year_type(year)
  -- מקבלת שנה עברית בתור מספר ומחזירה את הסוג של השנה כמחרוזת
  local difference = (p.first_day(year + 1) + 7 - p.first_day(year)) % 7
  if difference == 0 then difference = 7 end
  if difference == 3 or (p.is_leap(year) and difference == 5) then return "חסרה" end
  if difference == 4 or difference == 6 then return "כסדרה" end
  if (difference == 5 and not p.is_leap(year)) or difference == 7 then return "שלמה" end
end

function p.days_a_month(year, month)
  -- מקבלת שנה עברית בתור מספר וחודש בתור מחרוזת ומחזירה את מספר הימים בו
  local full_months = {"תשרי", "שבט", "אדר א'", "ניסן", "סיוון", "אב"}
  local missing_months = {"טבת", "אדר", "אדר ב'", "אייר", "תמוז", "אלול"}
  for _, value in pairs(full_months) do
    if month == value then return 30 end
  end
  for _, value in pairs(missing_months) do
    if month == value then return 29 end
  end
  if month == "חשוון" then 
    if p.year_type(year) == "שלמה" then return 30 else return 29 end
  end
  if month == "כסלו" then
    if p.year_type(year) == "חסרה" then return 29 else return 30 end
  end
end

function p.return_month(year, month, previous)
  -- מקבלת שנה עברית בתור מספר וחודש בתור מחרוזת ומחזירה את החודש הקודם או הבא
  local months = {"תשרי", "חשון", "כסלו", "טבת", "שבט", "אדר", "ניסן", "אייר", "סיון", "תמוז", "אב", "אלול"}
  if p.is_leap(year) then
    months = {"תשרי", "חשון", "כסלו", "טבת", "שבט", "אדר א'", "אדר ב'", "ניסן", "אייר", "סיון", "תמוז", "אב", "אלול"}
  end
  for index, value in pairs(months) do
    if month == value then
      if previous then
        if month == "תשרי" then return "אלול" else return months[index - 1] end
        else if month == "אלול" then return "תשרי" else return months[index + 1] end
      end
    end
  end
  return error("שם חודש לא מוכר")
end

function p.return_day(year, month, day, previous)
  -- מקבלת שנה עברית בתור מספר וחודש בתור מחרוזת ויום בחודש בתור מספר ומחזירה את היום הקודם (ברירת מחדל) או הבא
  if previous then
    if day == 1 then return p.days_a_month(year, p.return_month(year, month, true)) else return day - 1 end
    else if day == p.days_a_month(year, month) then return 1 else return day + 1 end
  end
  return error("שגיאת תאריך")
end

function p.next_month(year, month)
  -- מקבלת שנה עברית בתור מספר וחודש בתור מחרוזת ומחזירה טבלה עם החודש הבא
  month = p.return_month(year, month, false)
  if month == "תשרי" then year = year + 1 end
  return {year, month}
end

function p.previous_month(year, month)
  -- מקבלת שנה עברית בתור מספר וחודש בתור מחרוזת ומחזירה טבלה עם החודש הקודם
  month = p.return_month(year, month, true)
  if month == "אלול" then year = year - 1 end
  return {year, month}
end

function p.next_date(year, month, day)
  -- מקבלת תאריך מלא ומחזירה טבלה עם התאריך המלא הבא
  day = p.p.return_day(year, month, day, false)
  if day == 1 then
    month = p.return_month(year, month, false)
    if month == "תשרי" then year = year + 1 end
  end
  return {year, month, day}
end

function p.previous_date(year, month, day)
  -- מקבלת תאריך מלא ומחזירה טבלה עם התאריך המלא הקודם
  new_day = p.return_day(year, month, day, true)
  if day == 1 then
    if month == "תשרי" then year = year - 1 end
    month = p.return_month(year, month, true)
  end
  return {year, month, new_day}
end

return p