[texhax] Time calculations within LaTeX?

E. Krishnan ekmath at asianetindia.com
Sat Feb 9 06:56:01 CET 2008


On Sat, 9 Feb 2008, Richard Hartmann wrote:

> Hi all,
>
> I was wondering if I could do simple time calculations from within LaTeX.
> For example, 10:15-16:00 should result in 5.75, while 10:15-01:00 should
> result in 14.75.

Please try this:

  \usepackage{ifthen}
  \usepackage{calc}

  \newcounter{bh}
  \newcounter{bm}
  \newcounter{eh}
  \newcounter{em}
  \newcounter{dh}
  \newcounter{dm}
  \newcounter{hrdec}


  \def\timediff#1:#2-#3:#4 {%
   \setcounter{bh}{#1}
   \setcounter{bm}{#2}
   \setcounter{eh}{#3}
   \setcounter{em}{#4}
   \ifthenelse{\value{eh}>\value{bh}\and\value{em}>\value{bm}}
     {\setcounter{dh}{\value{eh}-\value{bh}}
      \setcounter{dm}{\value{em}-\value{bm}}}
     {\ifthenelse{\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}}
        {\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}



Then \timediff10:15-16:00 gives 5.75 and \timediff10:15-01:00 gives 14.75.


-- 
Krishnan


More information about the texhax mailing list