%------------------hebcal.sty------------------------------
%
%   TeX & LaTeX MACROS FOR COMPUTING HEBREW DATE FROM GREGORIAN ONE
%   ( Released:     Tammuz 12, 5751 -- June 24, 1991 )
%   Corrected: 15 January, 1992. Rama. 
%   by Michail Rozman, misha@iop.tartu.ew.su
%   Translated to Hebrew by Rama.
%
%   Please direct any comments, bugfixed, questions, etc.
%   to the address above.
%
%   TABLE OF CONTENTS
%
%   INTRODUCTION
%   REGISTERS, COMMANDS AND MACROS FOR HEBREW DATE FORMATTING
%   AUXILIARY MACROS
%   GREGORIAN PART
%   HEBREW PART
%

%   *******************************************
%   *              INTRODUCTION               *
%   *******************************************
%
%   The Hebrew calendar is inherently complicated:
%   It is lunisolar -- each year starts close to the
%   autumn equinox, but each month must strictly start
%   at a new moon.  Thus Hebrew calendar must be harmonized
%   simultaneously with both lunar and solar events.
%   In addition, for reasons of the religious practice, the
%   year cannot start on Sunday, Wednesday or Friday.
%
%   For the full description of Hebrew calendar and for
%   the list of references see:
%
%     Nachum Dershowitz and Edward M. Reingold,
%     "Calendrical Calculations", Software--Pract.Exper.,
%     vol. 20 (9), pp.899--928 (September 1990)
%     C translation of LISP programs from the above article
%     available from Mr. Wayne Geiser, geiser%pictel@uunet.uu.net
%
%   The 4th distribution ( July 1989 ) of hdate/hcal ( Hebrew
%   calendar programs similar to UNIX date/cal ) by
%   Mr. Amos Shapir, amos@shum.huji.ac.il , contains short
%   and very clear description of algorithms.
%
\catcode`@=11
%   *******************************************
%   *   REGISTERS, COMMAND, FORMATTING MACROS  *
%   *******************************************
%
%      The command \Hebrewtoday produces today's date for Hebrew
%   calendar.  It is similar to the \today command of the LETTER
%   style.  In addition three numerical registers \Hebrewday
%   \Hebrewmonth and \Hebrewyear are set.
%      For setting this registers without producing of date string
%   command \Hebrewsetreg can be used.
%      The command \Hebrewdate{Gday}{Gmonth}{Gyear} produces Hebrew
%   calendar date corresponding to Gregorian date Gday.Gmonth.Gyear.
%   Three numerical registers \Hebrewday \Hebrewmonth and
%   \Hebrewyear are set.
%      For converting arbitrary Gregorian date Gday.Gmonth.Gyear
%   to Hebrew date Hday.Hmonth.Hyear without producing date string
%   the command:
%   \HebrewFromGregorian{Gday}{Gmonth}{Gyear}{Hday}{Hmonth}{Hyear}
%   can be used.
%
\newcount\Hebrewday  \newcount\Hebrewmonth   \newcount\Hebrewyear
%
%
% \Hebrewtoday
% ============
% Today's date in hebrew calendar
\def\Hebrewtoday{%
    \HebrewFromGregorian{\day}{\month}{\year}
                        {\Hebrewday}{\Hebrewmonth}{\Hebrewyear}%
    \HebrewDayName{\Hebrewday} \HebrewMonthName{\Hebrewmonth}{\Hebrewyear}, zyp"a% 
%   \HebrewMonthName{\Hebrewmonth}{\Hebrewyear} \number\Hebrewday, \ %
%    \number\Hebrewyear%
}%
\let\hebrewtoday=\Hebrewtoday
%
% \Hebrewsetreg
% ============
% Set registers: today's date in hebrew calendar
\def\Hebrewsetreg{%
    \HebrewFromGregorian{\day}{\month}{\year}                     
                        {\Hebrewday}{\Hebrewmonth}{\Hebrewyear}%
}%
%
% \Hebrewdate{Gday}{Gmonth}{Gyear}
% ================================
% Hebrew calendar date corresponding to Gregorian Gday.Gmonth.Gyear
\def\Hebrewdate #1#2#3{%
    \HebrewFromGregorian{#1}{#2}{#3}
                        {\Hebrewday}{\Hebrewmonth}{\Hebrewyear}%
    \HebrewDayName{\Hebrewday} \HebrewMonthName{\Hebrewmonth}{\Hebrewyear}, $\number\Hebrewyear$%         
%  \HebrewMonthName{\Hebrewmonth}{\Hebrewyear} \number\Hebrewday, \ %
%  \number\Hebrewyear%
}%
%
% TO PREPARE ANOTHER LANGUAGE VERSION CHANGE HERE !!!
%
% \HebrewMonthName{month}{year}
% ===============================
% Name of month in the `year'
\def\HebrewMonthName #1#2{%
    \ifnum #1 = 7%
    \CheckLeapHebrewYear{#2}%
        \if@HebrewLeap `cx a' \else `cx \fi%
    \else%
        \ifcase #1%
            Dummy     \or%
            zyxi      \or%
            gyeo      \or%
            kqle      \or%
            haz       \or%
            yah       \or%
            `cx `'    \or%
            `cx a'    \or%
            piqo      \or%
            `iix      \or%
            qieo      \or%
            znef      \or%
            `a        \or%
            `lel %
        \fi                                  %
    \fi                                      %
}%
%
% \HebrewDayName{day}
% ===============================
% Name of day in Hebrew letters.
%
\def\HebrewDayName #1{
    \ifcase #1% 
       Dummy \or%
       %Dummy \or%
       `' \or%
       a' \or%
       b' \or%
       c' \or%
       d' \or%
       e' \or%
       f' \or%
       g' \or%
       h' \or%
       i' \or%
       i`' \or%
       ia' \or%
       ib' \or%
       ic' \or%
       he' \or%
       hf' \or%
       if' \or%
       ig' \or%
       ih' \or%
       k' \or%
       k`' \or%
       ka' \or%
       kb' \or%
       kc' \or%
       kd' \or%
       ke' \or%
       kf' \or%
       kg' \or%
       kh' \or%
       l' \or%
       l`'%
  \fi
}
%\def\HebrewMonthName #1#2{%
    %\ifnum #1 = 7%
    %\CheckLeapHebrewYear{#2}%
        %\if@HebrewLeap Adar II \else Adar \fi%
    %\else%
        %\ifcase #1%
            %Dummy     \or%
            %Tishri    \or%
            %Heshvan   \or%
            %Kislev    \or%
            %Tebeth    \or%
            %Shebat    \or%
            %Adar I    \or%
            %Adar II   \or%
            %Nisan     \or%
            %Iyar      \or%
            %Sivan     \or%
            %Tammuz    \or%
            %Av        \or%
            %Ellul%
        %\fi                                  %
    %\fi                                      %
%}%
%   *******************************************
%   *           AUXILIARY MACROS               *
%   *******************************************
%
\newcount\@common
%
% \Remainder{a}{b}{c}
% ===================
%  c = a%b == a - b(a/b)
\def\Remainder #1#2#3{%
    #3 = #1%                   %  c = a
    \divide #3 by #2%          %  c = a/b
    \multiply #3 by -#2%       %  c = -b(a/b)
    \advance #3 by #1%         %  c = a - b(a/b)
}%
%
\newif\if@Divisible
%
% \CheckIfDivisible{a}{b}
% ==================
% Set \@Divisibletrue if   a%b == 0
\def\CheckIfDivisible #1#2{%
    {%
      \countdef\tmp = 0%          %  \tmp == \count0 - temporary variable
      \Remainder{#1}{#2}{\tmp}%
      \ifnum \tmp = 0%
          \global\@Divisibletrue%
      \else%
          \global\@Divisiblefalse%
      \fi%
    }%
}%

