# [texhax] Time calculations within LaTeX?

E. Krishnan ekmath at asianetindia.com
Sat Feb 9 13:49:01 CET 2008

On Sat, 9 Feb 2008, Richard Hartmann wrote:

> On Feb 9, 2008 6:56 AM, E. Krishnan <ekmath at asianetindia.com> wrote:
>
>
>> Then \timediff10:15-16:00 gives 5.75 and \timediff10:15-01:00 gives 14.75.
>
> I found two issues with the function, one of which I could fix :)
>
> You need to reinitialize dh, dm, hrdec for the function to give the correct
> result when called more than once in a tex file.
>
> \setcounter{dh}{0}
> \setcounter{dm}{0}
> \setcounter{hrdec}{0}
>
> does the trick.

But I didn't have this issue.

> The other issue is that when the minute or hour values are equal, you
> get a null result, no matter what the other parameter is.

Oops! Sort of overlooked that case. Here's the modification to take care
of this:

\def\timediff#1:#2-#3:#4 {%
\setcounter{bh}{#1}
\setcounter{bm}{#2}
\setcounter{eh}{#3}
\setcounter{em}{#4}
\ifthenelse{$$\value{eh}>\value{bh}\or\value{eh}=\value{bh}$$
\and$$\value{em}>\value{bm}\or\value{em}=\value{bm}$$}
{\setcounter{dh}{\value{eh}-\value{bh}}
\setcounter{dm}{\value{em}-\value{bm}}}
{\ifthenelse{$$\value{eh}>\value{bh}\or\value{eh}=\value{bh}$$
\and\value{em}<\value{bm}}
{\setcounter{dh}{\value{eh}-\value{bh}-1}
\setcounter{dm}{60+\value{em}-\value{bm}}}
{\ifthenelse{\value{eh}<\value{bh}
\and$$\value{em}>\value{bm}\or\value{em}=\value{bm}$$}
{\setcounter{dh}{24+\value{eh}-\value{bh}}
\setcounter{dm}{\value{em}-\value{bm}}}
{\ifthenelse{\value{eh}<\value{bh}\and\value{em}<\value{bm}}
{\setcounter{dh}{23+\value{eh}-\value{bh}}
\setcounter{dm}{60+\value{em}-\value{bm}}}}}{}}
\setcounter{hrdec}{\value{dm}*5/3}
\thedh.\thehrdec}

--
Krishnan