1

Someone told me, minutes were accessible in TeX, but I cannot find a command to call this.

How do I call minutes in TeX?

Please do not tell me about the construction of minutes out of \the\time, I know how to do that.

Please: Answers only for TeX, not LaTeX.

9
  • 4
    “do not tell me about the construction of minutes out of \the\time”: then how? That's the only way to access the minutes from within TeX – Phelype Oleinik Feb 10 at 1:06
  • 4
    @MatthiasBorck-Elsner No, the only time-related primitives are \year, \month, \day, and \time. With e-TeX (any TeX except Knuth's tex) you can define \def\minute{\numexpr\time-60*\numexpr\time/60\relax\relax} then \the\minute, but that's just a macro emulation on top of \the\time. With Knuth's TeX you can also get the minute, but it's less easy – Phelype Oleinik Feb 10 at 1:11
  • 5
    @MatthiasBorck-Elsner What is it with extracting the minute from \the\time that doesn't do what you want? – Phelype Oleinik Feb 10 at 1:27
  • 1
    @PhelypeOleinik I believe your definition of \minute is not entirely correct: \numexpr rounds, while \divide truncates. No difference in the first half hour, but after that... ;-) – campa Feb 10 at 9:42
  • 1
    @campa They say even a broken clock is right twice a day :) Indeed, you're right. A quick fix is \def\minute{\numexpr\expandafter\minuteaux\the\numexpr\time-60*\numexpr\time/60;} \def\minuteaux#1;{#1\ifnum#1<0+60\fi\relax} – Phelype Oleinik Feb 10 at 12:08
7

I give much simpler calculation of \minutes and \hours than egreg:

\def\hours{\numexpr (\time+30)/60-1\relax}
\def\minutes{\numexpr \time-60*\hours \relax}

\the\hours, \the\minutes

\bye
4

Well, there might be, with some recursion magic.

Here it is then, a \minute macro that works in ANY engine :)

It differs from wipet's \minutes because his macro emulates a TeX register which you can access with \the, while with this one you can't, because Knuth's TeX doesn't have fake registers like ε-TeX's \numexpr.

\catcode`\@=11
\def\minute{\number\expandafter\min@i\the\time{}{}{};}
\def\min@i#1#2#3#4#5;{\min@ii#1#2#3#4;}
\def\min@ii#1;{\ifnum#1>59 \expandafter\min@ii\number\min@decr@hour\else #1\fi}
\def\min@decr@hour\else#1\fi{\fi\min@reverse#1{}{};}
\def\min@reverse#1#2#3#4#5;{\min@minus@sixty#4#3#2#100;}
\def\min@afterfi#1#2\fi{\fi\min@re@reverse#1{}{};}
\def\min@re@reverse#1#2#3#4#5;{#4#3#2#1;}
\def\min@minus@sixty#1#2#3#4#5;{\ifcase#2
     \min@minus@one{#3}{#4}{#14}\or\min@minus@one{#3}{#4}{#15}%
  \or\min@minus@one{#3}{#4}{#16}\or\min@minus@one{#3}{#4}{#17}%
  \or\min@minus@one{#3}{#4}{#18}\or\min@minus@one{#3}{#4}{#19}%
  \or\min@afterfi{#10#3#4}\or\min@afterfi{#11#3#4}%
  \or\min@afterfi{#12#3#4}\or\min@afterfi{#13#3#4}%
  \fi}
\def\min@minus@one#1#2#3{\ifcase#1 \min@minus@one{#2}{}{#39}% 0
  \or\min@afterfi{#30#2}\or\min@afterfi{#31#2}\or\min@afterfi{#32#2}%
  \or\min@afterfi{#33#2}\or\min@afterfi{#34#2}\or\min@afterfi{#35#2}%
  \or\min@afterfi{#36#2}\or\min@afterfi{#37#2}\or\min@afterfi{#38#2}%
  \fi}
\catcode`\@=12

\edef\x{\minute}\show\x

\end

The algorithm is really stupid. It takes the tokens from the expansion of \the\time, and successively subtracts 60 from that, until what remains is less than 60, and returns that. The subtraction, in the lack of \numexpr, is done by a ten-way \ifcase that looks at the tens digit of the number, and returns the value subtracted (no arithmetic was harmed in the making of this code :)

5
  • 1
    +1 because it also works with experimental LuaMetaTeX. Nice :) – Jairo A. del Rio Feb 10 at 20:20
  • @JairoA.delRio Oh, cool! (not too surprising, as this uses really basic TeX constructs, but cool nonetheless) – Phelype Oleinik Feb 10 at 20:24
  • Does not work. Is this a fully functioning example? – user186178 Feb 14 at 0:53
  • @MatthiasBorck-Elsner It does work. See the log file. – Jairo A. del Rio Feb 14 at 1:57
  • @MatthiasBorck-Elsner Yes, it uses primitives available in TeX since early versions (1982 likely, but I'm not sure. Works on modern systems, definitely). The \show command pauses TeX to ask for interaction, so if you run from an IDE, that won't work. Try replacing the line \edef\x{\minute}\show\x by just \minute – Phelype Oleinik Feb 14 at 3:56
2

There is no expandable way to access the time in hours and minutes in Knuth TeX, which can only do arithmetic via register assignments.

Well, there might be, with some recursion magic.

It's much easier if you allow e-TeX extensions:

\long\def\firstoftwo#1#2{#1}
\long\def\secondoftwo#1#2{#2}
\def\minutes{\computeminutes\time}
\def\computeminutes#1{%
  \ifnum#1<60
    \expandafter\firstoftwo
  \else
    \expandafter\secondoftwo
  \fi
  {#1}{\expandafter\computeminutes\expandafter{\the\numexpr#1-60\relax}}%
}
\def\hours{\expandafter\computehours\time}
\def\computehours#1{\the\numexpr(#1-\computeminutes{#1})/60\relax}

\def\longtime{\printlongtime\time}
\def\printlongtime#1{%
  \ifnum\computehours{#1}<10 0\fi\computehours{#1}%
  :%
  \ifnum\computeminutes{#1}<10 0\fi\computeminutes{#1}%
}

\hours, \minutes, \longtime

\computehours{955}, \computeminutes{955}, \printlongtime{955}

\computehours{905}, \computeminutes{905}, \printlongtime{905}

\computehours{37}, \computeminutes{37}, \printlongtime{37}

\computehours{0}, \computeminutes{0}, \printlongtime{0}

\bye

Why doing the long way? Because division in \numexpr rounds, rather than truncate.

enter image description here

The final examples are meant to emulate different times.

With expl3 (that can also be used in plain TeX):

\input expl3-generic

\ExplSyntaxOn

\cs_new:Npn \minutes { \int_to_arabic:n { \c_sys_minute_int } }
\cs_new:Npn \hours { \int_to_arabic:n { \c_sys_hour_int } }
\cs_new:Npn \longtime
 {
  \int_compare:nT { \c_sys_hour_int < 10 } { 0 }
  \hours
  :
  \int_compare:nT { \c_sys_minute_int < 10 } { 0 }
  \minutes
 }

\ExplSyntaxOff
0
\newcount\timebase
\timebase=\the\time
\newcount\hour
\hour=\the\time
\divide\hour by 60
\newcount\minute
\minute=0
\newcount\second
\second=0
\newcount\reduce
\reduce=60
\newcount\start%  
\newcount\stop%  
\def\while[#1,#2,#3]{%  
  \start=#1 %  
  \stop=#2 %  
  \def\doit{#3}%  
  \loop \ifnum\start<\the\stop \doit \advance\start by 1\repeat%  
  \start=0 \stop=0}%   
\while[0,24,\ifnum\the\hour=0 \else\ifnum\the\hour=\the\start\multiply\reduce by \start \vskip\baselineskip\advance\timebase by -\reduce\fi\fi]%  
\minute=\the\timebase

Output:

The Time: \the\time%

Current time: 
\ifnum\the\hour<10 0\fi%

\the\hour:%

\ifnum\the\minute<10 0\fi%

\the\minute:%

\ifnum\the\second<10 0\fi%

\the\second


Seconds are not defined yet.
\bye
2
  • Why don't you write shorter \minute=-\hour \multiply\minute by 60 \advance\minute by \time? – Udo Wermuth Feb 14 at 16:35
  • ...and...why did I get negative scores for this question? – user186178 Mar 4 at 0:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy