A \def syntax problem
\numexpr
expressions have to expand to the numeric syntax so you can not have unexpandable assignments from \def
I think this is the calculation you intended
\documentclass{article}
\def\nok#1:#2\relax#3:#4\relax{%
\numexpr
\numexpr(#3)*60+#4\relax
\ifnum\numexpr(#1)*60+#2\relax<\numexpr(#1)*60+#2\relax+1440\fi
\relax
}
\begin{document}
\the\numexpr(\nok22:44\relax22:50\relax)\relax
\end{document}
The expected expl3
answer providing the expected padding. This also support (single) macros in the input.
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewExpandableDocumentCommand{\timedelta}{mm}
{
\joseph_timediff:ff { #1 } { #2 }
}
\cs_new:Nn \joseph_timediff:nn
{
\__joseph_timediff:ff
{ \__joseph_minutes:w #1 \q_stop }
{ \__joseph_minutes:w #2 \q_stop }
}
\cs_generate_variant:Nn \joseph_timediff:nn { ff }
\cs_new:Nn \__joseph_timediff:nn
{
\joseph_output:f
{
\int_eval:n { (#2) - (#1) \int_compare:nNnT { #2 } < { #1 } { + 1440 } }
}
}
\cs_generate_variant:Nn \__joseph_timediff:nn { ff }
\use:x
{
\cs_new:Npn \exp_not:N \__joseph_minutes:w ##1 \token_to_str:N : ##2 \exp_not:N \q_stop
}
{
#1 * 60 + #2
}
\cs_new:Nn \joseph_output:n
{
\int_compare:nNnT { \int_div_truncate:nn { #1 } { 60 } } < { 10 } { 0 }
\int_div_truncate:nn { #1 } { 60 }
:
\int_compare:nNnT { \int_mod:nn { #1 } { 60 } } < { 10 } { 0 }
\int_mod:nn { #1 } { 60 }
}
\cs_generate_variant:Nn \joseph_output:n { f }
\ExplSyntaxOff
\newcommand\routeStart{01:27}
\newcommand\routeStop{01:27}
\begin{document}
\timedelta{22:40}{22:50}
\timedelta{23:00}{00:10}
\timedelta{08:24}{19:32}
\timedelta{\routeStart}{01:27}
\timedelta{16:03}{\routeStop}
\timedelta{\routeStart}{\routeStop}
\end{document}
You can also use Lua, if you're willing to compile with lualatex
instead of pdflatex
:
\documentclass{article}
\directlua{dofile('timediff.lua')}
\def\wait#1#2{\directlua{tex.print(timediff('#1', '#2'))}}
\begin{document}
The time from 22:44 to 22:50 is \wait{22:44}{22:50}.
The time from 23:00 to 00:10 is \wait{23:00}{00:10}.
\end{document}
where timediff.lua
is:
function hhms(s)
-- Example: hhms('01:42') gives 102
local _, _, hh, mm = string.find(s, "(%d*):(%d*)")
return hh * 60 + mm
end
function timediff(time1, time2)
-- Example: timediff('22:45', '01:15') gives '02:30'
local t1 = hhms(time1)
local t2 = hhms(time2)
if t2 < t1 then t2 = t2 + 1440 end
local d = t2 - t1
local mm = d % 60
local hh = (d - mm) / 60
return string.format('%02d:%02d', hh, mm)
end
Later you may find Lua's time and date functions useful.