help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to subtract timestamp in elisp?


From: Dmitry Alexandrov
Subject: Re: How to subtract timestamp in elisp?
Date: Sun, 05 Jul 2020 00:11:25 +0300
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

stardiviner <numbchild@gmail.com> wrote:
> I hope a function can subtract two timestamps:
>
>     00:12:35 - 00:10:45 = 00:01:50
>
> Is there some hints or suggestion like function name or Emacs library or 
> package?

> Or Linux command is acceptable. I can write a function to execute shell 
> command then parse the result.

OMG!  Since when that started to require anything but arithmetics?

        (defun timestamp-interval (a b)
          (cl-flet* ((hms->s (h m s) (+ (* 3600 h)
                                        (* 60 m)
                                        s))
                     (s->hms (s) (let* ((h (/ s 3600))
                                        (s (% s 3600))
                                        (m (/ s 60))
                                        (s (% s 60)))
                                   (list h m s)))
                     (timestamp->s (string) (apply #'hms->s
                                                   (mapcar #'string-to-number
                                                           (split-string string 
":"))))
                     (s->timestamp (s) (apply #'format "%s%02d:%02d:%02d"
                                              (if (> 0 s) "-" "") (s->hms (abs 
s)))))
            (s->timestamp (- (timestamp->s a) (timestamp->s b)))))
        
        (timestamp-interval "00:12:35" "00:10:45")
        ;; => "00:01:50"

(Not tested.)


P. S. Sending mail ‘From: …@gmail.com’ right from your home machine is a best 
way to send it straight to junk folder. ;-)  Use smtp.gmail.com.

Attachment: signature.asc
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]