%   *******************************************
%   *           GREGORIAN PART                *
%   *******************************************
%
\newif\if@GregorianLeap
%
% \IfGregorianLeap{year}
% ======================
% Conditional which is true if Gregorian `year' is
% a leap year:
% ( ( year%4 == 0 ) && ( year%100 != 0 ) || ( year%400 == 0 ) )
\def\IfGregorianLeap #1{%
    \CheckIfDivisible{#1}{4}%
    \if@Divisible%
        \CheckIfDivisible{#1}{100}%
        \if@Divisible%
            \CheckIfDivisible{#1}{400}%
            \if@Divisible%
                \@GregorianLeaptrue%
            \else%
                \@GregorianLeapfalse%
            \fi%
        \else%
            \@GregorianLeaptrue%
        \fi%
    \else%
        \@GregorianLeapfalse%
    \fi%
    \if@GregorianLeap%
}%
%
% \GregorianDaysInPriorMonths{month}{year}{days}
% ===============================
% Number of days in months prior to `month' in the `year'
\def\GregorianDaysInPriorMonths #1#2#3{%
    {%
        #3 = \ifcase #1%
               0 \or %          % no month number 0
               0 \or  31 \or  59 \or  90 \or 120 \or 151 \or %
             181 \or 212 \or 243 \or 273 \or 304 \or 334%
        \fi%
        \IfGregorianLeap{#2}%           % in leap year ...
            \ifnum #1 > 2%              % if month after February ...
                \advance #3 by 1%       % add leap day
            \fi%
        \fi%
        \global\@common = #3%
    }%
    #3 = \@common%
}%
%
% \GregorianDaysInPriorYears{year}{days}
% ===============================
% Number of days since 01.01.01 in years prior to  the `year'
\def\GregorianDaysInPriorYears #1#2{%
     {%
         \countdef\tmpc = 4%               % \tmpc==\count4
         \countdef\tmpb = 2%               % \tmpb==\count2
         \tmpb = #1%
         \advance \tmpb by -1%
         \tmpc = \tmpb%                    % \tmpc = \tmpb = year-1
         \multiply \tmpc by 365%           % Days in prior years =
         #2 = \tmpc%                       % = 365*(year-1) ...
         \tmpc = \tmpb%
         \divide \tmpc by 4%               % \tmpc = (year-1)/4
         \advance #2 by \tmpc%             % ... plus Julian leap days ...
         \tmpc = \tmpb%                    %
         \divide \tmpc by 100%             % \tmpc = (year-1)/100
         \advance #2 by -\tmpc%            % ... minus century years ...
         \tmpc = \tmpb%
         \divide \tmpc by 400%             % \tmpc = (year-1)/400
         \advance #2 by \tmpc%             % ... plus 4-century years.
         \global\@common = #2%
    }%
    #2 = \@common%
}%
%
% \AbsoluteFromGregorian{day}{month}{year}{absdate}
% ======================
% Calculation of absolute date ( days since 01.01.01 ) from
% Gregorian date day.month.year
\def\AbsoluteFromGregorian #1#2#3#4{%
    {%
        \countdef\tmpd = 0%               % \tmpd==\count0
        #4 = #1%                          % days so far this month
        \GregorianDaysInPriorMonths{#2}{#3}{\tmpd}%
        \advance #4 by \tmpd%             % add days in prior months
        \GregorianDaysInPriorYears{#3}{\tmpd}%
        \advance #4 by \tmpd%             % add days in prior years
        \global\@common = #4%
    }%
    #4 = \@common%
}%
%
\newif\if@GregorianLeap
%
% \IfGregorianLeap{year}
% ======================
% Conditional which is true if Gregorian `year' is
% a leap year:
% ( ( year%4 == 0 ) && ( year%100 != 0 ) || ( year%400 == 0 ) )
\def\IfGregorianLeap #1{%
    \CheckIfDivisible{#1}{4}%
    \if@Divisible%
        \CheckIfDivisible{#1}{100}%
        \if@Divisible%
            \CheckIfDivisible{#1}{400}%
            \if@Divisible%
                \@GregorianLeaptrue%
            \else%
                \@GregorianLeapfalse%
            \fi%
        \else%
            \@GregorianLeaptrue%
        \fi%
    \else%
        \@GregorianLeapfalse%
    \fi%
    \if@GregorianLeap%
}%
%
% \GregorianDaysInPriorMonths{month}{year}{days}
% ===============================
% Number of days in months prior to `month' in the `year'
\def\GregorianDaysInPriorMonths #1#2#3{%
    {%
        #3 = \ifcase #1%
               0 \or%           % no month number 0
               0 \or%
              31 \or%
              59 \or%
              90 \or%
             120 \or%
             151 \or%
             181 \or%
             212 \or%
             243 \or%
             273 \or%
             304 \or%
             334%
        \fi%
        \IfGregorianLeap{#2}%
            \ifnum #1 > 2%              % if month after February
                \advance #3 by 1%       % add leap day
            \fi%
        \fi%
        \global\@common = #3%
    }%
    #3 = \@common%
}%
%
% \GregorianDaysInPriorYears{year}{days}
% ===============================
% Number of days in years prior to  the `year'
\def\GregorianDaysInPriorYears #1#2{%
     {%
         \countdef\tmpc = 4%               % \tmpc==\count4
         \countdef\tmpb = 2%               % \tmpb==\count2
         \tmpb = #1%                       %
         \advance \tmpb by -1%             %
         \tmpc = \tmpb%                    % \tmpc = \tmpb = year-1
         \multiply \tmpc by 365%           % Days in prior years =
         #2 = \tmpc%                       % = 365*(year-1) ...
         \tmpc = \tmpb%                    %
         \divide \tmpc by 4%               % \tmpc = (year-1)/4
         \advance #2 by \tmpc%             % ... plus Julian leap days ...
         \tmpc = \tmpb%                    %
         \divide \tmpc by 100%             % \tmpc = (year-1)/100
         \advance #2 by -\tmpc%            % ... minus century years ...
         \tmpc = \tmpb%                    %
         \divide \tmpc by 400%             % \tmpc = (year-1)/400
         \advance #2 by \tmpc%             % ... plus 4-century years.
         \global\@common = #2%
    }%
    #2 = \@common%
}%
%
% \AbsoluteFromGregorian{day}{month}{year}{absdate}
% ======================
% Calculation of absolute date ( days since 01.01.0001 ) from
% Gregorian date day.month.year
\def\AbsoluteFromGregorian #1#2#3#4{%
    {%
        \countdef\tmpd = 0%               % \tmpd==\count0
        #4 = #1%                          % days so far this month
        \GregorianDaysInPriorMonths{#2}{#3}{\tmpd}%
        \advance #4 by \tmpd%             % add days in prior months
        \GregorianDaysInPriorYears{#3}{\tmpd}%
        \advance #4 by \tmpd%             % add days in prior years
        \global\@common = #4%
    }%
    #4 = \@common%
}%

%   *******************************************
%   *             HEBREW PART                 *
%   *******************************************
%
\newif\if@HebrewLeap
%
% \CheckLeapHebrewYear{year}
% ======================
% Set \@HebrewLeaptrue if Hebrew `year' is
% a leap year: if (1+7*year)%19 < 7  then true else false
\def\CheckLeapHebrewYear #1{%
    {%
        \countdef\tmpa = 0%              % \tmpa==\count0
        \countdef\tmpb = 1%              % \tmpb==\count1
%
        \tmpa = #1%
        \multiply \tmpa by 7%
        \advance \tmpa by 1%
        \Remainder{\tmpa}{19}{\tmpb}%
        \ifnum \tmpb < 7%               % \tmpb = (7*year+1)%19
            \global\@HebrewLeaptrue%
        \else%
            \global\@HebrewLeapfalse%
        \fi%
    }%
}%
%
% \HebrewElapsedMonths{year}{months}
% ==============================
% Number of months elapsed from the Sunday prior to the start
% of the Hebrew calendar to the mean conjunction of Tishri
% of Hebrew `year'
\def\HebrewElapsedMonths #1#2{%
    {%
        \countdef\tmpa = 0%               % \tmpa==\count0
        \countdef\tmpb = 1%               % \tmpb==\count1
        \countdef\tmpc = 2%               % \tmpc==\count2
%
        \tmpa = #1%                       %
        \advance \tmpa by -1%             %
        #2 = \tmpa%                       % #2 = \tmpa = year-1
        \divide #2 by 19%                 % Number of complete Meton cycles
        \multiply #2 by 235%              % #2 = 235*((year-1)/19)
%
        \Remainder{\tmpa}{19}{\tmpb}%     % \tmpa = years%19 - years this cycle
        \tmpc = \tmpb%                    %
        \multiply \tmpb by 12%            %
        \advance #2 by \tmpb%             % add regular months this cycle
%
        \multiply \tmpc by 7%             %
        \advance \tmpc by 1%              %
        \divide \tmpc by 19%              % \tmpc = (1+7*((year-1)%19))/19 -
%                                         %  number of leap months this cycle
        \advance #2 by \tmpc%             %  add leap months
%
        \global\@common = #2%
    }%
    #2 = \@common%
}%
%
% \HebrewElapsedDays{year}{days}
% ==============================
% Number of days elapsed from the Sunday prior to the start
% of the Hebrew calendar to the mean conjunction of Tishri
% of Hebrew `year'
\def\HebrewElapsedDays #1#2{%
    {%
        \countdef\tmpa = 0%               % \tmpa==\count0
        \countdef\tmpb = 1%               % \tmpb==\count1
        \countdef\tmpc = 2%               % \tmpc==\count2
%
        \HebrewElapsedMonths{#1}{#2}%    %
        \tmpa = #2%                      %
        \multiply \tmpa by 13753%        %
        \advance \tmpa by 5604%             % \tmpa=MonthsElapsed*13758 + 5604
        \Remainder{\tmpa}{25920}{\tmpc}%    % \tmpc == ConjunctionParts
        \divide \tmpa by 25920%
%
        \multiply #2 by 29%
        \advance #2 by 1%
        \advance #2 by \tmpa%               %  #2 = 1 + MonthsElapsed*29 +
%                                          %          PartsElapsed/25920
        \Remainder{#2}{7}{\tmpa}%        %  \tmpa == DayOfWeek
        \ifnum \tmpc < 19440%
            \ifnum \tmpc < 9924%
            \else%                    % New moon at 9 h. 204 p. or later ...
                \ifnum \tmpa = 2%               % on Tuesday ...
                    \CheckLeapHebrewYear{#1}%      % of a common year
                    \if@HebrewLeap%
                    \else%
                        \advance #2 by 1%
                    \fi%
                \fi%
            \fi%
            \ifnum \tmpc < 16789%
            \else%                         % New moon at 15 h. 589 p. or later ...
                \ifnum \tmpa = 1%                 % on Monday ...
                    \advance #1 by -1%
                    \CheckLeapHebrewYear{#1}%      % at the end of leap year
                    \if@HebrewLeap%
                        \advance #2 by 1%
                    \fi%
                \fi%
            \fi%
        \else%
            \advance #2 by 1%            %  new moon at or after midday
        \fi%
%
        \Remainder{#2}{7}{\tmpa}%        %  \tmpa == DayOfWeek
        \ifnum \tmpa = 0%                %  if Sunday ...
            \advance #2 by 1%
        \else%                           %
            \ifnum \tmpa = 3%            %  Wednesday ...
                \advance #2 by 1%
            \else%
                \ifnum \tmpa = 5%        %  or Friday
                     \advance #2 by 1%
                \fi%
            \fi%
        \fi%
        \global\@common = #2%
    }%
    #2 = \@common%
}%
%
% \DaysInHebrewYear{year}{days}
% ==============================
% Number of days in Hebrew `year'
\def\DaysInHebrewYear #1#2{%
    {%
        \countdef\tmpe = 12%             % \tmpe==\count12
%
        \HebrewElapsedDays{#1}{\tmpe}%
        \advance #1 by 1%
        \HebrewElapsedDays{#1}{#2}%
        \advance #2 by -\tmpe%
        \global\@common = #2%
    }%
    #2 = \@common%
}%
%
% \HebrewDaysInPriorMonths{month}{year}{days}
% ===============================
% Number of days in months prior to `month' in the `year'
\def\HebrewDaysInPriorMonths #1#2#3{%
    {%
        \countdef\tmpf= 14%            % \tmpf==\count14
%
        #3 = \ifcase #1%       % Days in prior month of regular year
               0 \or%          % no month number 0
               0 \or%          % Tishri
              30 \or%          % Heshvan
              59 \or%          % Kislev
              89 \or%          % Tebeth
             118 \or%          % Shebat
             148 \or%          % Adar I
             148 \or%          % Adar II
             177 \or%          % Nisan
             207 \or%          % Iyar
             236 \or%          % Sivan
             266 \or%          % Tammuz
             295 \or%          % Av
             325 \or%          % Elul
             400%              % Dummy
        \fi%
        \CheckLeapHebrewYear{#2}%
        \if@HebrewLeap%                 % in leap year
            \ifnum #1 > 6%              % if month after Adar I
                \advance #3 by 30%      % add  30 days
            \fi%
        \fi%
        \DaysInHebrewYear{#2}{\tmpf}%
        \ifnum #1 > 3%
            \ifnum \tmpf = 353%         %
                \advance #3 by -1%      %
            \fi%                        %  Short Kislev
            \ifnum \tmpf = 383%         %
                \advance #3 by -1%      %
            \fi%                        %
        \fi%
%
        \ifnum #1 > 2%
            \ifnum \tmpf = 355%         %
                \advance #3 by 1%       %
            \fi%                        %  Long Heshvan
            \ifnum \tmpf = 385%         %
                \advance #3 by 1%       %
            \fi%                        %
        \fi%
        \global\@common = #3%
    }%
    #3 = \@common%
}%
%
% \AbsoluteFromHebrew{day}{month}{year}{days}
% ===============================
% Absolute date of Hebrew day,month,year
\def\AbsoluteFromHebrew #1#2#3#4{%
    {%
        #4 = #1%
        \HebrewDaysInPriorMonths{#2}{#3}{#1}%
        \advance #4 by #1%             % Add days in prior months this year
        \HebrewElapsedDays{#3}{#1}%
        \advance #4 by #1%             % Add days in prior years
        \advance #4 by -1373429%       % Subtract days before Gregorian 1.1.1
        \global\@common = #4%
    }%
    #4 = \@common%
}%
%
% \HebrewFromGregorian{Gday}{Gmonth}{Gyear}{Hday}{Hmonth}{Hyear}
% ===============================
% Evaluating Hebrew date Hday,Hmonth,Hyear from
% Gregorian date Gday,Gmonth,Gyear
\def\HebrewFromGregorian #1#2#3#4#5#6{%
    {%
        \countdef\tmpx= 17%            % \tmpx==\count17
        \countdef\tmpy= 18%            % \tmpy==\count18
        \countdef\tmpz= 19%            % \tmpz==\count19
%
        #6 = #3%                       %
        \global\advance #6 by 3761%           % approximation from above
        \AbsoluteFromGregorian{#1}{#2}{#3}{#4} %
        \tmpz = 1  \tmpy = 1%
        \AbsoluteFromHebrew{\tmpz}{\tmpy}{#6}{\tmpx}%
        \ifnum \tmpx > #4%              %
            \global\advance #6 by -1%         % Hyear = Gyear + 3760
            \AbsoluteFromHebrew{\tmpz}{\tmpy}{#6}{\tmpx}%
        \fi%                            %
        \advance #4 by -\tmpx%         % Days in this year
        \advance #4 by 1%               %
        #5 = #4%                        %
        \divide #5 by 30%               % Approximation for month from below
        \loop%                          % Search for month
            \HebrewDaysInPriorMonths{#5}{#6}{\tmpx}%
            \ifnum \tmpx < #4%
                \advance #5 by 1%
                \tmpy = \tmpx%
        \repeat%
        \global\advance #5 by -1%
        \global\advance #4 by -\tmpy%
    }%
}%
\catcode`@=12