<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://stockhub.co/index.php?action=history&amp;feed=atom&amp;title=Module%3ASandbox%2Fmaslen%2FNext_Hebrew_Date</id>
	<title>Module:Sandbox/maslen/Next Hebrew Date - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://stockhub.co/index.php?action=history&amp;feed=atom&amp;title=Module%3ASandbox%2Fmaslen%2FNext_Hebrew_Date"/>
	<link rel="alternate" type="text/html" href="https://stockhub.co/index.php?title=Module:Sandbox/maslen/Next_Hebrew_Date&amp;action=history"/>
	<updated>2026-04-21T23:12:25Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://stockhub.co/index.php?title=Module:Sandbox/maslen/Next_Hebrew_Date&amp;diff=146546&amp;oldid=prev</id>
		<title>imported&gt;Maslen at 03:07, 22 January 2015</title>
		<link rel="alternate" type="text/html" href="https://stockhub.co/index.php?title=Module:Sandbox/maslen/Next_Hebrew_Date&amp;diff=146546&amp;oldid=prev"/>
		<updated>2015-01-22T03:07:45Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;local p = {}&lt;br /&gt;
&lt;br /&gt;
-- Absolute dates&lt;br /&gt;
&lt;br /&gt;
-- &amp;quot;Absolute date&amp;quot; means the number of days elapsed since the Gregorian date&lt;br /&gt;
-- Sunday, December 31, 1 BC. (Since there was no year 0, the year following&lt;br /&gt;
-- 1 BC is 1 AD.) Thus the Gregorian date January 1, 1 AD is absolute date&lt;br /&gt;
-- number 1.&lt;br /&gt;
&lt;br /&gt;
--  Gregorian dates&lt;br /&gt;
&lt;br /&gt;
function LastDayOfGregorianMonth(month, year)&lt;br /&gt;
-- Compute the last date of the month for the Gregorian calendar.&lt;br /&gt;
  &lt;br /&gt;
  if month == 2 then&lt;br /&gt;
    if ((((year % 4) == 0) and ((year % 100) ~= 0)) or ((year % 400) == 0)) then&lt;br /&gt;
      return 29;&lt;br /&gt;
    else&lt;br /&gt;
      return 28;&lt;br /&gt;
    end&lt;br /&gt;
  elseif month == 2 or month == 4 or month == 6 or month == 9 or month == 11 then&lt;br /&gt;
    return 30;&lt;br /&gt;
  else &lt;br /&gt;
    return 31;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
GregorianDate = {}&lt;br /&gt;
GregorianDate.__index = GregorianDate&lt;br /&gt;
&lt;br /&gt;
function GregorianDate.create(month, day, year)&lt;br /&gt;
   local gregoriandate = {}             -- our new object&lt;br /&gt;
   setmetatable(gregoriandate,GregorianDate)  -- make GregorianDate handle lookup&lt;br /&gt;
   -- initialize our object&lt;br /&gt;
    gregoriandate.month = month --  1 == January, ..., 12 == December&lt;br /&gt;
    gregoriandate.day = day     -- 1..LastDayOfGregorianMonth(month, year)&lt;br /&gt;
    gregoriandate.year = year   -- &lt;br /&gt;
   return gregoriandate&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
function GregorianDateFromAbsolute(d)&lt;br /&gt;
  -- Computes the Gregorian date from the absolute date.&lt;br /&gt;
    &lt;br /&gt;
    -- Search forward year by year from approximate year&lt;br /&gt;
    local year = math.floor(d/366);&lt;br /&gt;
    while (d &amp;gt;= AbsoluteFromGregorianDate(GregorianDate.create(1,1,year+1))) do&lt;br /&gt;
      year = year + 1;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    -- Search forward month by month from January&lt;br /&gt;
    local month = 1;&lt;br /&gt;
    while (d &amp;gt; AbsoluteFromGregorianDate(GregorianDate.create(month, LastDayOfGregorianMonth(month,year), year))) do&lt;br /&gt;
      month = month + 1;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    local day = d - AbsoluteFromGregorianDate(GregorianDate.create(month,1,year)) + 1;&lt;br /&gt;
    &lt;br /&gt;
    return GregorianDate.create(month, day, year)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
