%%%%%%%%%%%%% cal-util.def %%%%%%%%%%%%%%%%
% Macros shared by hijrical and hebrewcal %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% the following is adapted from hebcal.sty in babel
\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
\def\@CheckIfDivisible#1#2{%
    {%
      \countdef\tmpx=0%        % temporary variable
      \@Remainder{#1}{#2}{\tmpx}%
      \ifnum\tmpx=0%
          \global\@Divisibletrue%
      \else%
          \global\@Divisiblefalse%
      \fi}}
\newif\if@GregorianLeap
\def\@CheckIfGregorianLeap#1{%
   {%
   \@CheckIfDivisible{#1}{4}%
    \if@Divisible%
        \@CheckIfDivisible{#1}{100}%
        \if@Divisible%
            \@CheckIfDivisible{#1}{400}%
            \if@Divisible%
                \global\@GregorianLeaptrue%
            \else%
                \global\@GregorianLeapfalse%
            \fi%
        \else%
            \global\@GregorianLeaptrue%
        \fi%
    \else%
        \global\@GregorianLeapfalse%
    \fi%
    }}
%%

\newcounter{tmpA}\newcounter{tmpB}
\newcounter{tmpC}\newcounter{tmpD}
\newcounter{tmpE}\newcounter{tmpF}


%% This is an algorithm from Reingold & Dershowitz, 
%% Calendrical Calculations, The Millenium Edition
%%
\def\@FixedFromGregorian#1#2#3#4{%
 \setcounter{tmpA}{(#1-1)*365}%
 \setcounter{tmpB}{(#1-1)/4}%
 \setcounter{tmpC}{(#1-1)/100}%
 \setcounter{tmpD}{(#1-1)/400}%
 \setcounter{tmpE}{(367*#2-362)/12}%
 \ifnum#2<3%
    \setcounter{tmpF}{0}%
 \else%
      \@CheckIfGregorianLeap{#1}%
      \if@GregorianLeap%
        \setcounter{tmpF}{-1}%
      \else%
        \setcounter{tmpF}{-2}%
      \fi%
 \fi%
 \@ifundefined{c@#4}{\global\newcounter{#4}}{}%
 \setcounter{#4}{\value{tmpA}+\value{tmpB}-\value{tmpC}+\value{tmpD}+\value{tmpE}+\value{tmpF}+#3}%
}
\endinput
