%
%   TeX & LaTeX MACROS FOR COMPUTING HEBREW DATE FROM GREGORIAN ONE
%
%   by Michail Rozman, misha@iop.tartu.ew.su
%
%   Released:     Tammuz 12, 5751 -- June 24, 1991
%   Corrected:    Shebat 10, 5752 -- January 15, 1992  by Rama Porrat
%   Corrected:    Adar II 5, 5752 -- March 10, 1992    by Misha
%   Corrected:    Tebeth, 5756 -- January 1996         Dan Haran
%						  (haran@math.tau.ac.il)

%   Please direct any comments, bug reports, questions, etc.
%   to the address above.
%
%   If Hebrew fonts and style macros are loaded AND you are in
%   Hebrew mode the produced date is in Hebrew.
%   Otherwise the date string looks like Shebat 10, 5752.
%
%   Use this style as follows:
%
%          LaTeX                  |           TeX
%                                 |
% \documentstyle[...,hebcal,...]       \input hebcal.sty
%
%                                           %  ONLY IF \ifheb IS NOT
%                  \newif\ifheb \hebtrue    %  DEFINED IN YOUR
%                                           %  HEBREW STYLE !!
%
%                  \Hebrewtoday
%          or
%                  {\day=10 \month=3 \year=1992 % to fool \today
%                  \today == \Hebrewtoday}
%          or
%                  \newcount\hd \newcount\hm \newcount\hy
%                  \hd=10 \hm=3 \hy=1992 \Hebrewdate{\hd}{\hm}{\hy}
%
%
%   TABLE OF CONTENTS
%
%   INTRODUCTION
%   REGISTERS, COMMANDS AND LANGUAGE-DEPENDENT INTERFACE
%      Hebrew language macros
%      English language macros
%   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
%
% \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}%
    \ifundefined{ifheb}%  Hebrew fonts&macros are not loaded
       \FormatForEnglish{\Hebrewday}{\Hebrewmonth}{\Hebrewyear}%
    \else%
       \FormatDate{\Hebrewday}{\Hebrewmonth}{\Hebrewyear}%
    \fi%
}%
%
%\Hebrewtoday
% ============
% Today's date in Hebrew calendar
\def\Hebrewtoday{\Hebrewdate{\day}{\month}{\year}}
\let\hebrewtoday=\Hebrewtoday
%
% \Hebrewsetreg
% ============
% Set registers: today's date in hebrew calendar
\def\Hebrewsetreg{%
    \HebrewFromGregorian{\day}{\month}{\year}
                        {\Hebrewday}{\Hebrewmonth}{\Hebrewyear}%
}%
%
% \FormatDate{Hebrewday}{Hebrewmonth}{Hebrewyear}
% ===============================================
\def\FormatDate #1#2#3{%
        \ifheb%           Hebrew mode
            \FormatForHebrew{#1}{#2}{#3}%
        \else%            English mode
            \FormatForEnglish{#1}{#2}{#3}
        \fi%
}%
%
% TO PREPARE ANOTHER LANGUAGE VERSION CHANGE/ADD HERE !!!
%
% ====================== Hebrew macros ===============================
%
\def\HebrewYearChar{'}
%
%
%  \ShortHYName{year}
%  =====================
%  Hebrew year (mod 1000) as a string of Hebrew letters
\def\ShortHYName#1{{%
       \count2 = #1%
       \Remainder{\count2}{1000}{#1}% E.g., for  5752: #1 = 752
       \def\HY{}% the string of hundreds, tens and ones
       \ifnum #1 > 799 \strcat{\HY}{zz}\advance#1 by -800 \fi%
       \ifnum #1 > 399 \strcat{\HY}{z}\advance#1  by -400 \fi%
       \ifnum #1 > 299 \strcat{\HY}{y}\advance#1  by -300 \fi%
       \ifnum #1 > 199 \strcat{\HY}{x}\advance#1  by -200 \fi%
       \ifnum #1 > 99  \strcat{\HY}{w}\advance#1  by -100 \fi%
       \ifnum #1 > 0 %
           %
           % 15 and 16 are special exceptions
           %
           \ifnum #1 = 16 %
                   \strcat{\HY}{hf}%
           \else%
                   \ifnum #1 = 15 %
                           \strcat{\HY}{he}%
                   \else%
                           \Remainder{#1}{10}{\count0 }%
                           \divide #1 by 10 %
                           \ifnum\count0 = 0%
                               \strcat{\HY}{\TensInHebrewEnd{#1}}%
                           \else%
                               \strcat{\HY}{\TensInHebrew{#1}}%
                               \strcat{\HY}{\DigitInHebrew{\count0 }}%
                           \fi%
                   \fi%
           \fi%
       \fi%
       \countdef\HYLength=0 %          \HYLength==\count0 = strlen(\HY)
       \HYLength =0 \expandafter\getlength\HY\end%
       \ifnum\HYLength > 0 %
           \ifnum\HYLength = 1 %
                   \HY\HebrewYearChar%
           \else%
                   \expandafter\Insert\HY%
           \fi%
       \else%
                   \relax%
       \fi%
}}%
%
%  \HebrewYearName{year}
%  =====================
%  Hebrew year as a string of Hebrew letters
%  0 < year < 10000
\def\HebrewYearName#1{{%
       \count2 = #1%
       \Remainder{\count2}{10000}{#1}% macro only for year <= 9999
       \count2 = #1%
       \Remainder{\count2}{1000}{#1}% E.g., for  5752: #1 = 752
       \divide \count2 by 1000 %       E.g., for  5752: \count2 = 5
       \countdef\thousand=2 %               \thousand==\count2
       \def\Thousands{}% the string of thousands
       \strcat{\Thousands}{\DigitInHebrew{\thousand }}% output 1000'
       \ifnum\thousand = 5 % Change it in the next Hebrew Millennium..
               \ifnum#1 > 0 % if 5000 < year < 6000
                      \def\Thousands{}% don't mention thousands
               \fi%
       \fi%
       \countdef\ThousandsLength=0 %
                        % \ThousandsLength==\count0 = strlen(\Thousands)
       \ThousandsLength =0 \expandafter\getlength\Thousands\end%
       \ifnum\ThousandsLength > 0 %       if the letter of thousands is not empty
               \strcat{\Thousands}{'}% add '
       \fi%
       \def\HY{\ShortHYName{#1}}% the string of hundreds, tens and ones
       \def\PratGadol{}%    An additional string to date. Default: empty
       \ifnum\thousand < 1%
           \def\PratGadol{\ )lt"b(}% 
       \fi%
       \ifnum#1 = 0%
           \def\PratGadol{\ )lt"b(}% 
       \fi%
       \Thousands\HY\PratGadol%
}}%
%
% \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%
`'\or%
a'\or%
b'\or%
c'\or%
d'\or%
e'\or%
f'\or%
g'\or%
h'\or%
i'\or%
i''`\or%
i''a\or%
i''b\or%
i''c\or%
h''e\or%
h''f\or%
i''f\or%
i''g\or%
i''h\or%
k'\or%
k''`\or%
k''a\or%
k''b\or%
k''c\or%
k''d\or%
k''e\or%
k''f\or%
k''g\or%
k''h\or%
l'\or%
l''`%
\fi%
}%
%
%  \DigitInHebrew{n}
%  =================
%  Single digit 1 <= n <= 9 as Hebrew letter
%  short macro:
%  \def\DigitInHebrew #1{%
%  {\ifnum#1>0 \ifnum#1<10 \advance#1 by `\_ \char#1 \fi\fi}}
%
\def\DigitInHebrew #1{%
       \ifcase#1%
                    %   nothing for 0
               \or `%   1
               \or a%   2
               \or b%   3
               \or c%   4
               \or d%   5
               \or e%   6
               \or f%   7
               \or g%   8
               \or h%   9
       \fi%
}%
%
%  \TensInHebrew{num}
%  ================
% Convert 10*num ( 1 <= num <= 9 ) into single Hebrew letter
\def\TensInHebrew #1{%
       \ifcase #1%
                    %   nothing for 0
               \or i%   10
               \or k%   20
               \or l%   30
               \or n%   40
               \or p%   50
               \or q%   60
               \or r%   70
               \or t%   80
               \or v%   90
       \fi%
}%
%
%  \TensInHebrewEnd{num}
%  ================
% Convert 10*num ( 1 <= num <= 9 ) into single Hebrew letter;
% this letter is an endletter version ("oth sofith") if it exists.
\def\TensInHebrewEnd #1{%
       \ifcase #1%
                    %   nothing for 0
               \or i%   10
               \or j%   20
               \or l%   30
               \or m%   40
               \or o%   50
               \or q%   60
               \or r%   70
               \or s%   80
               \or u%   90
       \fi%
}%
%
% \FormatForHebrew{hday}{hmonth}{hyear}
% =====================================
\def\FormatForHebrew#1#2#3{%
\HebrewDayName{#1}~a\HebrewMonthName{#2}{#3},~\HebrewYearName{#3}%
}%
%
%  English macros ==================================
%
% \HebrewMonthNameInEnglish{month}{year}
% ===============================
% Name of month in the `year'
\def\HebrewMonthNameInEnglish #1#2{%
    \ifnum #1 = 7%
    \CheckLeapHebrewYear{#2}%
        \if@HebrewLeap Adar II \else Adar \fi%
    \else%
        \ifcase #1%
            Dummy     \or%
            Tishrei    \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%
            Elul%
        \fi                                  %
    \fi                                      %
}%
%
% \FormatForEnglish{hday}{hmonth}{hyear}
% =====================================
\def\FormatForEnglish #1#2#3{%
    \HebrewMonthNameInEnglish{#2}{#3} \number#1,\ \number#3%
}%
%
%   *******************************************
%   *           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%
    }%
}%
%
%  \strcat{str1}{str2}
%  ===================
%  apend str2 to the end of macro str1
\def\strcat#1#2{\edef#1{#1#2}}
%
%  \getlength{ string }
%  ====================
% Number of nonblank tokens
%  This macro is called if \count0==strlen('string') >= 2
% usage:  {{\count0=0 \expandafter\getlength#1\end \number\count0}}
\def\getlength #1{%
       \ifx#1\end%
         \let\next=\relax%
       \else%
         \advance\count0 by 1%
         \let\next=\getlength%
       \fi%
       \next%
}%
%
%  \Insert
%  =======
%  Insert char <"> befor the last char and output 'string'
%  usage: {\def\abc{abc} \count0=0 \expandafter\getlength\abc\end
%         \expandafter\Insert\abc}
\def\Insert #1{%
       \ifnum\count0 = 1%
#1%
\let\next=\relax%
\else%
#1%
\ifnum\count0 = 2%
"%
\fi
\advance\count0 by -1%
\let\next=\Insert%
\fi%
\next%
}
%
%  \ifundefined{command}
%  =====================
%  TeXbook, ex. 7.7 \ifundefined{command}<true text>\else<false text>\fi
\def\ifundefined #1{\expandafter\ifx\csname#1\endcsname\relax}
%
%   *******************************************
%   *           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

