3

I have created an almost perfect 24-hour format time counter; only to discover unwanted white-spaces appearing around the values. How do I remove them?

\documentclass{article}
\newcounter{hour}
\newcounter{minute}
\newcommand{\Time}[2]{
    \setcounter{hour}{#1}
    \setcounter{minute}{#2}
}
\newcommand*{\Next}[1][20]{
    \addtocounter{minute}{#1}
    {
        \ifnum\value{minute}>59
            \refstepcounter{hour}
            \addtocounter{minute}{-60}
        \else\relax
        \fi
    }
    {
        \ifnum\value{minute}=0
            \ifnum\value{hour}<10
                0\thehour:\theminute0
            \else
                \thehour:\theminute0
            \fi
        \else
            \ifnum\value{hour}<10
                0\thehour:\theminute
            \else
                \thehour:\theminute
            \fi
        \fi
    }
}
\begin{document}
    \Time{8}{0}
    Given time is \thehour:\theminute, properly written as \Next[0]. Next default time slot is at \Next. Next slot in 10 minutes is at \Next[10].
\end{document}

6
  • tex.stackexchange.com/questions/7453/… Commented Nov 30 at 8:18
  • @JosephWright I tried adding % to the end of the lines. Did not work! Commented Nov 30 at 8:24
  • Without an updated question, it's hard to know if you got all of the line ends correct (if I edit the code in the question to terminate all lines correctly, it seems to be OK). Commented Nov 30 at 8:24
  • @JosephWright Yes it does! Thank you. Earlier I only ended the printing lines 0\thehour:\theminute0. However, now I did for every line and it works. If you kindly mention for exactly which lines are making the problem, I will be glad to accept your answer. Commented Nov 30 at 8:43
  • It looks like the algorithm has another issue. If I run it with \Time{8}{0} Next slot in 110 minutes is at \Next[110]. I end up with something like Next slot in 110 minutes is at 09:70. :) Commented Nov 30 at 13:39

1 Answer 1

3

You have several unprotected endlines, but also redundant {...} groups. And the code can be streamlined.

\documentclass{article}
\newcounter{hour}
\newcounter{minute}

\newcommand{\Time}[2]{%
  \setcounter{hour}{#1}%
  \setcounter{minute}{#2}%
}
\newcommand*{\Next}[1][20]{%
  \addtocounter{minute}{#1}%
  \ifnum\value{minute}>59
    \stepcounter{hour}%
    \addtocounter{minute}{-60}%
  \fi
  \ifnum\value{hour}<10 0\fi\thehour
  :%
  \ifnum\value{minute}<10 0\fi\theminute
}
\begin{document}

\Time{8}{0}

Given time is \thehour:\theminute, properly written as \Next[0].
Next default time slot is at \Next. Next slot in 10 minutes is at \Next[10].

\Next[29] -- \Next[1]

\end{document}

Note \stepcounter rather than \refstepcounter.

However, this won't work with an increment of more than 60 minutes and the hour would be wrong if we go past midnight.

\documentclass{article}

\ExplSyntaxOn
\NewDocumentCommand{\settime}{mm}
 {
  \subhajit_time_set:nn {#1} {#2}
 }
\NewDocumentCommand{\nexttime}{O{20}}
 {
  \subhajit_time_next:n {#1}
 }

\int_new:N \g_subhajit_time_hour_int
\int_new:N \g_subhajit_time_minute_int

\cs_new_protected:Nn \subhajit_time_set:nn
 {
  \int_gset:Nn \g_subhajit_time_hour_int {#1}
  \int_gset:Nn \g_subhajit_time_minute_int {#2}
 }

\cs_new_protected:Nn \subhajit_time_next:n
 {
  \int_gadd:Nn \g_subhajit_time_minute_int {#1}
  % we might go beyond 60
  \int_gset:Nn \g_subhajit_time_hour_int
   {
    \g_subhajit_time_hour_int % current hour
    +
    \int_div_truncate:nn { \g_subhajit_time_minute_int } {60} % plus ...
   }
  \int_gset:Nn \g_subhajit_time_hour_int
   {% maybe we go beyond 24
    \int_mod:nn { \g_subhajit_time_hour_int } {24}
   }
  \int_gset:Nn \g_subhajit_time_minute_int
   {% reduce modulo 60
    \int_mod:nn { \g_subhajit_time_minute_int } {60}
   }
  % print
  \int_compare:nT { \g_subhajit_time_hour_int < 10 } {0}
  \int_to_arabic:n { \g_subhajit_time_hour_int }
  :
  \int_compare:nT { \g_subhajit_time_minute_int < 10 } {0}
  \int_to_arabic:n { \g_subhajit_time_minute_int }
 }
\ExplSyntaxOff

\begin{document}

\settime{8}{0}

\nexttime[0]. 

Next default time slot is at \nexttime. Next slot in 10 minutes is at \nexttime[10].

\nexttime[29] -- \nexttime[1]

\nexttime[654]

\nexttime[654]

\end{document}

5
  • A cordial thank for reopening the question and sincere appreciation for streamlining the code. A couple of follow-up questions: Commented Nov 30 at 9:46
  • 1. What is the difference between \refstepcounter and \stepcounter? Commented Nov 30 at 9:47
  • 1
    Refstep additionally sets things up for referencing the counter, which makes no sense here Commented Nov 30 at 9:49
  • 2. This is the second time you engaged ExplSyntax code in my questions. Please share a resource to learn it. I have learned all my LaTeX knowledge from this website while doing different projects and hence lack a systematic learning. Commented Nov 30 at 9:53
  • 1
    @SubhajitPaul texdoc interface3 Commented Nov 30 at 14:10

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.