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?

Yasushi Shoji
  • 2,186
  • 16
  • 36

1 Answers1

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.

ebpa
  • 7,449
  • 29
  • 55
  • 1
    This is very close to what I want. Unfortunately, it prints 24:59:60 when I give 1.041666. – Yasushi Shoji Mar 23 '17 at 08: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 '17 at 17:47