TeX - LaTeX Asked by user186178 on April 2, 2021
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 thetime
, I know how to do that.
Please: Answers only for TeX, not LaTeX.
newcounttimebase
timebase=thetime
newcounthour
hour=thetime
dividehour by 60
newcountminute
minute=0
newcountsecond
second=0
newcountreduce
reduce=60
newcountstart%
newcountstop%
defwhile[#1,#2,#3]{%
start=#1 %
stop=#2 %
defdoit{#3}%
loop ifnumstart<thestop doit advancestart by 1repeat%
start=0 stop=0}%
while[0,24,ifnumthehour=0 elseifnumthehour=thestartmultiplyreduce by start vskipbaselineskipadvancetimebase by -reducefifi]%
minute=thetimebase
Output:
The Time: thetime%
Current time:
ifnumthehour<10 0fi%
thehour:%
ifnumtheminute<10 0fi%
theminute:%
ifnumthesecond<10 0fi%
thesecond
Seconds are not defined yet.
bye
Correct answer by user186178 on April 2, 2021
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:
longdeffirstoftwo#1#2{#1}
longdefsecondoftwo#1#2{#2}
defminutes{computeminutestime}
defcomputeminutes#1{%
ifnum#1<60
expandafterfirstoftwo
else
expandaftersecondoftwo
fi
{#1}{expandaftercomputeminutesexpandafter{thenumexpr#1-60relax}}%
}
defhours{expandaftercomputehourstime}
defcomputehours#1{thenumexpr(#1-computeminutes{#1})/60relax}
deflongtime{printlongtimetime}
defprintlongtime#1{%
ifnumcomputehours{#1}<10 0ficomputehours{#1}%
:%
ifnumcomputeminutes{#1}<10 0ficomputeminutes{#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.
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
Answered by egreg on April 2, 2021
I give much simpler calculation of minutes
and hours
than egreg:
defhours{numexpr (time+30)/60-1relax}
defminutes{numexpr time-60*hours relax}
thehours, theminutes
bye
Answered by wipet on April 2, 2021
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
defminute{numberexpandaftermin@ithetime{}{}{};}
defmin@i#1#2#3#4#5;{min@ii#1#2#3#4;}
defmin@ii#1;{ifnum#1>59 expandaftermin@iinumbermin@decr@hourelse #1fi}
defmin@decr@hourelse#1fi{fimin@reverse#1{}{};}
defmin@reverse#1#2#3#4#5;{min@minus@sixty#4#3#2#100;}
defmin@afterfi#1#2fi{fimin@re@reverse#1{}{};}
defmin@re@reverse#1#2#3#4#5;{#4#3#2#1;}
defmin@minus@sixty#1#2#3#4#5;{ifcase#2
min@minus@one{#3}{#4}{#14}ormin@minus@one{#3}{#4}{#15}%
ormin@minus@one{#3}{#4}{#16}ormin@minus@one{#3}{#4}{#17}%
ormin@minus@one{#3}{#4}{#18}ormin@minus@one{#3}{#4}{#19}%
ormin@afterfi{#10#3#4}ormin@afterfi{#11#3#4}%
ormin@afterfi{#12#3#4}ormin@afterfi{#13#3#4}%
fi}
defmin@minus@one#1#2#3{ifcase#1 min@minus@one{#2}{}{#39}% 0
ormin@afterfi{#30#2}ormin@afterfi{#31#2}ormin@afterfi{#32#2}%
ormin@afterfi{#33#2}ormin@afterfi{#34#2}ormin@afterfi{#35#2}%
ormin@afterfi{#36#2}ormin@afterfi{#37#2}ormin@afterfi{#38#2}%
fi}
catcode`@=12
edefx{minute}showx
end
The algorithm is really stupid. It takes the tokens from the expansion of thetime
, 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 :)
Answered by Phelype Oleinik on April 2, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP