[texhax] Time calculations within LaTeX?
Philip G. Ratcliffe
philip.ratcliffe at fastwebnet.it
Sun Feb 10 01:08:09 CET 2008
There are still a couple of problems with your script as I see it:
1. It doesn't round off properly it only truncates (which may, of course,
actually be desired).
2. It adds spurious space and needs a few percent symbols at line endings.
Also the requirement of a space following the arguments easily leads to
errors.
Finally, there's a certain amount of overkill here - compare the following
two versions (the third time difference is 0.966, which should round of to
0.97):
\documentclass{article}
\usepackage{calc}
\newcounter{dh}
\newcounter{dm}
\newcounter{em}
\def\TimeDiffTrunc(#1:#2-#3:#4){%
\setcounter{dm}{(60*(#3-#1)+#4-#2)*5/3}%
\ifnum\value{dm}<0\relax\setcounter{dm}{2400+\value{dm}}\fi
\setcounter{dh}{\value{dm}/100}%
\setcounter{dm}{\value{dm}-\value{dh}*100}%
\thedh.\ifnum\value{dm}<10\relax0\fi\thedm
}
\def\TimeDiffRound(#1:#2-#3:#4){%
\setcounter{em}{(60*(#3-#1)+#4-#2)*50/3}%
\ifnum\value{em}<0\relax\setcounter{em}{24000+\value{em}}\fi
\setcounter{dm}{\value{em}/10}%
\setcounter{em}{\value{em}-\value{dm}*10}%
\ifnum\value{em}>4\relax\stepcounter{dm}\fi
\setcounter{dh}{\value{dm}/100}%
\setcounter{dm}{\value{dm}-\value{dh}*100}%
\thedh.\ifnum\value{dm}<10\relax0\fi\thedm
}
\begin{document}
AT=\TimeDiffTrunc(10:10-5:05),
BT=\TimeDiffTrunc(5:05-10:10),
CT=\TimeDiffTrunc(1:02-2:00).
AR=\TimeDiffRound(10:10-5:05),
BR=\TimeDiffRound(5:05-10:10),
CR=\TimeDiffRound(1:02-2:00).
\end{document}
Cheers, Phil
More information about the texhax
mailing list