Google Groups unterstützt keine neuen Usenet-Beiträge oder ‑Abos mehr. Bisherige Inhalte sind weiterhin sichtbar.

Time Calculation in LaTex

82 Aufrufe
Direkt zur ersten ungelesenen Nachricht

Chitarup

ungelesen,
03.02.2008, 14:55:1203.02.08
an
Hi,

is there a possibility to calculate time in LaTex. Let's say I define
a start time, add some minutes to it and use the current value in the
document. Something like:

\def\startTime{9:00}
\add\startTime{0:30}
\startTime % output 9:30

A solution of this would be very helpful.

Thanks,
Chitarup

Nicola Talbot

ungelesen,
03.02.2008, 15:29:2103.02.08
an
On Feb 3, 7:55 pm, Chitarup <chita...@googlemail.com> wrote:
> is there a possibility to calculate time in LaTex. Let's say I define
> a start time, add some minutes to it and use the current value in the
> document.

Hi,

It's possible there's a neater way of doing this using the calc
package, but try the following

\documentclass{article}

\begin{document}

\newcount\hours
\newcount\minutes

\def\gettime#1:#2\relax#3#4{\def#3{#1}\def#4{#2}}

\newcommand{\add}[2]{\expandafter\gettime#1\relax{\hrs}{\mins}
\expandafter\gettime#2\relax{\addhrs}{\addmins}%
\hours=\hrs\relax
\advance\hours by \addhrs\relax
\minutes=\mins\relax
\advance\minutes by \addmins\relax
\ifnum\minutes>60\relax
\advance\minutes by -60\relax
\advance\hours by 1\relax
\else
\ifnum\minutes<0\relax
\advance\minutes by 60\relax
\advance\hours by -1\relax
\fi
\fi
\ifnum\hours>24\relax
\advance\hours by -24\relax
\else
\ifnum\hours<0\relax
\advance\hours by 24\relax
\fi
\fi
\ifnum\minutes<10\relax
\edef#1{\number\hours:0\number\minutes}%
\else
\edef#1{\number\hours:\number\minutes}%
\fi}

\def\startTime{9:00}
\add\startTime{0:30}
\startTime

\end{document}

Regards
Nicola Talbot

Chitarup

ungelesen,
04.02.2008, 04:14:2604.02.08
an
Hello Nicola,

far out!!!! This is exactly what need.
Thank you very much.

Best whishes,
Chitarup

Nicola Talbot

ungelesen,
04.02.2008, 04:18:4304.02.08
an
On Feb 3, 8:29 pm, Nicola Talbot <n...@cmp.uea.ac.uk> wrote:
> \ifnum\minutes>60\relax

Sorry, that should have been
\ifnum\minutes>59\relax

> \ifnum\hours>24\relax
Likewise, that should be
\ifnum\hours>23\relax

Regards
Nicola Talbot


Michele Dondi

ungelesen,
04.02.2008, 12:43:2604.02.08
an

Perhaps not exactly what you expected, but especially if you also need
to work with dates, then you may go with PerlTeX and one out of a
zillion date calculating modules, like DateTime, or Date::Calc.


Michele
--
>It's because the universe was programmed in C++.
No, no, it was programmed in Forth. See Genesis 1:12:
"And the earth brought Forth ..."
- Robert Israel in sci.math, thread "Why numbers?"

Chitarup

ungelesen,
04.02.2008, 17:09:1904.02.08
an
Hello Nicola,

thanks for the correction. Unfortunately the code doesn't work within
a table:

\def\startTime{9:45}
\begin{tabular}{|p{1cm}|p{4cm}|}
\hline
Time &
Content\\
\hline
\hline
\add\startTime{0:05} \textbf{\startTime} & \quad \\
\hline
\add\startTime{0:05} \textbf{\startTime} & \quad \\
\hline
\add\startTime{0:05} \textbf{\startTime} & \quad \\
\hline
\end{tabular}

The output is always 9:50. Outside of an table it works fine. Any
ideas?

Regards,
Chitarup

Nicola Talbot

ungelesen,
04.02.2008, 17:58:1304.02.08
an
On Feb 4, 10:09 pm, Chitarup <chita...@googlemail.com> wrote:
> thanks for the correction. Unfortunately the code doesn't work within
> a table:

try changing \edef to \xdef

Regards
Nicola Talbot

Chitarup

ungelesen,
04.02.2008, 20:59:0304.02.08
an
> try changing \edef to \xdef

Hello Nicola,

yes, this is working.

Thanks,
Chitarup

Olaf Meltzer

ungelesen,
05.02.2008, 05:39:3805.02.08
an
* Nicola Talbot schrieb:

> On Feb 4, 10:09 pm, Chitarup <chita...@googlemail.com> wrote:
>> thanks for the correction. Unfortunately the code doesn't work
>> within a table:
>
> try changing \edef to \xdef

Nicola,

I think one has to \xdef\startTime[] inside the tabular as outside.
Have a look on this example, which is correct:

\documentclass{article}
\newcount\hours
\newcount\minutes

\def\gettime#1:#2\relax#3#4{\def#3{#1}\def#4{#2}}

\newcommand{\add}[2]{\expandafter\gettime#1\relax{\hrs}{\mins}
\expandafter\gettime#2\relax{\addhrs}{\addmins}%
\hours=\hrs\relax
\advance\hours by \addhrs\relax
\minutes=\mins\relax
\advance\minutes by \addmins\relax

\ifnum\minutes>59\relax


\advance\minutes by -60\relax
\advance\hours by 1\relax
\else
\ifnum\minutes<0\relax
\advance\minutes by 60\relax
\advance\hours by -1\relax
\fi
\fi

\ifnum\hours>23\relax


\advance\hours by -24\relax
\else
\ifnum\hours<0\relax
\advance\hours by 24\relax
\fi
\fi
\ifnum\minutes<10\relax

\xdef#1{\number\hours:0\number\minutes}%
\else
\xdef#1{\number\hours:\number\minutes}%
\fi}

