%\def\filedate{30 Oct 2002}
%\def\fileversion{2.0}
%\def\filename{datetime.dtx}
%\def\docdate{3$^{\mathrm{rd}}$ November 2002}
%\iffalse
% Doc-Source file to use with LaTeX2e
% Copyright (C) 2000 Nicola Talbot, all rights reserved.
%\fi
%
%\changes{v1.0}{8 Aug 2000}{First release}
%\changes{v1.01}{18 Sept 2000}{Added documentation}
%\changes{v1.04}{19 Dec 2000}{Added macros for printing values of count registers as text strings}
%\changes{v1.1}{20 Apr 2002}{amended \texttt{$\backslash$ordinal} so that plain numbers could be
% passed as the argument (instead of count registers).  Added \texttt{$\backslash$thedate}}
%\changes{v2.0}{30 Oct 2002}{changed \texttt{$\backslash$thedate} to \texttt{$\backslash$formatdate}
% to avoid name conflict with \textsf{seminar} class file.  Also changed \texttt{$\backslash$ordinal},
% \texttt{$\backslash$ordinalstring}, \texttt{$\backslash$Ordinalstring},
% \texttt{$\backslash$numberstring}, \texttt{$\backslash$Numberstring} to take a
% \LaTeX\ counter as argument instead of a \TeX\ count register, to make them
% more consistent with \texttt{$\backslash$arabic}, \texttt{$\backslash$roman},
% \texttt{$\backslash$Roman} and \texttt{$\backslash$alph}}
%\CheckSum{780}
%
% \title{datetime.sty v\fileversion: Formatting Current Date and Time}
% \author{N.L.C. Talbot}
% \date{\docdate}
% \maketitle
% \section{Introduction}
% The package |datetime.sty| provides various different formats for |\today|,
% and provides commands for displaying the current time.  It also provides
% commands that display the value of a \LaTeX\ counter either as an ordinal
% or as text.
%
% \section{Date Declarations}
% There are various declarations that change the effect of |\today|.  The
% change can be localised by placing the declaration within a group.
%
% \vspace{10pt}\noindent
% \meta{Day} \meta{Month} \meta{Year} formats:\\
% \noindent\DescribeMacro{\longdate}
% The declaration |\longdate| will redefine |\today| to produce the
% current date displayed in the form Wednesday 8$^{\mathrm{th}}$ March, 2000
% if the option |dayofweek| is used, or 8$^{\mathrm{th}}$ March, 2000 if the option
% |nodayofweek| is used.\\
% \DescribeMacro{\shortdate}
% The declaration |\shortdate| will redefine |\today| to produce the
% current date displayed in the form Wed 8$^{\mathrm{th}}$ Mar, 2000
% if the option |dayofweek| is used, or 8$^{\mathrm{th}}$ Mar, 2000 if the option
% |nodayofweek| is used.\\
% \DescribeMacro{\ddmmyyyydate}
% The declaration |\ddmmyyyydate| will redefine |\today| to produce
% the current date displayed in the form 08/03/2000\\
% \DescribeMacro{\dmyyyydate}
% The declaration |\dmyyyydate| will redefine |\today| to produce
% the current date displayed in the form 8/3/2000\\
% \DescribeMacro{\ddmmyydate}
% The declaration |\ddmmyydate| will redefine |\today| to produce
% the current date displayed in the form 08/03/00\\
% \DescribeMacro{\dmyydate}
% The declaration |\dmyydate| will redefine |\today| to produce
% the current date displayed in the form 8/3/00\\
% \DescribeMacro{\textdate}
% The declaration |\textdate| will redefine |\today| to produce
% the current date displayed in the form: Wednesday the Eighth of March, Two Thousand
% if the option |dayofweek| is used, or Eighth of March, Two Thousand if the option
% |nodayofweek| is used.
%
% \vspace{10pt}\noindent
% \meta{Month} \meta{Day} \meta{Year} formats:\\
% \DescribeMacro{\usdate}
% The declaration |\usdate| will redefine |\today| to produce
% the current date displayed in the form March 8, 2000. (As \TeX\ and
% \LaTeX\ do by default.)\\
% \DescribeMacro{\mmddyyyydate}
% The declaration |\mmddyyyydate| will redefine |\today| to produce
% the current date displayed in the form 03/08/2000\\
% \DescribeMacro{\mdyyyydate}
% The declaration |\mdyyyydate| will redefine |\today| to produce
% the current date displayed in the form 3/8/2000\\
% \DescribeMacro{\mmddyydate}
% The declaration |\mmddyydate| will redefine |\today| to produce
% the current date displayed in the form 03/08/00\\
% \DescribeMacro{\mdyydate}
% The declaration |\mdyydate| will redefine |\today| to produce
% the current date displayed in the form 3/8/00\\
%
% \section{Time Commands}
% There are three commands available for formatting the current time:
%
% \noindent\DescribeMacro{\xxivtime}
% |\xxivtime| prints the current time in 24hr format, e.g. 22:28\\
% \DescribeMacro{\ampmtime}
% |\ampmtime| prints the current time in 12hr format, e.g. 10:28pm\\
% \DescribeMacro{\oclock}
% |\oclock| prints the current time as a string, e.g. Twenty-Eight minutes past Ten in the
% afternoon.
%
% \section{Other Available Macros}
% There are two commands that print the name of the current
% month:
%
% \noindent\DescribeMacro{\monthname}
% |\monthname| prints the current month name in full, e.g. August, and \\
% \DescribeMacro{\shortmonthname}
% |\shortmonthname| prints the abbreviated month name, e.g. Aug.  Both
% |\monthname| and |\shortmonthname| take an optional argument (a number from 1 to 12)
% if the name of a specific month is required.  For example,
% |\monthname[6]| will produced the output: June.
%
% The day of the week is computed using the algorithm documented at
% \texttt{http://userpages.wittenburg.edu/bshelburne/Comp150/DayOfWeek.htm}.
% This algorithm works for any date between 1$^{\mathrm{st}}$ Jan, 1901 and 31$^{\mathrm{st}}$ Dec, 2099.
% The following macros display the day of week for a given date:
%
% \noindent\DescribeMacro{\dayofweekname}
% |\dayofweekname{|\meta{day}|}{|\meta{month}|}{|\meta{year}|}| prints the
% day of week for the specified date.  For example, |\dayofweekname{31}{10}{2002}|
% will produce the output: Thursday.\\
% \DescribeMacro{\shortdayofweekname}
% |\shortdayofweekname{|\meta{day}|}{|\meta{month}|}{|\meta{year}|}| prints the abbreviated name for the
% day of week for the specified date.  For example, |\shortdayofweekname{31}{10}{2002}|
% will produce the output: Thu.
%
% \DescribeMacro{\formatdate}
% The macro |\formatdate{|\meta{day}|}{|\meta{month}|}{|\meta{year}|}|\footnote{Note the
% name change since version 1.1.  The command name was changed from \texttt{$\backslash$thedate}
% to \texttt{$\backslash$formatdate}
% to avoid a name clash when using the \textsf{seminar} class file.} formats
% the specified date according to the current format of |\today|\footnote{To be more precise, $\backslash$\texttt{today}
% is defined to be $\backslash$\texttt{formatdate\{$\backslash$day\}\{$\backslash$month\}\{$\backslash$year\}}
% where $\backslash$\texttt{longdate} etc change the definition of $\backslash$\texttt{formatdate}}. (Arguments
% must all be integers.) For example, in combination with |\longdate|, the command |\formatdate{3}{6}{1970}|
% will produce the output: Wednesday 3$^{\mathrm{rd}}$ June, 1970.
%
% \paragraph{Note:} The following commands have changed since version 1.1.  They now
% take the name of a \LaTeX\ counter as the parameter, \emph{not} a \TeX\ count register\footnote{which now means you can't do,
% say, $\backslash$\texttt{ordinal\{3\}}, but you can do $\backslash$\texttt{@ordinal\{3\}} as long as you
% make \texttt{@} a letter using $\backslash$\texttt{makeatletter}}.  This
% was done to make them more consistent with |\arabic|, |\Roman| etc, and also so that counters
% displayed in any of these formats can
% now be cross-referenced using |\label| and |\ref|.
%
% \DescribeMacro{\ordinal}
% The macro |\ordinal{|\meta{counter}|}| will
% print the value of a \LaTeX\ counter \meta{counter} as an ordinal,
% \DescribeMacro{\fmtord}
% where the macro |\fmtord{|\meta{text}|}| is used to format the st,nd,rd,th bit.
% By default the ordinal is formatted as a superscript, if the package option |level|
% is used, it is level with the text.
% For example, if the current section is 4, then |\ordinal{section}| will produce
% the output: $4^{\mathrm{th}}$.
%
% \DescribeMacro{\numberstring}
% The macro |\numberstring{|\meta{counter}|}| will print the value
% of \meta{counter} as text.  E.g.\ |\numberstring{section}| will produce:
% four.
% \DescribeMacro{\Numberstring}
% The macro |\Numberstring{|\meta{counter}|}| does the same as
% |\numberstring|, but with initial letters in uppercase.  For
% example, |\Numberstring{section}| will produce: Four.
%
% \DescribeMacro{\ordinalstring}
% The macro |\ordinalstring{|\meta{counter}|}| will print the
% value of \meta{counter} as a textual ordinal.  E.g.\
% |\ordinalstring{section}| will produce: fourth.
% \DescribeMacro{\Ordinalstring}
% The macro |\Ordinalstring{|\meta{counter}|}| does the same as |\ordinalstring|,
% but with initial letters in uppercase.  For example,
% |\Ordinalstring{section}| will produce: Fourth.
%
% \section{Package Options}
%
% The following options may be passed to this package:\\[10pt]
% \begin{tabular}{@{\ttfamily}ll}
% long     & make |\today| produce long date (default)\\
% short    & make |\today| produce short date\\
% ddmmyyyy & make |\today| produce DD/MM/YYYY date\\
% dmyyyy   & make |\today| produce D/M/YYYY date\\
% ddmmyy   & make |\today| produce DD/MM/YY date\\
% dmyy     & make |\today| produce D/M/YY date\\
% text     & make |\today| produce text date\\
% us       & make |\today| produce US style date\\
% mmddyyyy & make |\today| produce MM/DD/YYYY date\\
% mdyyyy   & make |\today| produce M/D/YYYY date\\
% mmddyy   & make |\today| produce MM/DD/YY date\\
% mdyy     & make |\today| produce M/D/YY date\\
% raise    & make ordinal st,nd,rd,th appear as superscript (default)\\
% level    & make ordinal st,nd,rd,th appear level with rest of text\\
% dayofweek & make the day of week appear for |\longdate|, |\shortdate| \\
%           & or |\textdate| (default)\\
% nodayofweek & don't display the day of week.
% \end{tabular}
%
% \StopEventually{}
% \section{The Code}
% \iffalse
% First we have the driver to get the documentation
%    \begin{macrocode}
%<*driver>
\documentclass{ltxdoc}
\begin{document}
\OnlyDescription
\DocInput{datetime.dtx}
\end{document}
%</driver>
%    \end{macrocode}
% \fi
% At the start of the package, specify that the \LaTeX2e format is
% required.  And specify the package provided.
%    \begin{macrocode}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{datetime}[2002/10/30 Date Time Package version 2.0]
%    \end{macrocode}
% Define macro that performs modulo arthmetic
%    \begin{macrocode}
\newcount\@DT@modctr
\def\@modulo#1#2{%
\@DT@modctr=#1\relax
\divide \@DT@modctr by #2\relax
\multiply \@DT@modctr by #2\relax
\advance #1 by -\@DT@modctr}
%    \end{macrocode}
% Define the macro to format the |st|, |nd|, |rd| or |th| of an ordinal.
% By default, the parameter is typeset as the surrounding text.
%    \begin{macrocode}
\newcommand{\fmtord}[1]{#1}
%    \end{macrocode}
% Define the macro that prints the value of the \TeX\ count register as
% an ordinal
%    \begin{macrocode}
\newcount\@ordinalctr
\newcount\@orgargctr
\def\@ordinal#1{%
\@orgargctr=#1\relax
\@ordinalctr=#1%
\@modulo{\@ordinalctr}{100}%
\ifnum\@ordinalctr=11
\the\@orgargctr\fmtord{th}%
\else
\ifnum\@ordinalctr=12
\the\@orgargctr\fmtord{th}%
\else
\ifnum\@ordinalctr=13
\the\@orgargctr\fmtord{th}%
\else
\@modulo{\@ordinalctr}{10}%
\ifcase\@ordinalctr
\the\@orgargctr\fmtord{th}%      case 0
\or \the\@orgargctr\fmtord{st}%  case 1
\or \the\@orgargctr\fmtord{nd}%  case 2
\or \the\@orgargctr\fmtord{rd}%  case 3
\else
\the\@orgargctr\fmtord{th}%      default case
\fi
\fi
\fi
\fi
}
%    \end{macrocode}
% Define the macro that prints the value of a \TeX\ count register
% as text
%    \begin{macrocode}
\newcommand{\@@unitstring}[1]{%
\ifcase#1\relax
zero%
\or one%
\or two%
\or three%
\or four%
\or five%
\or six%
\or seven%
\or eight%
\or nine%
\fi
}

\newcommand{\@@tenstring}[1]{%
\ifcase#1\relax
\or ten%
\or twenty%
\or thirty%
\or fourty%
\or fifty%
\or sixty%
\or seventy%
\or eighty%
\or ninety%
\fi
}

\newcommand{\@@teenstring}[1]{%
\ifcase#1\relax
ten%
\or eleven%
\or twelve%
\or thirteen%
\or fourteen%
\or fifteen%
\or sixteen%
\or seventeen%
\or eighteen%
\or nineteen%
\fi
}

\newcommand{\@@Unitstring}[1]{%
\ifcase#1\relax
Zero%
\or One%
\or Two%
\or Three%
\or Four%
\or Five%
\or Six%
\or Seven%
\or Eight%
\or Nine%
\fi
}

\newcommand{\@@Tenstring}[1]{%
\ifcase#1\relax
\or Ten%
\or Twenty%
\or Thirty%
\or Fourty%
\or Fifty%
\or Sixty%
\or Seventy%
\or Eighty%
\or Ninety%
\fi
}

\newcommand{\@@Teenstring}[1]{%
\ifcase#1\relax
Ten%
\or Eleven%
\or Twelve%
\or Thirteen%
\or Fourteen%
\or Fifteen%
\or Sixteen%
\or Seventeen%
\or Eighteen%
\or Nineteen%
\fi
}

\newcount\strctr
\newcommand{\@@numberstring}[1]{%
\ifnum#1>99000
\PackageError{datetime}{Out of range}%
{This macro only works for values less than 100000}%
\else
\ifnum#1<0
\PackageError{datetime}{Negative numbers not permitted}%
{This macro does not work for negative numbers, however
you can try typing "minus" first, and then pass the modulus of
this number}%
\fi
\fi
\strctr=#1\relax \divide\strctr by 1000\relax
\ifnum\strctr>9
% #1 is greater or equal to 10000
\divide\strctr by 10
\ifnum\strctr>1
\@tenstring{\strctr}%
\strctr=#1 \divide\strctr by 10000
\ifnum\strctr>0 -\@unitstring{\strctr}\fi
\else
\strctr=#1 \divide\strctr by 1000
\@teenstring{\strctr}%
\fi
\ \@thousand%
\else
\ifnum\strctr>0 \@unitstring{\strctr}\ \@thousand\fi
\fi
\strctr=#1\relax \@modulo{\strctr}{1000}%
\divide\strctr by 100
\ifnum\strctr>0
\ifnum#1>1000 \ \fi\@unitstring{\strctr}\ \@hundred%
\fi
\strctr=#1\relax \@modulo{\strctr}{100}%
\ifnum#1>100 \ifnum\strctr>0 \ and \fi\fi
\ifnum\strctr>19
\divide\strctr by 10
\@tenstring{\strctr}%
\strctr=#1\relax \@modulo{\strctr}{10}%
\ifnum\strctr>0
-\@unitstring{\strctr}%
\fi
\else
\ifnum\strctr<10
\ifnum\strctr=0
\ifnum#1<100 \@unitstring{\strctr}\fi
\else
\@unitstring{\strctr}%
\fi
\else
\@modulo{\strctr}{10}%
\@teenstring{\strctr}%
\fi
\fi
}

\newcommand{\@numberstring}[1]{%
\let\@unitstring=\@@unitstring \let\@teenstring=\@@teenstring \let\@tenstring=\@@tenstring
\def\@hundred{hundred}\def\@thousand{thousand}%
\@@numberstring{#1}}

\newcommand{\@Numberstring}[1]{%
\let\@unitstring=\@@Unitstring \let\@teenstring=\@@Teenstring \let\@tenstring=\@@Tenstring
\def\@hundred{Hundred}\def\@thousand{Thousand}%
\@@numberstring{#1}}
%    \end{macrocode}
% Define a macro that produces an ordinal as a string.
%    \begin{macrocode}
\newcommand{\@@unitthstring}[1]{%
\ifcase#1\relax
zeroth%
\or first%
\or second%
\or third%
\or fourth%
\or fifth%
\or sixth%
\or seventh%
\or eighth%
\or nineth%
\fi
}

\newcommand{\@@tenthstring}[1]{%
\ifcase#1\relax
\or tenth%
\or twentieth%
\or thirtieth%
\or fourtieth%
\or fiftieth%
\or sixtieth%
\or seventieth%
\or eightieth%
\or ninetieth%
\fi
}

\newcommand{\@@teenthstring}[1]{%
\ifcase#1\relax
tenth%
\or eleventh%
\or twelfth%
\or thirteenth%
\or fourteenth%
\or fifteenth%
\or sixteenth%
\or seventeenth%
\or eighteenth%
\or nineteenth%
\fi
}

\newcommand{\@@Unitthstring}[1]{%
\ifcase#1\relax
Zeroth%
\or First%
\or Second%
\or Third%
\or Fourth%
\or Fifth%
\or Sixth%
\or Seventh%
\or Eighth%
\or Nineth%
\fi
}

\newcommand{\@@Tenthstring}[1]{%
\ifcase#1\relax
\or Tenth%
\or Twentieth%
\or Thirtieth%
\or Fourtieth%
\or Fiftieth%
\or Sixtieth%
\or Seventieth%
\or Eightieth%
\or Ninetieth%
\fi
}

\newcommand{\@@Teenthstring}[1]{%
\ifcase#1\relax
Tenth%
\or Eleventh%
\or Twelfth%
\or Thirteenth%
\or Fourteenth%
\or Fifteenth%
\or Sixteenth%
\or Seventeenth%
\or Eighteenth%
\or Nineteenth%
\fi
}

\newcommand{\@@ordinalstring}[1]{%
\ifnum#1>99000
\PackageError{datetime}{Out of range}%
{This macro only works for values less than 100000}%
\else
\ifnum#1<0
\PackageError{datetime}{Negative numbers not permitted}%
{This macro does not work for negative numbers, however
you can try typing "minus" first, and then pass the modulus of
this number}%
\fi
\fi
\strctr=#1\relax \divide\strctr by 1000\relax
\ifnum\strctr>9
% #1 is greater or equal to 10000
\divide\strctr by 10
\ifnum\strctr>1
\@tenstring{\strctr}%
\strctr=#1 \divide\strctr by 10000
\ifnum\strctr>0 -\@unitstring{\strctr}\fi
\else
\strctr=#1 \divide\strctr by 1000
\@teenstring{\strctr}%
\fi
\strctr=#1\relax \@modulo{\strctr}{1000}%
\ifnum\strctr=0\ \@thousandth\else\ \@thousand \fi
\else
\ifnum\strctr>0\relax
\@unitstring{\strctr}%
\strctr=#1\relax \@modulo{\strctr}{1000}%
\ifnum\strctr=0\ \@thousandth\else\ \@thousand\fi
\fi
\fi
\strctr=#1\relax \@modulo{\strctr}{1000}%
\divide\strctr by 100
\ifnum\strctr>0
\ifnum#1>1000 \ \fi\@unitstring{\strctr}%
\strctr=#1\relax \@modulo{\strctr}{100}%
\ifnum\strctr=0\ \@hundredth\else\ \@hundred\fi
\fi
\strctr=#1\relax \@modulo{\strctr}{100}%
\ifnum#1>100 \ifnum\strctr>0\ and \fi\fi
\ifnum\strctr>19
\@dtctr=\strctr
\divide\strctr by 10
\@modulo{\@dtctr}{10}%
\ifnum\@dtctr=0 \@tenthstring{\strctr}\else \@tenstring{\strctr}\fi
\strctr=#1\relax \@modulo{\strctr}{10}%
\ifnum\strctr>0
-\@unitthstring{\strctr}%
\fi
\else
\ifnum\strctr<10
\ifnum\strctr=0
\ifnum#1<100 \@unitthstring{\strctr}\fi
\else
\@unitthstring{\strctr}%
\fi
\else
\@modulo{\strctr}{10}%
\@teenthstring{\strctr}%
\fi
\fi
}

\newcommand{\@ordinalstring}[1]{%
\let\@unitthstring=\@@unitthstring \let\@teenthstring=\@@teenthstring \let\@tenthstring=\@@tenthstring
\let\@unitstring=\@@unitstring \let\@teenstring=\@@teenstring \let\@tenstring=\@@tenstring
\def\@hundred{hundred}\def\@thousand{thousand}%
\def\@hundredth{hundredth}\def\@thousandth{thousandth}%
\@@ordinalstring{#1}}

\newcommand{\@Ordinalstring}[1]{%
\let\@unitthstring=\@@Unitthstring \let\@teenthstring=\@@Teenthstring \let\@tenthstring=\@@Tenthstring
\let\@unitstring=\@@Unitstring \let\@teenstring=\@@Teenstring \let\@tenstring=\@@Tenstring
\def\@hundred{Hundred}\def\@thousand{Thousand}%
\def\@hundredth{Hundredth}\def\@thousandth{Thousandth}%
\@@ordinalstring{#1}}
%    \end{macrocode}
%
%    \begin{macrocode}
\newcommand{\ordinal}[1]{\expandafter\protect\expandafter\@ordinal{\expandafter\the\csname c@#1\endcsname}}
\newcommand{\ordinalstring}[1]{\expandafter\protect\expandafter\@ordinalstring{\expandafter\the\csname c@#1\endcsname}}
\newcommand{\Ordinalstring}[1]{\expandafter\protect\expandafter\@Ordinalstring{\expandafter\the\csname c@#1\endcsname}}
\newcommand{\numberstring}[1]{\expandafter\protect\expandafter\@numberstring{\expandafter\the\csname c@#1\endcsname}}
\newcommand{\Numberstring}[1]{\expandafter\protect\expandafter\@Numberstring{\expandafter\the\csname c@#1\endcsname}}
%    \end{macrocode}
% Define the macro that prints the month name.
%    \begin{macrocode}
\providecommand{\monthname}[1][\month]{%
\@orgargctr=#1\relax
\ifcase\@orgargctr
\PackageError{datetime}{Invalid Month number \the\@orgargctr}{Month numbers should go from 1 (January) to 12 (December)}%
\or January%
\or February%
\or March%
\or April%
\or May%
\or June%
\or July%
\or August%
\or September%
\or October%
\or November%
\or December%
\else \PackageError{datetime}{Invalid Month number \the\@orgargctr}{Month numbers should go from 1 (January) to 12 (December)}%
\fi}
%    \end{macrocode}
% Define the macro that prints the abbreviated month name
%    \begin{macrocode}
\newcommand{\shortmonthname}[1][\month]{%
\@orgargctr=#1\relax
\ifcase\@orgargctr
\PackageError{datetime}{Invalid Month number \the\@orgargctr}{Month numbers should go from 1 (Jan) to 12 (Dec)}%
\or Jan%
\or Feb%
\or Mar%
\or Apr%
\or May%
\or Jun%
\or Jul%
\or Aug%
\or Sept%
\or Oct%
\or Nov%
\or Dec%
\PackageError{datetime}{Invalid Month number \the\@orgargctr}{Month numbers should go from 1 (Jan) to 12 (Dec)}%
\fi}
%    \end{macrocode}
% Define macros needed to compute the weekday
%    \begin{macrocode}
% Using the algorithm documented at
% http://userpages.wittenberg.edu/bshelburne/Comp150/DayofWeek.htm

\newcount\@dtctr

\newif\ifleapyear

% \testifleapyear{<year>}
\newcommand{\testifleapyear}[1]{%
\leapyearfalse
\@year=#1\relax
\@dtctr=\@year
\@modulo{\@dtctr}{400}%
\ifnum\@dtctr=0\relax
\leapyeartrue %         year mod 400 = 0 => leap year
\else
\@dtctr=\@year
\@modulo{\@dtctr}{100}%
\ifnum\@dtctr=0\relax
\leapyearfalse %        year mod 100 = 0 && year mod 400 != 0 => not a leap year
\else
\@dtctr=\@year
\@modulo{\@dtctr}{4}%
\ifnum\@dtctr=0\relax
\leapyeartrue %         year mod 4 = 0 && year mod 100 != 0 => leap year
\fi
\fi
\fi
}

\newcount\dayofyear

% \computedayofyear{<day>}{<month>}{<year>}

\newcommand{\computedayofyear}[3]{%
\testifleapyear{#3}%
\dayofyear=0\relax
\@day=#1\relax \@month=#2\relax \@year=#3\relax
\ifcase\@month
\or
\or \advance\dayofyear by 31\relax
\or \advance\dayofyear by 59\relax
\or \advance\dayofyear by 90\relax
\or \advance\dayofyear by 120\relax
\or \advance\dayofyear by 151\relax
\or \advance\dayofyear by 181\relax
\or \advance\dayofyear by 212\relax
\or \advance\dayofyear by 243\relax
\or \advance\dayofyear by 273\relax
\or \advance\dayofyear by 304\relax
\or \advance\dayofyear by 334\relax
\else
\PackageError{datetime}{Invalid month number}{The second argument to \protect\computedayofyear
should lie in the range 1-12}
\fi
\ifnum\@month>2\relax
\ifleapyear\advance\dayofyear by 1\relax\fi
\fi
\advance\dayofyear by \@day\relax
}

\newcount\dayofweek

% \computedayofweek{<day>}{<month>}{<year>}

\newcommand{\computedayofweek}[3]{%
\computedayofyear{#1}{#2}{#3}%
\@dtctr=#3\relax
\advance\@dtctr by -1901\relax
\@modulo{\@dtctr}{28}%
\dayofweek=\@dtctr
\divide\dayofweek by 4\relax
\advance\dayofweek by \@dtctr
\advance\dayofweek by 2\relax
\@modulo{\dayofweek}{7}%
\advance\dayofweek by \dayofyear
\advance\dayofweek by -1\relax
\@modulo{\dayofweek}{7}%
\advance\dayofweek by 1\relax}

\newcommand{\dayofweekname}[3]{%
\computedayofweek{#1}{#2}{#3}%
\ifcase\dayofweek
\or Sunday%
\or Monday%
\or Tuesday%
\or Wednesday%
\or Thursday%
\or Friday%
\or Saturday%
\fi}

\newcommand{\shortdayofweekname}[3]{%
\computedayofweek{#1}{#2}{#3}%
\ifcase\dayofweek
\or Sun%
\or Mon%
\or Tue%
\or Wed%
\or Thu%
\or Fri%
\or Sat%
\fi}
%    \end{macrocode}
% Now define the declarations that redefine |\today|
%    \begin{macrocode}
\newif\ifshowdow % show the day of week if true

\newcommand{\formatdate}[3]{}

\renewcommand{\today}{\formatdate{\day}{\month}{\year}}

\newcount\@day
\newcount\@month
\newcount\@year

\newcommand{\longdate}{%
\renewcommand{\formatdate}[3]{\ifshowdow\dayofweekname{##1}{##2}{##3} \fi
\@day=##1\relax\@month=##2\relax\@year=##3\relax
\@ordinal{\@day}\ \monthname[\@month], \the\@year}}

\newcommand{\shortdate}{%
\renewcommand{\formatdate}[3]{\ifshowdow\shortdayofweekname{##1}{##2}{##3} \fi
\@day=##1\relax\@month=##2\relax\@year=##3\relax
\@ordinal{\@day}\ \shortmonthname[\@month], \the\@year}}

\newcommand{\ddmmyyyydate}{%
\renewcommand{\formatdate}[3]{\@day=##1\relax\@month=##2\relax\@year=##3\relax
\ifnum\@day<10 0\fi\the\@day/%
\ifnum\@month<10 0\fi\the\@month/%
\the\@year}}

\newcommand{\dmyyyydate}{%
\renewcommand{\formatdate}[3]{\@day=##1\relax\@month=##2\relax\@year=##3\relax
\the\@day/\the\@month/\the\@year}}

\newcommand{\ddmmyydate}{\renewcommand{\formatdate}[3]{%
\@day=##1\relax\@month=##2\relax\@year=##3\relax
\@dtctr=\@year%
\@modulo{\@dtctr}{100}%
\ifnum\@day<10 0\fi\the\@day/%
\ifnum\@month<10 0\fi\the\@month/%
\ifnum\@dtctr<10 0\fi\the\@dtctr}}

\newcommand{\dmyydate}{\renewcommand{\formatdate}[3]{%
\@day=##1\relax\@month=##2\relax\@year=##3\relax
\@dtctr=\@year%
\@modulo{\@dtctr}{100}%
\the\@day/\the\@month/\ifnum\@dtctr<10 0\fi\the\@dtctr}}

\newcommand{\textdate}{%
\renewcommand{\formatdate}[3]{\ifshowdow\dayofweekname{##1}{##2}{##3} the \fi
\@day=##1\relax\@month=##2\relax\@year=##3\relax
\@Ordinalstring\@day\ of \monthname[\@month], \@Numberstring\@year}}

\newcommand{\usdate}{%
\renewcommand{\formatdate}[3]{\@day=##1\relax\@month=##2\relax\@year=##3\relax
\monthname[\@month]\ \the\@day, \the\@year}}

\newcommand{\mmddyyyydate}{%
\renewcommand{\formatdate}[3]{\@day=##1\relax\@month=##2\relax\@year=##3\relax
\ifnum\@month<10 0\fi\the\@month/%
\ifnum\@day<10 0\fi\the\@day/%
\the\@year}}

\newcommand{\mdyyyydate}{%
\renewcommand{\formatdate}[3]{\@day=##1\relax\@month=##2\relax\@year=##3\relax
\the\@month/\the\@day/\the\@year}}

\newcommand{\mmddyydate}{\renewcommand{\formatdate}[3]{%
\@day=##1\relax\@month=##2\relax\@year=##3\relax
\@dtctr=\@year%
\@modulo{\@dtctr}{100}%
\ifnum\@month<10 0\fi\the\@month/%
\ifnum\@day<10 0\fi\the\@day/%
\ifnum\@dtctr<10 0\fi\the\@dtctr}}

\newcommand{\mdyydate}{\renewcommand{\formatdate}[3]{%
\@day=##1\relax\@month=##2\relax\@year=##3\relax
\@dtctr=\@year%
\@modulo{\@dtctr}{100}%
\the\@month/\the\@day/\ifnum\@dtctr<10 0\fi\the\@dtctr}}


%    \end{macrocode}
% Specify the package options, specify default options and process
%    \begin{macrocode}
\DeclareOption{long}{\longdate}
\DeclareOption{short}{\shortdate}
\DeclareOption{ddmmyyyy}{\ddmmyyyydate}
\DeclareOption{dmyyyy}{\dmyyyydate}
\DeclareOption{ddmmyy}{\ddmmyydate}
\DeclareOption{dmyy}{\dmyydate}
\DeclareOption{text}{\textdate}
\DeclareOption{us}{\usdate}
\DeclareOption{mmddyyyy}{\mmddyyyydate}
\DeclareOption{mdyyyy}{\mdyyyydate}
\DeclareOption{mmddyy}{\mmddyydate}
\DeclareOption{mdyy}{\mdyydate}
\DeclareOption{level}{\renewcommand{\fmtord}[1]{#1}}
\DeclareOption{raise}{\renewcommand{\fmtord}[1]{$^{\mathrm{#1}}$}}
\DeclareOption{dayofweek}{\showdowtrue}
\DeclareOption{nodayofweek}{\showdowfalse}

\ExecuteOptions{long,raise,dayofweek}
\ProcessOptions
%    \end{macrocode}
% Define commands to print the current time.
%    \begin{macrocode}
\newcommand{\xxivtime}{%
\@dtctr=\time%
\divide\@dtctr by 60
\ifnum\@dtctr<10 0\fi\the\@dtctr:%
\@dtctr=\time%
\@modulo{\@dtctr}{60}%
\ifnum\@dtctr<10 0\fi\the\@dtctr%
}

\newcommand{\ampmtime}{%
\@dtctr=\time%
\divide\@dtctr by 60
\ifnum\@dtctr>12
\advance\@dtctr by -12
\the\@dtctr:%
\@dtctr=\time%
\@modulo{\@dtctr}{60}%
\ifnum\@dtctr<10 0\fi\the\@dtctr pm%
\else
\the\@dtctr:%
\@dtctr=\time%
\@modulo{\@dtctr}{60}%
\ifnum\@dtctr<10 0\fi\the\@dtctr am%
\fi
}

\newcommand{\amstring}{in the morning}
\newcommand{\pmstring}{in the afternoon}
\newcommand{\halfpast}{Half past}
\newcommand{\quarterpast}{Quarter past}
\newcommand{\quarterto}{Quarter to}
\newcommand{\noon}{Noon}
\newcommand{\midnight}{Midnight}
\newcommand{\oclockstring}{O'Clock}

\newcount\@hours
\newcount\@minutes

\newcommand{\oclock}{%
\ifnum\time=0
\midnight
\else
\ifnum\time=720
\noon
\else
\@dtctr=\time%
\divide\@dtctr by 60
\ifnum\@dtctr>12
% Afternoon
\advance\@dtctr by -12
\@hours=\@dtctr
\@dtctr=\time%
\@modulo{\@dtctr}{60}%
\@minutes=\@dtctr
\ifnum\@minutes=30
\halfpast\ \@Numberstring{\@hours}\ \pmstring
\else
\ifnum\@minutes<30
\ifnum\@minutes=0
\@Numberstring{\@hours} \oclockstring \pmstring
\else
\ifnum\@minutes=15
\quarterpast\ \@Numberstring{\@hours}\ \pmstring
\else
\@Numberstring{\@minutes}\
\ifnum\@minutes=1\relax minute\else minutes\fi\ past
\@Numberstring{\@hours}\
\pmstring%
\fi
\fi
\else
\multiply\@minutes by -1\relax
\advance\@minutes by 60\relax
\advance\@hours by 1\relax
\ifnum\@minutes=15
\quarterto\ \@Numberstring{\@hours}\ \pmstring
\else
\@Numberstring{\@minutes}\
\ifnum\@minutes=1\relax minute\else minutes\fi\ to
\@Numberstring{\@hours}\ \pmstring
\fi
\fi
\fi
\else
% Morning
\@hours=\@dtctr
\@dtctr=\time%
\@modulo{\@dtctr}{60}%
\@minutes=\@dtctr
\ifnum\@hours=0 \@hours=12\fi
\ifnum\@minutes=30
\halfpast\ \@Numberstring{\@hours}\ \amstring
\else
\ifnum\@minutes<30
\ifnum\@minutes=15
\quarterpast\ \@Numberstring{\@hours}\ \amstring
\else
\ifnum\@minutes=0
\@Numberstring{\@hours}\ \oclockstring\ \amstring
\else
\@Numberstring{\@minutes}\
\ifnum\@minutes=1\relax minute\else minutes\fi\ past
\@Numberstring{\@hours}\
\amstring%
\fi
\fi
\else
\multiply\@minutes by -1\relax
\advance\@minutes by 60\relax
\advance\@hours by 1\relax
\ifnum\@minutes=15
\quarterto\ \@Numberstring{\@hours}\ \amstring
\else
\@Numberstring{\@minutes}\
\ifnum\@minutes=1\relax minute\else minutes\fi\ to
\@Numberstring{\@hours}\ \amstring
\fi
\fi
\fi
\fi
\fi
\fi
}
%    \end{macrocode}
%\Finale
\endinput
