Runaway argument? with \newcommand[2]
\expandafter
only expands one token after the token that follows (unless there are arguments). In your case this is just #1
. One way to get your result, is to collect #1 #2
expanded in a macro before insertion:
\documentclass{article}
\makeatletter
% Timestamp
\newcommand{\timeStamp}[2]{%{#1-Date (YYYY.MM.DD)}{#2-Time (HH:MM)}
\edef\mytmp{#1 #2}\expandafter\timeStamp@t\mytmp\@nil%
}%
\def\timeStamp@t#1.#2.#3 #4:#5\@nil{%
\the\numexpr#5+#4*60+(#3-1)*60*24+(#2-1)*60*24*31+(#1-2017)*60*24*31*365\relax%
}%
\makeatother
\begin{document}
\tracingmacros=2\tracingcommands=2
\def\tOne{03:00}%
\def\dOne{2017.08.01}%
\timeStamp{\dOne}{\tOne}
\end{document}
This approach should generalise well to more than two arguments.
One can do some juggling with \expandafter
, but a direct approach may be better:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\cs_new:Nn \joseph_time_stamp:nn
{
\__joseph_time_stamp:w #1.#2\q_stop
}
\cs_generate_variant:Nn \joseph_time_stamp:nn { ff }
% a devious trick for the colon
\use:x
{
\cs_new:Npn
\exp_not:N \__joseph_time_stamp:w
##1.##2.##3.##4\token_to_str:N :##5
\exp_not:N \q_stop
}
{
\int_eval:n
{
#5+#4*60+(#3-1)*60*24+(#2-1)*60*24*31+(#1-2017)*60*24*31*365
}
}
\NewExpandableDocumentCommand{\timeStamp}{mm}
{
\joseph_time_stamp:ff { #1 } { #2 }
}
\ExplSyntaxOff
\begin{document}
\def\tOne{03:00}
\def\dOne{2017.08.01}
\timeStamp{\dOne}{\tOne}
\timeStamp{2017.1.1}{0:0} % should print 0
\end{document}
If the time specification didn't include the colon, it would be easier; the problem is that :
is special in the scope of \ExplSyntaxOn
, so the direct
\cs_new:Npn \__joseph_time_stamp:w #1.#2.#3.#4:#5\q_stop
{
\int_eval:n
{
#5+#4*60+(#3-1)*60*24+(#2-1)*60*24*31+(#1-2017)*60*24*31*365
}
}
would not work and an indirect method is needed in order to “stringify” the colon.
An expandable solution with \expandafter
and argument juggling (OS
for “old style”):
\makeatletter
\newcommand{\timeStampOS}[2]{%
\expandafter\timeStampOS@a\expandafter{#2}{#1}%
}
\newcommand{\timeStampOS@a}[2]{%
\expandafter\timeStampOS@b\expandafter{#2}{#1}%
}
\newcommand{\timeStampOS@b}[2]{\timeStampOS@c #1 #2\@nil}
\def\timeStampOS@c #1.#2.#3 #4:#5\@nil{%
\the\numexpr
#5+#4*60+(#3-1)*60*24+(#2-1)*60*24*31+(#1-2017)*60*24*31*365
\relax
}
\makeatother
expand #1 and #2
% Timestamp
\newcommand\timeStamp[2]{%
\expandafter\timeStamp@t#2 #1\@nil}%
\def\timeStamp@t#1:#2 #3\@nil{\expandafter\timeStamp@@t#3 #1:#2\@nil}%
\def\timeStamp@@t#1.#2.#3 #4:#5\@nil{%
\the\numexpr#5+#4*60+(#3-1)*60*24+(#2-1)*60*24*31+(#1-2017)*60*24*31*365\relax%
}%