function AbsoluteFromGregorianDate(gregoriandate)&lt;br /&gt;
-- Computes the absolute date from the Gregorian date.&lt;br /&gt;
    local N = gregoriandate.day;           -- days this month&lt;br /&gt;
    &lt;br /&gt;
    -- days in prior months this year&lt;br /&gt;
    local m = gregoriandate.month - 1;&lt;br /&gt;
    while (m &amp;gt; 0) do &lt;br /&gt;
      N = N + LastDayOfGregorianMonth(m, gregoriandate.year);&lt;br /&gt;
      m = m - 1;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    return (N                    -- days this year&lt;br /&gt;
       + 365 * (gregoriandate.year - 1)   -- days in previous years ignoring leap days&lt;br /&gt;
       + math.floor((gregoriandate.year - 1)/4)       -- Julian leap days before this year...&lt;br /&gt;
       - math.floor((gregoriandate.year - 1)/100)     -- ...minus prior century years...&lt;br /&gt;
       + math.floor((gregoriandate.year - 1)/400));   -- ...plus prior years divisible by 400&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-- Hebrew dates&lt;br /&gt;
&lt;br /&gt;
HebrewEpoch = -1373429 -- Absolute date of start of Hebrew calendar&lt;br /&gt;
&lt;br /&gt;
function HebrewLeapYear(year)&lt;br /&gt;
-- True if year is an Hebrew leap year&lt;br /&gt;
  if ((((7 * year) + 1) % 19) &amp;lt; 7)&lt;br /&gt;
  then&lt;br /&gt;
    return 1;&lt;br /&gt;
  else&lt;br /&gt;
    return 0;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
--Last month of Hebrew year.&lt;br /&gt;
function LastMonthOfHebrewYear(year)&lt;br /&gt;
  if (HebrewLeapYear(year) == 1) then&lt;br /&gt;
    return 13;&lt;br /&gt;
  else&lt;br /&gt;
    return 12;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
function HebrewCalendarElapsedDays(year)&lt;br /&gt;
-- Number of days elapsed from the Sunday prior to the start of the&lt;br /&gt;
-- Hebrew calendar to the mean conjunction of Tishri of Hebrew year.&lt;br /&gt;
  &lt;br /&gt;
  local MonthsElapsed =&lt;br /&gt;
    (235 * math.floor((year - 1) / 19))           -- Months in complete cycles so far.&lt;br /&gt;
    + (12 * ((year - 1) % 19))          -- Regular months in this cycle.&lt;br /&gt;
    + math.floor((7 * ((year - 1) % 19) + 1) / 19); -- Leap months this cycle&lt;br /&gt;
  local PartsElapsed = 204 + 793 * (MonthsElapsed % 1080);&lt;br /&gt;
  local HoursElapsed =&lt;br /&gt;
    5 + 12 * MonthsElapsed + 793 * math.floor(MonthsElapsed  / 1080)&lt;br /&gt;
    + math.floor(PartsElapsed / 1080);&lt;br /&gt;
  local ConjunctionDay = 1 + 29 * MonthsElapsed + math.floor(HoursElapsed / 24);&lt;br /&gt;
  local ConjunctionParts = 1080 * (HoursElapsed % 24) + PartsElapsed % 1080;&lt;br /&gt;
  local AlternativeDay = 0;&lt;br /&gt;
  if ((ConjunctionParts &amp;gt;= 19440)        -- If new moon is at or after midday,&lt;br /&gt;
      or (((ConjunctionDay % 7) == 2)    -- ...or is on a Tuesday...&lt;br /&gt;
          and (ConjunctionParts &amp;gt;= 9924)  -- at 9 hours, 204 parts or later...&lt;br /&gt;
          and (HebrewLeapYear(year)) == 0)   -- ...of a common year,&lt;br /&gt;
      or (((ConjunctionDay % 7) == 1)    -- ...or is on a Monday at...&lt;br /&gt;
          and (ConjunctionParts &amp;gt;= 16789) -- 15 hours, 589 parts or later...&lt;br /&gt;
          and (HebrewLeapYear(year - 1) == 1))) then -- at the end of a leap year&lt;br /&gt;
    -- Then postpone Rosh HaShanah one day&lt;br /&gt;
    AlternativeDay = ConjunctionDay + 1;&lt;br /&gt;
  else&lt;br /&gt;
    AlternativeDay = ConjunctionDay;&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  if (((AlternativeDay % 7) == 0)-- If Rosh HaShanah would occur on Sunday,&lt;br /&gt;
      or ((AlternativeDay % 7) == 3)     -- or Wednesday,&lt;br /&gt;
      or ((AlternativeDay % 7) == 5))    -- or Friday&lt;br /&gt;
    -- Then postpone it one (more) day&lt;br /&gt;
  then&lt;br /&gt;
    return (1+ AlternativeDay);&lt;br /&gt;
  else&lt;br /&gt;
    return AlternativeDay;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function DaysInHebrewYear(year) &lt;br /&gt;
