%%
%% This is file `datenumber.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% datenumber.dtx  (with options: `datenumber')
%% 
%% This file is part of the `datenumber' package,
%% 
%% Copyright (C) 2001 J"org-Michael Schr"oder.
%% 
%% IMPORTANT NOTICE:
%% 
%% This program may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.2
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%% http://www.latex-project.org/lppl.txt
%% and version 1.2 or later is part of all distributions of LaTeX
%% version 1999/12/01 or later.
%% 
%% This program consists of all files listed in README.txt.
%% 
%% For error reports, or offers to help make this a more powerful
%% tool, please contact me on schrj020@mail.uni-mainz.de
%% 
\def\fileversion{v0.03}
\def\filedate{2022/01/27}

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{datenumber}[\filedate \fileversion]

\DeclareOption{english}{\input datenumberenglish.ldf}
\DeclareOption{USenglish}{\input datenumberUSenglish.ldf}
\DeclareOption{french}{\input datenumberfrench.ldf}
\DeclareOption{italian}{\input datenumberitalian.ldf}
\DeclareOption{spanish}{\input datenumberspanish.ldf}
\DeclareOption{german}{\input datenumbergerman.ldf}
\DeclareOption{ngerman}{\input datenumbergerman.ldf}
\input datenumberUSenglish.ldf
\ProcessOptions
\newcounter{@dayscnt}
\newcounter{@startyear}

\def\setstartyear#1{%
  \ifnum#1<1
    \errmessage{Package datenumber: startyear < 1}%
  \fi
  \setcounter{@startyear}{#1}%
  \setdatetoday
}

\def\thestartyear{\the@startyear}

\def\setmydatenumber#1#2#3#4{%
  \ifvaliddate{#2}{#3}{#4}%
  \else
    \errmessage{\@errmessage}%
  \fi
  \ifnum\the@startyear<1
    \errmessage{Package datenumber: startyear < 1}%
  \fi
  \ifnum#2<\the@startyear
    \errmessage{Package datenumber: datenumber < startyear (#2 < \the@startyear)}%
  \fi
  \setcounter{@dayscnt}{0}%
  \ifnum#2>\the@startyear
    \relax\@yearloop{#2}%
  \fi
  \addtocounter{@dayscnt}{%
     \ifcase #3%
        \or 0\or 31\or 59
        \or 90\or 120\or 151
        \or 181\or 212\or 243
        \or 273\or 304\or 334
     \fi
  }%
  \ifleapyear{#2}%
    \ifnum#3>2
      \addtocounter{@dayscnt}{1}%
    \fi
  \fi
  \ifnum#2=1582
    \ifnum#3>10
      \addtocounter{@dayscnt}{-10}%
    \fi
    \ifnum#3=10
      \ifnum#4>14
        \addtocounter{@dayscnt}{-10}%
      \fi
    \fi
  \fi
  \addtocounter{@dayscnt}{#4}%
  \setcounter{#1}{\the@dayscnt}%
}

\def\setmydatebynumber#1#2#3#4{%
  \ifnum\the@startyear<1
    \errmessage{Package datenumber: startyear < 1}%
  \fi
  \ifnum#1<1
    \errmessage{Package datenumber: datenumber < 1}%
  \fi
  \setcounter{#2}{\the@startyear}%
  \setcounter{@dayscnt}{1}%
  \loop
    \ifleapyear{\thedateyear}%
      \@tempcnta=366
    \else
      \@tempcnta=365
    \fi
    \ifnum\thedateyear=1582
      \@tempcnta=355
    \fi
    \addtocounter{@dayscnt}{\the\@tempcnta}%
    \ifnum\the@dayscnt<#1\addtocounter{dateyear}{1}%
  \repeat
  \addtocounter{@dayscnt}{-\the\@tempcnta}%
  %
  \setcounter{#3}{1}%
  \setcounter{#4}{1}%
  \setmydatenumber{@dayscnt}{\value{#2}}{\value{#3}}{\value{#4}}%
  \ifnum#1>\the@dayscnt\relax
    \loop
      \mynextdate{#2}{#3}{#4}%
      \addtocounter{@dayscnt}{1}%
      \ifnum\the@dayscnt<#1\relax
    \repeat
    \setmydatenumber{@dayscnt}{\value{#2}}{\value{#3}}{\value{#4}}%
  \fi
}

\def\mynextdate#1#2#3{%
  \ifvaliddate{\value{#1}}{\value{#2}}{\value{#3}}%
  \else
    \errmessage{\@errmessage}%
  \fi
  \addtocounter{#3}{1}%
  \ifleapyear{\value{#1}}%
    \@tempcnta=29
  \else
    \@tempcnta=28
  \fi
  \ifnum\value{#3}>%
    \ifcase\value{#2}\or31\or\@tempcnta\or31\or30\or31\or30%
                     \or31\or31\or30\or31\or30\else31%
    \fi\relax
    \setcounter{#3}{1}%
    \addtocounter{#2}{1}%
    \ifnum\value{#2}>12
      \setcounter{#2}{1}%
      \addtocounter{#1}{1}%
    \fi
  \fi
  \ifnum\value{#1}=1582
    \ifnum\value{#2}=10
      \ifnum\value{#3}=5
       \setcounter{#3}{15}%
      \fi
    \fi
  \fi
}

\def\myprevdate#1#2#3{%
  \ifvaliddate{\value{#1}}{\value{#2}}{\value{#3}}%
  \else
    \errmessage{\@errmessage}%
  \fi
  \addtocounter{#3}{-1}%
  \ifleapyear{\value{#1}}%
    \@tempcnta=29
  \else
    \@tempcnta=28
  \fi
  \ifnum\value{#3}=0
    \addtocounter{#2}{-1}%
    \ifnum\value{#2}=0
      \addtocounter{#1}{-1}%
      \setcounter{#2}{12}%
    \fi
    \setcounter{#3}{%
    \ifcase\value{#2}\or 31\or\the\@tempcnta\or 31\or 30\or 31\or 30
                     \or 31\or 31\or 30\or 31\or 30\else 31
    \fi
    }%
  \fi
  \ifnum\value{#1}=1582
    \ifnum\value{#2}=10
      \ifnum\value{#3}=14
        \setcounter{#3}{4}%
      \fi
    \fi
  \fi
}


\newcounter{datenumber}
\newcounter{dateyear}
\newcounter{datemonth}
\newcounter{dateday}
\newcounter{datedayname}

\def\setdatenumber#1#2#3{%
  \setmydatenumber{datenumber}{#1}{#2}{#3}%
  \setcounter{dateyear}{#1}%
  \setcounter{datemonth}{#2}%
  \setcounter{dateday}{#3}%
  \setdaynamebynumber{\thedatenumber}%
  \setmonthname{\thedatemonth}%
}

\def\setdatebynumber#1{%
  \setmydatebynumber{#1}{dateyear}{datemonth}{dateday}%
  \setcounter{datenumber}{#1}%
  \setdaynamebynumber{\thedatenumber}%
  \setmonthname{\thedatemonth}%
}

\def\nextdate{%
 \mynextdate{dateyear}{datemonth}{dateday}%
 \setdate{\thedateyear}{\thedatemonth}{\thedateday}%
}

\def\prevdate{%
 \myprevdate{dateyear}{datemonth}{dateday}%
 \setdate{\thedateyear}{\thedatemonth}{\thedateday}%
}

\def\setdate#1#2#3{%#1 year, #2 month, #3 day
  \setdatenumber{#1}{#2}{#3}%
}

\def\setdatetoday{%
  \setdatenumber{\the\year}{\the\month}{\the\day}%
}


\def\setmonthname#1{%
  \def\datemonthname{\@monthname{#1}}%
}


\def\setdayname#1{%
  \def\datedayname{\@dayname{#1}}%
}

\def\setdaynamebynumber#1{%
  \@tempcnta=#1\relax
  \ifnum\the@startyear=1800\advance \@tempcnta by 1\fi
  \ifnum\the@startyear=1900\advance \@tempcnta by 6\fi
  \ifnum\the@startyear=2000\advance \@tempcnta by 4\fi
  \@modulo{\the\@tempcnta}{7}%
  \setcounter{datedayname}{\the\@den}%
  \addtocounter{datedayname}{1}%
  \setdayname{\thedatedayname}%
}

\def\datedate{\@datedate}%

\def\dateselectlanguage#1{%
  \csname @datelanguage#1\endcsname
  \setdayname{\thedatedayname}%
  \setmonthname{\thedatemonth}%
}

\def\ifleapyear#1{%#1 year
  \ifnum#1>1500
    \@modulo{#1}{100}%
    \ifnum\@den=0
      \@modulo{#1}{400}%
    \else
      \@modulo{#1}{4}%
    \fi
  \else
    \@modulo{#1}{4}%
  \fi
  \ifnum\@den=0
}

\def\ifvaliddate#1#2#3{%
  \ifleapyear{#1}%
    \@tempcnta=29
  \else
    \@tempcnta=28
  \fi
  \gdef\@errmessage{}%
  \ifnum#1<1
    \gdef\@errmessage{Package datenumber: %
                      invalid date: year < 1, #1/#2/#3}%
  \fi%
  \ifnum#2<1
    \gdef\@errmessage{Package datenumber: %
                      invalid date: month < 1, #1/#2/#3}%
  \fi%
  \ifnum#2>12
    \gdef\@errmessage{Package datenumber: %
                      invalid date: month > 12, #1/#2/#3}%
  \fi%
  \ifnum#3<1
    \gdef\@errmessage{Package datenumber: %
                      invalid date: day < 1, #1/#2/#3}%
  \fi%
  \@tempcntb=
  \ifcase#2 31\or 31\or\the\@tempcnta\or 31\or 30\or 31\or 30
                     \or 31\or 31\or 30\or 31\or 30\else 31
  \fi\relax
  \ifnum#3>\@tempcntb
    \gdef\@errmessage{Package datenumber: %
                      invalid date: day > \the\@tempcntb, #1/#2/#3}%
  \fi%
  \ifnum#1=1582
    \ifnum#2=10
      \ifnum#3>4
        \ifnum#3<15
          \gdef\@errmessage{Package datenumber: %
                            invalid date: #1/#2/#3}%
        \fi
      \fi
    \fi
  \fi
  \ifx\@errmessage\empty
}

\def\@yearloop#1{%
  \@tempcnta=\the@startyear%
  \loop
    \ifleapyear{\@tempcnta}%
      \addtocounter{@dayscnt}{366}%
    \else
      \addtocounter{@dayscnt}{365}%
    \fi
    \ifnum\@tempcnta=1582
      \addtocounter{@dayscnt}{-10}%
    \fi
    \advance \@tempcnta by 1
    \ifnum\@tempcnta=#1\else
  \repeat
}

\newcount\@nom\newcount\@den

\def\@modulo#1#2{%
  \@nom=#1\relax
  \@den=#2\relax
  \divide\@nom by \@den%
  \multiply\@nom by \@den%
  \@den=#1%
  \advance\@den by -\@nom%
}

\setstartyear{1800}

\endinput
%%
%% End of file `datenumber.sty'.
