From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: stardiviner Newsgroups: gmane.emacs.help Subject: Re: How to subtract timestamp in elisp? Date: Mon, 06 Jul 2020 09:43:51 +0800 Message-ID: <878sfx1kq0.fsf@gmail.com> References: <87zh8fwn4u.fsf@gmail.com> <87fta6m6mb.fsf@ebih.ebihd> Reply-To: numbchild@gmail.com Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="26981"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.4; emacs 28.0.50 Cc: help-gnu-emacs@gnu.org To: Emanuel Berg Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Mon Jul 06 03:44:30 2020 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jsGBK-0006u6-TJ for geh-help-gnu-emacs@m.gmane-mx.org; Mon, 06 Jul 2020 03:44:30 +0200 Original-Received: from localhost ([::1]:48222 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jsGBJ-0001Ao-WD for geh-help-gnu-emacs@m.gmane-mx.org; Sun, 05 Jul 2020 21:44:30 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:40712) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jsGAu-0001AU-E2 for help-gnu-emacs@gnu.org; Sun, 05 Jul 2020 21:44:04 -0400 Original-Received: from [183.249.139.74] (port=10523 helo=localhost) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jsGAr-00021j-HQ for help-gnu-emacs@gnu.org; Sun, 05 Jul 2020 21:44:04 -0400 Original-Received: by localhost (Postfix, from userid 1000) id 68234241387; Mon, 6 Jul 2020 09:43:51 +0800 (CST) In-reply-to: <87fta6m6mb.fsf@ebih.ebihd> X-Host-Lookup-Failed: Reverse DNS lookup failed for 183.249.139.74 (deferred) Received-SPF: softfail client-ip=183.249.139.74; envelope-from=numbchild@gmail.com; helo=localhost X-detected-operating-system: by eggs.gnu.org: First seen = 2020/07/05 21:43:52 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: 63 X-Spam_score: 6.3 X-Spam_bar: ++++++ X-Spam_report: (6.3 / 5.0 requ) BAYES_00=-1.9, DKIM_ADSP_CUSTOM_MED=0.001, FORGED_GMAIL_RCVD=1, FREEMAIL_FROM=0.001, FSL_HELO_NON_FQDN_1=0.001, HELO_LOCALHOST=3.828, NML_ADSP_CUSTOM_MED=0.9, RDNS_NONE=0.793, SPF_SOFTFAIL=0.665, SPOOFED_FREEMAIL_NO_RDNS=1, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: reject X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:123511 Archived-At: Emanuel Berg via Users list for the GNU Emacs text editor 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