-- Number of days in Hebrew year.&lt;br /&gt;
  &lt;br /&gt;
  return ((HebrewCalendarElapsedDays(year + 1)) -&lt;br /&gt;
          (HebrewCalendarElapsedDays(year)));&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function LongHeshvan(year) &lt;br /&gt;
-- True if Heshvan is long in Hebrew year.&lt;br /&gt;
  &lt;br /&gt;
  if ((DaysInHebrewYear(year) % 10) == 5)&lt;br /&gt;
  then&lt;br /&gt;
    return 1;&lt;br /&gt;
  else&lt;br /&gt;
    return 0;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function ShortKislev(year) &lt;br /&gt;
-- True if Kislev is short in Hebrew year.&lt;br /&gt;
  &lt;br /&gt;
  if ((DaysInHebrewYear(year) % 10) == 3)&lt;br /&gt;
  then&lt;br /&gt;
    return 1;&lt;br /&gt;
  else&lt;br /&gt;
    return 0;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
  &lt;br /&gt;
function LastDayOfHebrewMonth(month, year)&lt;br /&gt;
-- Last day of month in Hebrew year.&lt;br /&gt;
  &lt;br /&gt;
  if ((month == 2)&lt;br /&gt;
      or (month == 4)&lt;br /&gt;
      or (month == 6)&lt;br /&gt;
      or ((month == 8) and LongHeshvan(year) == 0)&lt;br /&gt;
      or ((month == 9) and ShortKislev(year) == 1)&lt;br /&gt;
      or (month == 10)&lt;br /&gt;
      or ((month == 12) and (HebrewLeapYear(year) == 0))&lt;br /&gt;
      or (month == 13))&lt;br /&gt;
  then&lt;br /&gt;
    return 29;&lt;br /&gt;
  else&lt;br /&gt;
    return 30;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
HebrewDate = {}&lt;br /&gt;
HebrewDate.__index = HebrewDate&lt;br /&gt;
&lt;br /&gt;
function HebrewDate.create(month, day, year)&lt;br /&gt;
   local hebrewdate = {}             -- our new object&lt;br /&gt;
   setmetatable(hebrewdate,HebrewDate)  -- make HebrewDate handle lookup&lt;br /&gt;
   -- initialize our object&lt;br /&gt;
    hebrewdate.month = month -- 1...&lt;br /&gt;
    hebrewdate.day = day     -- 1..LastMonthOfHebrewYear(year)&lt;br /&gt;
    hebrewdate.year = year   -- 1..LastDayOfHebrewMonth(month, year)     &lt;br /&gt;
   return hebrewdate&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
