From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Karl Voit Newsgroups: gmane.emacs.help Subject: Re: How to get time difference with Elisp? Date: Tue, 12 Jul 2016 13:48:39 +0200 Organization: www.karl-voit.at Message-ID: <2016-07-12T13-44-31@devnull.Karl-Voit.at> References: <2016-07-12T12-41-55@devnull.Karl-Voit.at> <20160712111409.GA25909@tuxteam.de> Reply-To: Karl Voit NNTP-Posting-Host: plane.gmane.org X-Trace: ger.gmane.org 1468324182 1116 80.91.229.3 (12 Jul 2016 11:49:42 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 12 Jul 2016 11:49:42 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Jul 12 13:49:33 2016 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1bMwBs-0007Zl-59 for geh-help-gnu-emacs@m.gmane.org; Tue, 12 Jul 2016 13:49:28 +0200 Original-Received: from localhost ([::1]:39507 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bMwBr-0003WR-Fw for geh-help-gnu-emacs@m.gmane.org; Tue, 12 Jul 2016 07:49:27 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:57995) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bMwBI-0003WD-1S for help-gnu-emacs@gnu.org; Tue, 12 Jul 2016 07:48:53 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bMwBE-0000gP-SG for help-gnu-emacs@gnu.org; Tue, 12 Jul 2016 07:48:52 -0400 Original-Received: from plane.gmane.org ([80.91.229.3]:46513) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bMwBE-0000gL-LF for help-gnu-emacs@gnu.org; Tue, 12 Jul 2016 07:48:48 -0400 Original-Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1bMwBC-0007Gx-Pt for help-gnu-emacs@gnu.org; Tue, 12 Jul 2016 13:48:46 +0200 Original-Received: from friends.grml.info ([136.243.234.19]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 12 Jul 2016 13:48:46 +0200 Original-Received: from news1142 by friends.grml.info with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 12 Jul 2016 13:48:46 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 73 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: friends.grml.info X-GPG-Key: http://www.Karl-Voit.at/Karl_Voit_GnuPG_public_key.gpg X-Registered-Linux-User: 224337 X-Confession: Pastafarian http://www.venganza.org/ User-Agent: slrn/pre1.0.0-18 (Linux) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 80.91.229.3 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.21 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.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.org gmane.emacs.help:110789 Archived-At: Hi Thomas, * wrote: > > On Tue, Jul 12, 2016 at 12:46:37PM +0200, Karl Voit wrote: >> >> I need to determine total office hours of a day without the time >> spent in lunch break. The source data is officebegin, officeend, >> lunchbreakbegin, lunchbreakend - all in string format "HH:MM" like >> "14:58". >> >> So far, I failed miserably to find the right combination of >> parse-time-string, encode-time, time-substract. >> >> Even determining the difference between only two times resulted in >> errors to me: >> >> (setq difference (time-subtract (encode-time (parse-time-string "12:24")) >> (encode-time (parse-time-string "11:45")))) >> >> ... results in: time-subtract: Wrong number of arguments: encode-time, 1 > > The immediate problem is that parse-time-string returns a list of values, > but encode-time expects separate arguments. Ah, I see. How unfortunate. > Apply takes care of that: > (apply #'encode-time (parse-time-string "12:24")) > etc. > > Now the problem is that for your time string day, month, year turn up > as nil, something encode-time doesn't like at all. Grrr. No wonder I could not come up with a solution myself. > I guess you'd have > to fill in missing stuff with the values from current-time. Here's > a rough sketch to start with: > > (let ((time-default (decode-time (current-time)))) > (setq difference > (time-subtract > (encode-time > (mapcar* (lambda (x y) (or x y)) > (apply #'encode-time (parse-time-string "12:24")) > time-default)) > (encode-time > (mapcar* (lambda (x y) (or x y)) > (apply #'encode-time (parse-time-string "11:45")) > time-default)) > > Of course, there might be functions around which make this much simpler. > If not, I think abstracting away some part as a function might enhance > readability a lot. I was also thinking of converting "13:49" into UNIX epoch seconds, calculate arithmetically, and re-convert the result to string. With your explanation of the issues with the original approach above, I tend to think that this seems to be the much simpler way to go. > Note that I used mapcar* which I think is part of cl. Is it just me or is this really just a matter of minutes to implement in *any* other language? -- All in all, one of the most disturbing things today is the definitive fact that the NSA, GCHQ, and many more government organizations are massively terrorizing the freedom of us and the next generations. http://Karl-Voit.at