3

I'd like to calculate duration time between two dates in calc.

<2017-03-21 Mon 18:45> - <2017-03-20 Mon 18:45> = 24 hr

How can I do it?

I can insert a date to calc by typing ' <2017-03-20 18:45> at calc prompt. I can change the date format by typing dd10 to org-format.

But when I subtract <2017-03-20 Mon 18:45> from <2017-03-21 Mon 18:45>, I got 1.000035.

How can i get duration in hours and minutes?

4
  • 2
    The result is in days. Just multiply by 24 to get hours.
    – Juancho
    Mar 20, 2017 at 15:43
  • 1
    is there easier function to convert numbers to HH:MM:SS format? M-9 t P is closest I found. Mar 21, 2017 at 0:10
  • 1
    Depending on your task, org-duration.el might be helpful here.
    – rasmus
    Mar 22, 2017 at 9:21
  • org-duration.el will do. But I wanted to do it with the calc proper. (to report a bug in org ;-)) Mar 22, 2017 at 9:24

1 Answer 1

1

It looks like the part that's missing is printing the value on the top of the stack in the desired format. Try this:

(defun my/days-to-time-string (days)
  "Format number of DAYS as a string (HH:MM:SS)."
  (let* ((hours (* days 24))
         (minutes (* (mod hours 1) 60))
         (seconds (* (mod minutes 1) 60)))
    (format "%02d:%02d:%02d" (floor hours) (floor minutes) (floor seconds))))

(defmath days-to-time-string ()
  "Print the value on calc stack as days converted to hours."
  (interactive)
  (let ((days (string-to-number (calc-eval 1 'top))))
    (message (my/days-to-time-string days))))

Unless you create a binding you would use it with M-x calc-days-to-time-string.

2
  • 1
    This is very close to what I want. Unfortunately, it prints 24:59:60 when I give 1.041666. Mar 23, 2017 at 8:29
  • @YasushiShoji Whoops; good catch! Tricky edge cases :-) Fixed ;-) I'm sure you can work something a little more complex out if you care about not rounding seconds down.
    – ebpa
    Mar 25, 2017 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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