#'time-subtract
's docstring says "See ‘current-time-string’ for the various forms of a time value." #'current-time-string
specifies times to be in this format:
If SPECIFIED-TIME is given, it is a time to format instead of the
current time. The argument should have the form (HIGH LOW . IGNORED).
However, #'parse-time-string
says this:
Parse the time-string STRING into (SEC MIN HOUR DAY MON YEAR DOW DST TZ).
So one shouldn't pass this value into #'time-subtract
. But there is #'date-to-time
:
ELISP> (date-to-time "2018-10-20 08:50:36.343")
(23499 9372)
And we can pass these values to #'time-subtract
:
ELISP> (time-subtract (date-to-time "2018-10-20 08:50:36.343")
(date-to-time "2018-10-20 08:47:43.029"))
(0 173)
This makes sense; 8:50:36 is exactly 173 seconds after 8:47:43! Cool! Note that this throws away the milliseconds in the timestamps. This is a limitation of how Emacs deals with time.
Now let's get it into the output format you want: 00:02:53.314
. We can use #'format-time-string
to do so:
ELISP> (format-time-string "%H:%M:%S"
(time-subtract (date-to-time "2018-10-20 08:50:36.343")
(date-to-time "2018-10-20 08:47:43.029"))
t)
"00:02:53"
Note that you have to give the third argument ZONE
to #'format-time-string
, or it will format it in your local time zone. Passing t
there formats it in UTC. This is definitely confusing.
parse-time-string
will decode integer seconds, so you'll need to parse fractional seconds separately.format-time-string
.