%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Advance Date Package for LaTeX2e                                       %%
%% (C) Z. Wagner -- Ice Bear Soft, 20 Oct 1996, <wagner@mbox.cesnet.cz>   %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% This package contains macros which can add specified number of days    %%
%% to the current date (as specified in \today) and print it. Some other  %%
%% macros are also provided. It is intended for use e.g. in invoices      %%
%% payable within 14 days from today etc.                                 %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Macros with one default argument [in square brackets]                  %%
%% \AdvanceDate   Default: [1]                                            %%
%%      Advances date the specified number of days and sets the result    %%
%%      to \today                                                         %%
%% \DayAfter      Default: [1]                                            %%
%%      Advances date by call to \AdvanceDate, prints \today and restores %%
%%      the oroginal value of \today                                      %%
%% \SaveDate      Default: [\s@v@dT@d@y]                                  %%
%%      Saves \today into the specified macro                             %%
%% \SetDate       Default: [\s@v@dT@d@y]                                  %%
%%      Sets the date stored in \today. The argument must be in form:     %%
%%      [dd/mm/yyyy] and may be present in a macro because the argument   %%
%%      is always expanded before use. The value saved by \SaveDate can   %%
%%      be used here.                                                     %%
%%                                                                        %%
%% Macros with one mandatory argument                                     %%
%% \ThisDay     sets \day                                                 %%
%% \ThisMonth   sets \month                                               %%
%% \ThisYear    sets \year                                                %%
%% \AdvMonth    advances \month and fixes it                              %%
%% \AdvYear     advances \year                                            %%
%%                                                                        %%
%% Macros without arguments                                               %%
%% \FixMonth    fixes \today so that 0 < \month < 13                      %%
%% \FixDate     fixes \today so that it is valid                          %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{advdate}[1996/10/20 Advance Date Package]

% KH code

\newcount\r@k  \newcount\un@r
\newcount\m@l  \m@l31
\newcount\m@s  \m@s30
\newcount\M@s

\newcommand\is@LeapYear[1][\year]{\r@k#1\un@r\r@k
  \divide\r@k4\multiply\r@k4\advance\r@k-\un@r
  \ifnum\r@k=0%
    \divide\un@r100\r@k\un@r\multiply\un@r100\advance\un@r-#1%
    \ifnum\un@r=0%
      \un@r\r@k \divide\un@r4\multiply\un@r4\advance\un@r-\r@k
      \un@r\ifnum\un@r=0 29\else 28\fi
    \else
      \un@r 29%
    \fi
  \else
    \un@r 28%
  \fi
  \r@k 337\advance\r@k\un@r}

\newcommand\m@sic{\ifcase \month \or \m@l \or \un@r \or \m@l \or \m@s \or \m@l \or \m@s
  \or \m@l \or \m@l \or \m@s \or \m@l \or \m@s \or \m@l \fi}

\newcommand\FixMonth{%
  \L@@p \ifnum\month<1 \advance\year\m@ne \advance\month12 \is@LeapYear \repeat
  \L@@p \ifnum\month>12 \advance\year\@ne \advance\month-12 \is@LeapYear \repeat}

\newcommand\FixDate{%
  \FixMonth \is@LeapYear
  \l@@p \ifnum\day<1 \Pr@vD@y \repeat
  \l@@p \M@s\m@sic \ifnum\day>\M@s \N@xtD@y \repeat
}

% We need a nested \loop and the user might call the macros from his/her \loop

\def\L@@p#1\repeat{\def\It@r@te{#1\relax \expandafter\It@r@te\fi}\It@r@te
  \let\It@r@te\relax}

\def\l@@p#1\repeat{\def\it@r@te{#1\relax \expandafter\it@r@te\fi}\it@r@te
  \let\it@r@te\relax}

\def\Pr@vD@y{%
  \ifnum\day<-366
    \ifnum\month>2
      \advance\day\r@k \advance\year\m@ne \is@LeapYear
    \else
      \advance\year\m@ne \is@LeapYear \advance\day\r@k
    \fi
  \else
    \advance\month\m@ne \FixMonth
    \advance\day\m@sic
  \fi}

\def\N@xtD@y{%
  \ifnum\day>366
    \ifnum\month>2
      \advance\year\@ne \is@LeapYear \advance\day-\r@k
    \else
      \advance\day-\r@k \advance\year\@ne \is@LeapYear
    \fi
  \else
    \advance\day-\M@s \advance\month\@ne \FixMonth
  \fi}

\newcommand\AdvanceDate[1][\@ne]{\advance\day#1 \FixDate}

\newcommand\DayAfter[1][\@ne]{{\AdvanceDate[#1]\today}}

\newcommand\SaveDate[1][\s@v@dT@d@y]{%
  \expandafter \expandafter \expandafter \expandafter
  \expandafter \expandafter \expandafter \edef
  \expandafter \expandafter \expandafter \expandafter
  \expandafter \expandafter \expandafter #1\expandafter
  \expandafter \expandafter \expandafter
  \expandafter \expandafter \expandafter
  {\expandafter \expandafter \expandafter \number
    \expandafter \expandafter \expandafter \day
    \expandafter \expandafter \expandafter /\expandafter \number
    \expandafter \month \expandafter /\number \year}}

\newcommand\SetDate[1][\s@v@dT@d@y]{\edef\d@t@{#1}%
  \expandafter\t@d@y\d@t@\t@d@y\FixDate}

\def\t@d@y#1/#2/#3\t@d@y{\day#1 \month#2 \year#3 \relax}

\newcommand\ThisDay[1]{\day#1 \relax}
\newcommand\ThisMonth[1]{\month#1 \relax}
\newcommand\ThisYear[1]{\year#1 \relax}
\newcommand\AdvMonth[1]{\advance\month#1 \FixMonth}
\newcommand\AdvYear[1]{\advance\year#1 \relax}
