Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

format time difference with %H

Tags:

emacs

elisp

How do I format a time-difference value, computed using time-subtract? Consider this simple example:

(let*
    ((before (current-time))
     (after (progn (sit-for 0.25) (current-time)))
     (diff (time-subtract after before)))
  (insert (format "before: %s--%s\n" before (format-time-string "%Hh%Mm%S.%3Ns" before)))
  (insert (format "after:  %s--%s\n" after (format-time-string "%Hh%Mm%S.%3Ns" after)))
  (insert (format "diff:   %s--%s--%s\n" diff (format-time-string "%s.%3Ns" diff)
                  (format-time-string "%Hh%Mm%S.%3Ns" diff))))

Evaluating it inserts the following:

before: (22616 21856 296901 800000)--13h47m12.296s
after:  (22616 21856 552345 800000)--13h47m12.552s
diff:   (0 0 255444 0)--0.255s--16h00m00.255s

The difference between the two timestamps is about 250ms. But formatting it using format-time-string with format string "%Hh%Mm%S.%3Ns" produces 16.

Shouldn't it be 0?

like image 664
Miserable Variable Avatar asked May 28 '26 18:05

Miserable Variable


1 Answers

"16" comes from your timezone.

I think if you do (format-time-string "%Hh%Mm%S.%3Ns" diff 0), you will see the result you expected (i.e., 0).

like image 155
sds Avatar answered May 30 '26 15:05

sds



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!