1

With datetime2, is there a way to calculate an end time from a start time if I have the duration in minutes? Something like

\newcommand*\timespan{90}
\newcommand*\starttime{13:45:00}
\DTMtime{\starttime+\timespan}

(analoguous to \DTMdate where you can add days)

I don't see anything in the docs, but I have the impression it might be possible via \DTMfetchhour and \DTMfetchminute.

(The background here is changing start time with a fixed duration, and I want to automatically output the whole slot including the end time)

  • Calculating clock times is trickier due to the need of using modulo computation -- adding days is not that difficult. Apparently neither datetime2 nor datetime2-calc provide such features – user31729 Feb 5 at 21:35
3

This is a preliminary version, using expl3 features and calculating the total number of seconds, then splitting and calculating the new time.

It does not indicate that the time span is more than 24 hours, so using a timespan of multiples of 1440 (minutes) will result in the same time as the start time.

Wrapping hour values after midnight is done.

datetime2 does not provide macros in order to calculate time spans or end times in sense of hour:minute:seconds -- there are some computation macros concerning dates.

enter image description here

\documentclass{article}

\usepackage{xparse}
\usepackage{datetime2}

\newcommand*\timespan{90}
\newcommand*\starttime{13:45:00}

\ExplSyntaxOn
\cs_generate_variant:Nn \int_set:Nn {NV}
\cs_generate_variant:Nn \seq_set_split:Nnn {NVn,NVV}

\NewDocumentCommand{\endtime}{+m+m}{%
  \group_begin:
  \tl_set:Nx \l_tmpa_tl {#1}
  \seq_set_split:NVV \l_tmpa_seq {\c_colon_str} \l_tmpa_tl
  \exp_args:NNx \fp_set:Nn \l_tmpa_fp {\seq_item:Nn \l_tmpa_seq {1} * 3600 + \seq_item:Nn \l_tmpa_seq {2} * 60 + \seq_item:Nn \l_tmpa_seq {3}  }
  \exp_args:NNx \fp_set:Nn \l_tmpb_fp {#2 * 60}
  \fp_add:Nn \l_tmpa_fp {#2 * 60} 
  \tl_clear:N \l_tmpa_tl 
  % Compute hours
  \exp_args:NNx \int_set:Nn \l_tmpa_int {\int_div_truncate:nn {\fp_use:N \l_tmpa_fp}{3600}}
  \int_compare:nNnTF {\l_tmpa_int } > {23} {% After midnight?
    \exp_args:NNx \int_set:Nn \l_tmpb_int {\int_mod:nn {\l_tmpa_int } {24}}
  }{%
    \int_set_eq:NN \l_tmpb_int \l_tmpa_int
  }
  \tl_put_right:NV \l_tmpa_tl {\l_tmpb_int\c_colon_str}

  % Remaining seconds, split them into minutes and seconds 
  \int_set:Nn \l_tmpa_int {\fp_use:N \l_tmpa_fp}

  \exp_args:NNx \int_set:Nn \l_tmpb_int {\int_mod:nn {\l_tmpa_int}{3600}}
  \exp_args:NNx \int_set:Nn \l_tmpb_int {\int_div_truncate:nn {\l_tmpb_int}{60}}
  \tl_put_right:NV \l_tmpa_tl {\l_tmpb_int\c_colon_str}
  % Finally splitting: Seconds
  \exp_args:NNx \int_set:Nn \l_tmpa_int {\int_mod:nn {\l_tmpa_int}{60}}
  \tl_put_right:NV \l_tmpa_tl {\l_tmpa_int}
  % Display output:
  \exp_args:NV \DTMtime{\l_tmpa_tl}
  \group_end:
}



\ExplSyntaxOff

\begin{document}
We started at \expandafter\DTMtime\expandafter{\starttime} 

The event lasts \timespan\ minutes

It ends at \endtime{\starttime}{\timespan}
\end{document}
  • 1
    Thank you so much! This works perfectly for my needs (the 24h limitation is not an issue). If you don't mind, I'll integrate this code into my jslectureplanner package (of course with appropriate credits) – JSpitzm Feb 6 at 9:14
  • @JSpitzm: Permission granted -- please come back occasionally, I will improve my 'hacky' solution from time to time. I will look forward to your package – user31729 Feb 6 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.