From: stardiviner <numbchild@gmail.com>
To: Emanuel Berg <moasenwood@zoho.eu>
Cc: help-gnu-emacs@gnu.org
Subject: Re: How to subtract timestamp in elisp?
Date: Mon, 06 Jul 2020 09:43:51 +0800 [thread overview]
Message-ID: <878sfx1kq0.fsf@gmail.com> (raw)
In-Reply-To: <87fta6m6mb.fsf@ebih.ebihd>
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
prev parent reply other threads:[~2020-07-06 1:43 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
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 message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=878sfx1kq0.fsf@gmail.com \
--to=numbchild@gmail.com \
--cc=help-gnu-emacs@gnu.org \
--cc=moasenwood@zoho.eu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).