unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* How to subtract timestamp in elisp?
@ 2020-07-04 11:11 stardiviner
  2020-07-04 11:39 ` Eli Zaretskii
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: stardiviner @ 2020-07-04 11:11 UTC (permalink / raw)
  To: Emacs Help


I'm find an Elisp solution to subtract timestamps like "00:12:35".

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 can convert timestamp into another format then convert back to timestamp.

I use those timestamps in "ffmpeg" command video cut clip command option "-ss" and "-t".

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

Here is my current code:

#+begin_src emacs-lisp
(defun ffmpeg-cut-clip (input-filename start-timestamp end-timestamp output-filename)
  (interactive (list
                (read-file-name "FFmpeg input filename: ")
                (read-string "FFmpeg start timestamp: ")
                (read-string "FFmpeg time timestamp: ")
                (read-file-name "FFmpeg output filename: ")))
  (let ((time-timestamp (FUNC start-timestamp end-timestamp)))
    (make-process
     :name "ffmpeg cut clip"
     ;; "ffmpeg -i input-filename -ss start-timestamp -t time-timestamp -codec copy output-filename"
     :command (list "ffmpeg"
                    "-i" input-filename
                    "-ss" start-timestamp
                    "-t" time-timestamp
                    "-codec copy"
                    output-filename)
     :buffer "*ffmpeg-cut-clip*"
     :sentinel (lambda (proc event)
                 (message "FFmpeg cut video clip finished.")))))
#+end_src

-- 
[ stardiviner ]
       I try to make every word tell the meaning that I want to express.

       Blog: https://stardiviner.github.io/
       IRC(freenode): stardiviner, Matrix: stardiviner
       GPG: F09F650D7D674819892591401B5DF1C95AE89AC3



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: How to subtract timestamp in elisp?
  2020-07-04 11:11 How to subtract timestamp in elisp? stardiviner
@ 2020-07-04 11:39 ` Eli Zaretskii
  2020-07-04 12:54 ` Michael Heerdegen
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 9+ messages in thread
From: Eli Zaretskii @ 2020-07-04 11:39 UTC (permalink / raw)
  To: help-gnu-emacs

> From: stardiviner <numbchild@gmail.com>
> Date: Sat, 04 Jul 2020 19:11:29 +0800
> 
> 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 can convert timestamp into another format then convert back to timestamp.

I think you should start with parse-time-string.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: How to subtract timestamp in elisp?
  2020-07-04 11:11 How to subtract timestamp in elisp? stardiviner
  2020-07-04 11:39 ` Eli Zaretskii
@ 2020-07-04 12:54 ` Michael Heerdegen
  2020-07-04 21:17   ` Dmitry Alexandrov
  2020-07-04 21:11 ` Dmitry Alexandrov
  2020-07-05 13:30 ` How to subtract timestamp in elisp? Emanuel Berg via Users list for the GNU Emacs text editor
  3 siblings, 1 reply; 9+ messages in thread
From: Michael Heerdegen @ 2020-07-04 12:54 UTC (permalink / raw)
  To: help-gnu-emacs

stardiviner <numbchild@gmail.com> writes:

> Is there some hints or suggestion like function name or Emacs library
> or package?
> Or can convert timestamp into another format then convert back to timestamp.

Several possibilities I think.  Calc can do it interactively, but I
think that's not what you want, you want to do this from Lisp.

I didn't find anything that wouldn't require some in-between
conversations.  Something to start with, as an alternative to Eli's
suggestion:

`appt-convert-time': Convert hour:min[am/pm] format to minutes from
midnight.

Obviously, multiply by 60 to get seconds.  You may also want to use

(defun my-last-midnight (&optional time)
  (cl-callf or time (current-time))
  (apply #'encode-time (apply #'list 0 0 0 (nthcdr 3 (decode-time time)))))

if your stamps may span midnight.

Use time-add, time-subtract, time-less-p etc for time arithmetic.
Accepted input values are time values like the return value of
(current-time) but seconds are also allowed where it makes sense.

`format-time-string' to convert back to a time stamp like your input is.

There are several places in Emacs that implement handling of time
strings somehow (Calc, org, calendar, diary, appt), but none offers what
you want out of the box completely AFAIK.


Michael.




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: How to subtract timestamp in elisp?
  2020-07-04 11:11 How to subtract timestamp in elisp? stardiviner
  2020-07-04 11:39 ` Eli Zaretskii
  2020-07-04 12:54 ` Michael Heerdegen
@ 2020-07-04 21:11 ` Dmitry Alexandrov
  2020-07-05  3:04   ` [SOLVED] " stardiviner
  2020-07-05 13:30 ` How to subtract timestamp in elisp? Emanuel Berg via Users list for the GNU Emacs text editor
  3 siblings, 1 reply; 9+ messages in thread
From: Dmitry Alexandrov @ 2020-07-04 21:11 UTC (permalink / raw)
  To: numbchild; +Cc: Emacs Help

[-- Attachment #1: Type: text/plain, Size: 1453 bytes --]

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 #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 247 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: How to subtract timestamp in elisp?
  2020-07-04 12:54 ` Michael Heerdegen
@ 2020-07-04 21:17   ` Dmitry Alexandrov
  0 siblings, 0 replies; 9+ messages in thread
From: Dmitry Alexandrov @ 2020-07-04 21:17 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 272 bytes --]

Michael Heerdegen <michael_heerdegen@web.de> wrote:
> `format-time-string' to convert back to a time stamp like your input is.

Nope.  This is for _dates_, while @numbchild@gmail.com is dealing with _lengths_ of media files.  It will fail on records longer than 24 hours.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 247 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [SOLVED] Re: How to subtract timestamp in elisp?
  2020-07-04 21:11 ` Dmitry Alexandrov
@ 2020-07-05  3:04   ` stardiviner
  2020-07-06  2:48     ` mu4e: configuring SMTP (was: [SOLVED] Re: How to subtract timestamp in elisp?) Dmitry Alexandrov
  0 siblings, 1 reply; 9+ messages in thread
From: stardiviner @ 2020-07-05  3:04 UTC (permalink / raw)
  To: Dmitry Alexandrov; +Cc: Emacs Help


Dmitry Alexandrov <dag@gnui.org> writes:

> 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.)
>

Dmitry:

This is really great. I thought to use `split-string` too to parse timestamp. My
thought was to use existing API if exist when I compose this email. But
implement a function to dealing with this is great too. Thanks for your help.
And of course Eli and Michael too.

> 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.

I used to realized my email always go into Gmail junk, can't find out the
reason. I guess you just solved my long time issue. I'm using Emacs mu4e
package. I will Google how to use SMTP server in mu4e. Thanks again.

-- 
[ stardiviner ]
       I try to make every word tell the meaning that I want to express.

       Blog: https://stardiviner.github.io/
       IRC(freenode): stardiviner, Matrix: stardiviner
       GPG: F09F650D7D674819892591401B5DF1C95AE89AC3



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: How to subtract timestamp in elisp?
  2020-07-04 11:11 How to subtract timestamp in elisp? stardiviner
                   ` (2 preceding siblings ...)
  2020-07-04 21:11 ` Dmitry Alexandrov
@ 2020-07-05 13:30 ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-07-06  1:43   ` stardiviner
  3 siblings, 1 reply; 9+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-07-05 13:30 UTC (permalink / raw)
  To: help-gnu-emacs

stardiviner wrote:

> I'm find an Elisp solution to subtract timestamps
> like "00:12:35".
>
> I hope a function can subtract two timestamps:
>
>     00:12:35 - 00:10:45 = 00:01:50

Here are there functions that might be what you look
for, or be interesting to check out at least:

;;; -*- lexical-binding: t -*-
;;;
;;; this file:
;;;   http://user.it.uu.se/~embe8573/emacs-init/time-cmp.el
;;;   https://dataswamp.org/~incal/emacs-init/time-cmp.el

(defun wall-clock-time (h1 m1 s1 h2 m2 s2)
  (let*((d   08) ; arbitrary day to use below, any would do
        (m   05) ; actually something cool happened that day
        (y 1978) ; in the history of climbing
        (total-seconds-1 (float-time (encode-time s1 m1 h1 d m y)))
        (total-seconds-2 (float-time (encode-time s2 m2 h2 d m y)))
        (s-diff (- total-seconds-2 total-seconds-1)) )
    (format-seconds "%.2h:%.2m:%.2s" s-diff) ))
(defalias 'wct #'wall-clock-time)
;; (wct 09 35 10 23 00 00) ; 13:24:50
;; (wct 09 35 10 09 35 20) ; 00:00:10

(defun time-between-times (year1 month1 day1
                           year2 month2 day2)
  (let*((seconds-then  (float-time (encode-time 0 0 0 day1 month1 year1)))
        (seconds-now   (float-time (encode-time 0 0 0 day2 month2 year2)))
        (seconds-diff  (- seconds-now seconds-then)) )
    (format-seconds "%yy %dd" seconds-diff)))
;;   (time-between-times 1958  4 13 1958 8 30) ; Tahiti Nui 2 -> 3,
;;                                             ; i.e. 0y 139d

(defun get-time-since (year month day)
  (interactive "nyear: \nnmonth: \nnday: ")
  (message "%s"
           (format-seconds
            "%yy %dd"
            (float-time
             (time-since (encode-time 0 0 0 day month year)) ))))
;; (get-time-since 2011 09 27) ; 8y 228d @ 2020-05-10

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: How to subtract timestamp in elisp?
  2020-07-05 13:30 ` How to subtract timestamp in elisp? Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-07-06  1:43   ` stardiviner
  0 siblings, 0 replies; 9+ messages in thread
From: stardiviner @ 2020-07-06  1:43 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: help-gnu-emacs


Emanuel Berg via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> writes:

> stardiviner wrote:
>
>> I'm find an Elisp solution to subtract timestamps
>> like "00:12:35".
>>
>> I hope a function can subtract two timestamps:
>>
>>     00:12:35 - 00:10:45 = 00:01:50
>
> Here are there functions that might be what you look
> for, or be interesting to check out at least:
>
> ;;; -*- lexical-binding: t -*-
> ;;;
> ;;; this file:
> ;;;   http://user.it.uu.se/~embe8573/emacs-init/time-cmp.el
> ;;;   https://dataswamp.org/~incal/emacs-init/time-cmp.el
>
> (defun wall-clock-time (h1 m1 s1 h2 m2 s2)
>   (let*((d   08) ; arbitrary day to use below, any would do
>         (m   05) ; actually something cool happened that day
>         (y 1978) ; in the history of climbing
>         (total-seconds-1 (float-time (encode-time s1 m1 h1 d m y)))
>         (total-seconds-2 (float-time (encode-time s2 m2 h2 d m y)))
>         (s-diff (- total-seconds-2 total-seconds-1)) )
>     (format-seconds "%.2h:%.2m:%.2s" s-diff) ))
> (defalias 'wct #'wall-clock-time)
> ;; (wct 09 35 10 23 00 00) ; 13:24:50
> ;; (wct 09 35 10 09 35 20) ; 00:00:10
>
> (defun time-between-times (year1 month1 day1
>                            year2 month2 day2)
>   (let*((seconds-then  (float-time (encode-time 0 0 0 day1 month1 year1)))
>         (seconds-now   (float-time (encode-time 0 0 0 day2 month2 year2)))
>         (seconds-diff  (- seconds-now seconds-then)) )
>     (format-seconds "%yy %dd" seconds-diff)))
> ;;   (time-between-times 1958  4 13 1958 8 30) ; Tahiti Nui 2 -> 3,
> ;;                                             ; i.e. 0y 139d
>
> (defun get-time-since (year month day)
>   (interactive "nyear: \nnmonth: \nnday: ")
>   (message "%s"
>            (format-seconds
>             "%yy %dd"
>             (float-time
>              (time-since (encode-time 0 0 0 day month year)) ))))
> ;; (get-time-since 2011 09 27) ; 8y 228d @ 2020-05-10

Thanks, learned more about time handling functions and code.

-- 
[ stardiviner ]
       I try to make every word tell the meaning that I want to express.

       Blog: https://stardiviner.github.io/
       IRC(freenode): stardiviner, Matrix: stardiviner
       GPG: F09F650D7D674819892591401B5DF1C95AE89AC3



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: mu4e: configuring SMTP (was: [SOLVED] Re: How to subtract timestamp in elisp?)
  2020-07-05  3:04   ` [SOLVED] " stardiviner
@ 2020-07-06  2:48     ` Dmitry Alexandrov
  0 siblings, 0 replies; 9+ messages in thread
From: Dmitry Alexandrov @ 2020-07-06  2:48 UTC (permalink / raw)
  To: numbchild; +Cc: Emacs Help

[-- Attachment #1: Type: text/plain, Size: 1259 bytes --]

stardiviner <numbchild@gmail.com> wrote:
> Thanks for your help.

You are welcome.

>> 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.
>
> I used to realized my email always go into Gmail junk, can't find out the reason. I guess you just solved my long time issue. I'm using Emacs mu4e package. I will Google how to use SMTP server in mu4e.

You have not done that so far, as I see [0].  Any issues?  I have never used mu4e, but it should not be really different from any other Emacs-based MUAs, such as Gnus:

	(setq send-mail-function 'smtpmail-send-it
	      message-send-mail-function send-mail-function
	      smtpmail-smtp-server "smtp.gmail.com"
	      smtpmail-stream-type 'ssl ; TLS really, was nil means STARTTLS or cleartext
	      smtpmail-smtp-service "smtps"     ; SMTP/TLS, was 25 == "smtp"
	      smtpmail-smtp-user "numbchild")

Passphrases are supposed to be stored in ~/.authinfo.gpg by default, but Emacs supports pass(1)-compatible storage too.  See (info "(auth) Help for users").

Or, if you need a sendmail(8)-compatible submission agent for some reason, take a look at msmtp(1).

[0] <878sfx1kq0.fsf@gmail.com>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 247 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2020-07-06  2:48 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-07-04 11:11 How to subtract timestamp in elisp? stardiviner
2020-07-04 11:39 ` Eli Zaretskii
2020-07-04 12:54 ` Michael Heerdegen
2020-07-04 21:17   ` Dmitry Alexandrov
2020-07-04 21:11 ` Dmitry Alexandrov
2020-07-05  3:04   ` [SOLVED] " stardiviner
2020-07-06  2:48     ` mu4e: configuring SMTP (was: [SOLVED] Re: How to subtract timestamp in elisp?) Dmitry Alexandrov
2020-07-05 13:30 ` How to subtract timestamp in elisp? Emanuel Berg via Users list for the GNU Emacs text editor
2020-07-06  1:43   ` stardiviner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).