function HebrewDateFromAbsolute(d)&lt;br /&gt;
  -- Computes the Hebrew date from the absolute date.&lt;br /&gt;
    local year = math.floor((d + HebrewEpoch) / 366); -- Approximation from below.&lt;br /&gt;
    -- Search forward for year from the approximation.&lt;br /&gt;
    while (d &amp;gt;= AbsoluteFromHebrewDate(HebrewDate.create(7,1,year + 1)))&lt;br /&gt;
    do&lt;br /&gt;
      year = year + 1;&lt;br /&gt;
    end&lt;br /&gt;
    -- Search forward for month from either Tishri or Nisan.&lt;br /&gt;
    local month = 0&lt;br /&gt;
    if (d &amp;lt; AbsoluteFromHebrewDate(HebrewDate.create(1, 1, year))) then  &lt;br /&gt;
      month = 7;  --  Start at Tishri&lt;br /&gt;
    else&lt;br /&gt;
      month = 1;  --  Start at Nisan&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    while (d &amp;gt; AbsoluteFromHebrewDate(HebrewDate.create(month, (LastDayOfHebrewMonth(month,year)), year))) do&lt;br /&gt;
        month = month + 1;&lt;br /&gt;
      end&lt;br /&gt;
    -- Calculate the day by subtraction.&lt;br /&gt;
    local day = d - AbsoluteFromHebrewDate(HebrewDate.create(month, 1, year)) + 1;&lt;br /&gt;
    return HebrewDate.create(month, day, year)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
function AbsoluteFromHebrewDate(hebrewdate)&lt;br /&gt;
    -- Computes the absolute date of Hebrew date.&lt;br /&gt;
    local DayInYear = hebrewdate.day; -- Days so far this month.&lt;br /&gt;
    if (hebrewdate.month &amp;lt; 7) then -- Before Tishri, so add days in prior months&lt;br /&gt;
                     -- this year before and after Nisan.&lt;br /&gt;
      local m = 7;&lt;br /&gt;
      while (m &amp;lt;= (LastMonthOfHebrewYear(hebrewdate.year))) do&lt;br /&gt;
        DayInYear = DayInYear + LastDayOfHebrewMonth(m, hebrewdate.year);&lt;br /&gt;
        m = m + 1;&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      m = 1;&lt;br /&gt;
      while (m &amp;lt; hebrewdate.month) do&lt;br /&gt;
        DayInYear = DayInYear + LastDayOfHebrewMonth(m, hebrewdate.year);&lt;br /&gt;
        m = m + 1;&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
    else  -- Add days in prior months this year&lt;br /&gt;
      local m = 7;&lt;br /&gt;
      while (m &amp;lt; hebrewdate.month) do&lt;br /&gt;
        DayInYear = DayInYear + LastDayOfHebrewMonth(m, hebrewdate.year);&lt;br /&gt;
        m = m + 1;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    return (DayInYear +&lt;br /&gt;
            (HebrewCalendarElapsedDays(hebrewdate.year)-- Days in prior years.&lt;br /&gt;
             + HebrewEpoch));         -- Days elapsed before absolute date 1.&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