\begin{document}

Start 22:08\def\startTime{22:08},
duration 5:53\add\startTime{05:53},
ETA (should be 4:01) calculated \startTime

\begin{table}
\centering
\begin{tabular}{rrrr}
Start 23:01 \xdef\startTime{23:01}
&duration 01:01 \add\startTime{01:01}
&ETA should be 0:02
&calculated \startTime\\
Start 02:55 \xdef\startTime{02:55}
&duration 23:05 \add\startTime{23:05}
&ETA should be 2:00
&calculated \startTime\\
\end{tabular}
\caption{Timetable}
\end{table}

\end{document}

Sincerely -- Olaf Meltzer

Olaf Meltzer

ungelesen,
05.02.2008, 05:50:4405.02.08
an
* Nicola Talbot schrieb:

> On Feb 4, 10:09 pm, Chitarup <chita...@googlemail.com> wrote:
>> thanks for the correction. Unfortunately the code doesn't work
>> within a table:
>
> try changing \edef to \xdef

Nicola,

I think one has to \xdef\startTime{} even inside the tabular.


Have a look on this example, which is correct:

\documentclass{article}
\newcount\hours
\newcount\minutes

\def\gettime#1:#2\relax#3#4{\def#3{#1}\def#4{#2}}

\newcommand{\add}[2]{\expandafter\gettime#1\relax{\hrs}{\mins}
\expandafter\gettime#2\relax{\addhrs}{\addmins}%
\hours=\hrs\relax
\advance\hours by \addhrs\relax
\minutes=\mins\relax
\advance\minutes by \addmins\relax

\ifnum\minutes>59\relax


\advance\minutes by -60\relax
\advance\hours by 1\relax
\else
\ifnum\minutes<0\relax
\advance\minutes by 60\relax
\advance\hours by -1\relax
\fi
\fi

\ifnum\hours>23\relax


\advance\hours by -24\relax
\else
\ifnum\hours<0\relax
\advance\hours by 24\relax
\fi
\fi
\ifnum\minutes<10\relax

\xdef#1{\number\hours:0\number\minutes}%
\else
\xdef#1{\number\hours:\number\minutes}%
\fi}

\begin{document}

Olaf Meltzer

ungelesen,
05.02.2008, 07:40:1305.02.08
an
* Olaf Meltzer schrieb:

> Nicola,
>
> I think one has to \xdef\startTime{} even inside the tabular.
> Have a look on this example, which is correct:

Another very small correction: First line of \newcommand{\add}...
should end with % to prevent a superfluous space wich otherwise would
prevent correct alignment.

As a result of further discussion in d.c.t.t one supposed to have a
solution to subtract times as well, and I modified your proposal in
accordance. Have a look on the combined add/subtract example.


%% LaTeX-Example: To add and subtract times
%%
%% Based on Nicola Talbots solution
%% of Chitarups problem from 2008-02-04 in ctt
%%


\documentclass{article}
\newcount\hours
\newcount\minutes

\def\gettime#1:#2\relax#3#4{\def#3{#1}\def#4{#2}}

\newcommand{\add}[2]{\expandafter\gettime#1\relax{\hrs}{\mins}%

\newcommand{\sub}[2]{\expandafter\gettime#1\relax{\hrs}{\mins}%
\expandafter\gettime#2\relax{\subhrs}{\submins}%
\hours=\hrs\relax
\advance\hours by -\subhrs\relax
\minutes=\mins\relax
\advance\minutes by -\submins\relax


\ifnum\minutes>59\relax
\advance\minutes by -60\relax
\advance\hours by 1\relax
\else
\ifnum\minutes<0\relax
\advance\minutes by 60\relax
\advance\hours by -1\relax
\fi
\fi
\ifnum\hours>23\relax
\advance\hours by -24\relax
\else
\ifnum\hours<0\relax
\advance\hours by 24\relax
\fi
\fi
\ifnum\minutes<10\relax
\xdef#1{\number\hours:0\number\minutes}%
\else
\xdef#1{\number\hours:\number\minutes}%
\fi}


\begin{document}

\section{Calculation of estimated time of arrival (ETA)}

The estimated time of arrival (ETA) is calculated as estimated
time of takeoff (ETT) plus duration.

ETT 22:08\def\startTime{22:08},


duration 5:53\add\startTime{05:53},

ETA (should be 4:01) calculated \startTime.


\section{Calculation of duration}

The duration is calculated as differenz between actual time of
arrival (ATA) and actual time of takoff (ATT).

ATT 20:05, ATA 0:15\def\startTime{00:15}, duration (should be
4:10)\sub\startTime{20:05}, calculated \startTime.


\begin{table}
\centering
\begin{tabular}{rrrr}

ETT&duration&correct&calculated\\


23:01\xdef\startTime{23:01}

&1:01\add\startTime{1:01}
&0:02
&\startTime\\
2:55\xdef\startTime{2:55}
&23:05\add\startTime{23:05}
&2:00
&\startTime\\
\end{tabular}
\caption{Calculated ETA (added times)}
\end{table}

\begin{table}
\centering
\begin{tabular}{rrrr}

ATA&ATT&correct&calculated\\
21:15\xdef\startTime{21:15}
&20:05\sub\startTime{20:05}
&1:10
&\startTime\\
0:15\xdef\startTime{0:15}
&20:05\sub\startTime{20:05}
&4:10
&\startTime\\
\end{tabular}
\caption{Calculated duration (subtracted times)}

0 neue Nachrichten