From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Newsgroups: gmane.emacs.help Subject: Re: How to get time difference with Elisp? Date: Tue, 12 Jul 2016 13:14:09 +0200 Message-ID: <20160712111409.GA25909@tuxteam.de> References: <2016-07-12T12-41-55@devnull.Karl-Voit.at> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; x-action=pgp-signed Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1468322102 607 80.91.229.3 (12 Jul 2016 11:15:02 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 12 Jul 2016 11:15:02 +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:14:56 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 1bMveQ-0006zQ-Cv for geh-help-gnu-emacs@m.gmane.org; Tue, 12 Jul 2016 13:14:54 +0200 Original-Received: from localhost ([::1]:39013 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bMveK-0004L6-D8 for geh-help-gnu-emacs@m.gmane.org; Tue, 12 Jul 2016 07:14:48 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:47468) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bMvdv-0004Ky-Ux for help-gnu-emacs@gnu.org; Tue, 12 Jul 2016 07:14:24 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bMvdq-0001jI-UJ for help-gnu-emacs@gnu.org; Tue, 12 Jul 2016 07:14:23 -0400 Original-Received: from mail.tuxteam.de ([5.199.139.25]:59937 helo=tomasium.tuxteam.de) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bMvdq-0001iF-Oh for help-gnu-emacs@gnu.org; Tue, 12 Jul 2016 07:14:18 -0400 Original-Received: from tomas by tomasium.tuxteam.de with local (Exim 4.80) (envelope-from ) id 1bMvdh-0006qk-3k for help-gnu-emacs@gnu.org; Tue, 12 Jul 2016 13:14:09 +0200 In-Reply-To: <2016-07-12T12-41-55@devnull.Karl-Voit.at> User-Agent: Mutt/1.5.21 (2010-09-15) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 5.199.139.25 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:110788 Archived-At: -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Tue, Jul 12, 2016 at 12:46:37PM +0200, Karl Voit wrote: > Hi! > > 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. 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. 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. Note that I used mapcar* which I think is part of cl. regards - -- tomás -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.12 (GNU/Linux) iEYEARECAAYFAleE0QEACgkQBcgs9XrR2ka3eQCfYdyZO6g6E0UKND1RgcOkPItV qPoAn0iFvEQ5ioJphRIA0BbNYHIXgKVA =71DO -----END PGP SIGNATURE-----