function find_gregorian_for_next_hebrew_date_occurrence(greg_year, greg_month, greg_day, heb_month, heb_day)&lt;br /&gt;
    local greg_absolute = AbsoluteFromGregorianDate(GregorianDate.create(greg_month, greg_day, greg_year))&lt;br /&gt;
    local hebrew_date = HebrewDateFromAbsolute(greg_absolute)&lt;br /&gt;
    &lt;br /&gt;
    local heb_year = hebrew_date.year&lt;br /&gt;
    &lt;br /&gt;
    -- Check if we already passed that date this year. If we have, increase the year by 1&lt;br /&gt;
    local this_years_hebrew_absolute = AbsoluteFromHebrewDate(HebrewDate.create(heb_month, heb_day, heb_year))&lt;br /&gt;
    if (greg_absolute &amp;gt; this_years_hebrew_absolute) then&lt;br /&gt;
        heb_year = heb_year + 1&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    -- Certain months only have 29 days. Advance years until we find a year with 30 days that month.&lt;br /&gt;
    if heb_day == 30 then&lt;br /&gt;
        while ((heb_month == 8) and not(LongHeshvan(heb_year))) or ((heb_month == 9) and ShortKislev(heb_year)) or ((heb_month == 12) and (HebrewLeapYear(heb_year) == 0)) do&lt;br /&gt;
            heb_year = heb_year + 1&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    -- The year we&amp;#039;re in has the date. Get the absolute and convert it back to a Gregorian date&lt;br /&gt;
    local absolute = AbsoluteFromHebrewDate(HebrewDate.create(heb_month, heb_day, heb_year))&lt;br /&gt;
    local gregorian = GregorianDateFromAbsolute(absolute)&lt;br /&gt;
    return gregorian&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function GregorianMonthToName(monthNumber)&lt;br /&gt;
    if monthNumber == 1 then&lt;br /&gt;
        return &amp;quot;January&amp;quot;&lt;br /&gt;
    elseif monthNumber == 2 then&lt;br /&gt;
        return &amp;quot;February&amp;quot;&lt;br /&gt;
    elseif monthNumber == 3 then&lt;br /&gt;
        return &amp;quot;March&amp;quot;&lt;br /&gt;
    elseif monthNumber == 4 then&lt;br /&gt;
        return &amp;quot;April&amp;quot;&lt;br /&gt;
    elseif monthNumber == 5 then&lt;br /&gt;
        return &amp;quot;May&amp;quot;&lt;br /&gt;
    elseif monthNumber == 6 then&lt;br /&gt;
        return &amp;quot;June&amp;quot;&lt;br /&gt;
    elseif monthNumber == 7 then&lt;br /&gt;
        return &amp;quot;July&amp;quot;&lt;br /&gt;
    elseif monthNumber == 8 then&lt;br /&gt;
        return &amp;quot;August&amp;quot;&lt;br /&gt;
    elseif monthNumber == 9 then&lt;br /&gt;
        return &amp;quot;September&amp;quot;&lt;br /&gt;
    elseif monthNumber == 10 then&lt;br /&gt;
        return &amp;quot;October&amp;quot;&lt;br /&gt;
    elseif monthNumber == 11 then&lt;br /&gt;
        return &amp;quot;November&amp;quot;&lt;br /&gt;
    elseif monthNumber == 12 then&lt;br /&gt;
        return &amp;quot;December&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function p.next_occurrence_of_hebrew_date_from_date(frame)&lt;br /&gt;
	local greg_year = tonumber(frame.args[1])&lt;br /&gt;
	local greg_month = tonumber(frame.args[2])&lt;br /&gt;
	local greg_day = tonumber(frame.args[3])&lt;br /&gt;
	local heb_month = tonumber(frame.args[4])&lt;br /&gt;
	local heb_day = tonumber(frame.args[5])&lt;br /&gt;
    gregorian = find_gregorian_for_next_hebrew_date_occurrence(greg_year, greg_month, greg_day, heb_month, heb_day)&lt;br /&gt;
    month_name = GregorianMonthToName(gregorian.month)&lt;br /&gt;
    return month_name..&amp;quot; &amp;quot;.. gregorian.day..&amp;quot;, &amp;quot;..gregorian.year&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function p.next_occurrence_of_hebrew_date(frame)&lt;br /&gt;
	local greg_year = tonumber(os.date(&amp;quot;%Y&amp;quot;))&lt;br /&gt;
	local greg_month = tonumber(os.date(&amp;quot;%m&amp;quot;))&lt;br /&gt;
	local greg_day = tonumber(os.date(&amp;quot;%d&amp;quot;))&lt;br /&gt;
	local heb_month = tonumber(frame.args[1])&lt;br /&gt;
	local heb_day = tonumber(frame.args[2])&lt;br /&gt;
    gregorian = find_gregorian_for_next_hebrew_date_occurrence(greg_year, greg_month, greg_day, heb_month, heb_day)&lt;br /&gt;
    month_name = GregorianMonthToName(gregorian.month)&lt;br /&gt;
    return month_name..&amp;quot; &amp;quot;.. gregorian.day..&amp;quot;, &amp;quot;..gregorian.year&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return p&lt;/div&gt;</summary>
		<author><name>imported&gt;Maslen</name></author>
	</entry>
</feed>