[Tex/LaTex] Time of the day or time period using the package siunitx

datetimesiunitx

I would like to display the time of the day like 18h30 or 18h30m with siunitx. Is there a proper way to display time and time intervals?

I have read some discussions on the Mailing-List, but it seems to be outdated.

My personal solution was defining the following command:

\newcommand{\hms}[3]{
% Stunden nur zeigen, wenn gesetzt
\ifthenelse{\isempty{#1}}
  {}
  {\SI{#1}{\hour}}
%
% Luecke nur, wenn Stunden und Minuten
\ifthenelse{\isempty{#1} \OR \isempty{#2}}
  {}
  {\,\,}
%
% Minuten nur zeigen, wenn gesetzt
\ifthenelse{\isempty{#2}}
  {}
  {\SI{#2}{\minute}}
% %
% Luecke nur, wenn Minuten und Sekunden
\ifthenelse{\isempty{#2} \OR \isempty{#3}}
  {}
  {\,\,}
%
%Luecke nur, wenn Sekunden und Stunden aber nicht Minuten
\ifthenelse{\isempty{#2} \AND \NOT \isempty{#3} \AND \NOT \isempty{#1}}
  {\,\,}
  {}
%
% Sekunden nur zeigen, wenn gesetzt
\ifthenelse{\isempty{#3}}
  {}
  {\SI{#3}{\second}}
}

What is your opinion? How would you typeset times and time periods?

Best Answer

I've been asked about this a few times. It's an awkward one, as the format people want ('18 h 30 min', for example) is really not a single idea for expressing a time in scientific work. You'd normally want to express in one unit, so in either hours or minutes, but not both. That said, it's not too hard to construct a function \hms which works in a similar way to \ang (the multiple-unit argument applies to the later too!). This needs a code test as well as \IfNoValueTF, which is a little awkward for end users, but something like

\documentclass{article}
\usepackage{siunitx}
\ExplSyntaxOn
\NewDocumentCommand \hms { o > { \SplitArgument { 2 } { ; } } m }
  {
    \group_begin:
      \IfNoValueF {#1}
        { \keys_set:nn { siunitx } {#1} }
      \siunitx_hms_output:nnn #2
    \group_end:
  }
\cs_new_protected:Npn \siunitx_hms_output:nnn #1#2#3
  {
    \IfNoValueF {#1}
      {
        \tl_if_blank:nF {#1}
          {
            \SI {#1} { \hour }
            \IfNoValueF {#2} { ~ }
          }
      }
    \IfNoValueF {#2}
      {
        \tl_if_blank:nF {#2}
          {
            \SI {#2} { \minute }
            \IfNoValueF {#3} { ~ }
          }
      }
    \IfNoValueF {#3}
      { \tl_if_blank:nF {#3} { \SI {#3} { \second } } }
  }
\ExplSyntaxOff
\begin{document}
\hms{10}\\
\hms{;10}\\
\hms{;;10}\\
\hms{10;10}\\
\hms{10;;10}\\
\hms{10;10;10}\\
\end{document}

should do what you want. I've included an optional first argument for local set up in the usual siunitx way.