Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have this code

\documentclass{article}
\usepackage{datetime}
\begin{document}
\currenttime
\par
\formattime{18}{0}{0} 
\end{document}

and want to compare these times if the time is before or after 18 o clock.

share|improve this question

2 Answers 2

up vote 9 down vote accepted

With datetime you can print times; if you want an expandable test for times here's a possibility:

\documentclass{article}
\usepackage{datetime}
\usepackage{pdftexcmds}

\makeatletter

\newif\iftim@seconds
\@ifpackageloaded{datetime}
  {\@ifundefined{pdfcreationdate}\tim@secondsfalse\tim@secondstrue}
  {\ifdefined\pdfcreationdate\tim@secondstrue\fi}

\newcommand{\comparetime}[6]{%
  % #1=hour, #2=minutes, #3=seconds
  % #4=code if time is before current time
  % #5=code if time is equal to current time
  % #6=code if time is after current time
  \ifcase\pdf@strcmp{\tim@giventime{#1}{#2}{#3}}{\tim@currenttime}
    #5% case 0
  \or
    #6% case 1
  \else
    #4% case -1
  \fi
}

\iftim@seconds
  \def\tim@currenttime{%
    \expandafter\tim@currenttime@aux\pdfcreationdate\@nil
  }
  \expandafter\def\expandafter\tim@currenttime@aux\string D:#1#2#3#4#5#6#7#8#9\@nil{%
    \tim@currenttime@auxi#9\@nil
  }
  \def\tim@currenttime@auxi#1#2#3#4#5#6#7\@nil{%
    \number\numexpr #1#2*3600+#3#4*60+#5#6\relax
  }
  \def\tim@giventime#1#2#3{\number\numexpr#1*3600+#2*60+#3\relax}
\else
  \def\tim@currenttime{\number\time}
  \def\tim@giventime#1#2#3{\number\numexpr#1*60+#2\relax}
\fi

\begin{document}

\currenttime

\comparetime{15}{14}{0}{before}{equal}{after}

\comparetime{15}{40}{0}{before}{equal}{after}

\end{document}

The code is independent of datetime and it works with all engines; however, with XeLaTeX, seconds are not available so times that differ only for the seconds part are considered to be equal.

The example was compiled at the shown time (which is the only reason for loading datetime).

enter image description here

share|improve this answer
    
Thank you very much. That works perfect :) –  Tim S. Aug 15 at 13:42

The seconds are irrelevant because TeX knows only minutes from midnight when it is executed, no seconds. This information is stored in \time primitive register. I created the \giventime macro which is similar to mentioned \formattime, but the third parameter (seconds) is simply ignored.

\newcount\minutes
\def\giventime#1#2#3{\minutes=#1 \multiply\minutes by60 \advance\minutes by#2 }

\giventime{18}{59}{0}

\ifnum\time<\minutes Current time is before given \fi
\ifnum\time=\minutes Current time is in the same minute as given \fi
\ifnum\time>\minutes Current time is after given \fi

Note, there is no @ in my macros :-)

Edit My solution shows that if we know what is desired and we know TeX primitives then the solution could be much more simple and straghtforward than using LaTeX packages. Using datetime, you must compare two strings and the solution includes complicated macros like in the accepted answer. Using TeX primitives you are comparing simply two numbers.

share|improve this answer
    
Thank you, your answer is much appreciated, too :) –  Tim S. Aug 15 at 19:22
    
@your edit: Yes that's true. I would have accepted your answer but there was already a working one. Btw: you could improve your code by checking one if or in worst case two if. Your does always check three if. Here what I mean: \ifnum\time<\minutes Current time is before given \else \ifnum\time=\minutes Current time is in the same minute as given \else Current time is after given \fi \fi –  Tim S. Aug 16 at 12:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.