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 15:34:07 +0200 Organization: www.karl-voit.at Message-ID: <2016-07-12T15-17-56@devnull.Karl-Voit.at> References: <2016-07-12T12-41-55@devnull.Karl-Voit.at> Reply-To: Karl Voit NNTP-Posting-Host: plane.gmane.org X-Trace: ger.gmane.org 1468330521 8555 80.91.229.3 (12 Jul 2016 13:35:21 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 12 Jul 2016 13:35:21 +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 15:35:04 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 1bMxq1-0004ET-FL for geh-help-gnu-emacs@m.gmane.org; Tue, 12 Jul 2016 15:35:01 +0200 Original-Received: from localhost ([::1]:40536 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bMxq0-0008Uo-Nz for geh-help-gnu-emacs@m.gmane.org; Tue, 12 Jul 2016 09:35:00 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:55714) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bMxpS-0008Rn-Op for help-gnu-emacs@gnu.org; Tue, 12 Jul 2016 09:34:32 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bMxpM-0005ML-Qv for help-gnu-emacs@gnu.org; Tue, 12 Jul 2016 09:34:26 -0400 Original-Received: from plane.gmane.org ([80.91.229.3]:53671) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bMxpM-0005Lq-KG for help-gnu-emacs@gnu.org; Tue, 12 Jul 2016 09:34:20 -0400 Original-Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1bMxpK-0003rN-S7 for help-gnu-emacs@gnu.org; Tue, 12 Jul 2016 15:34:18 +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 15:34:18 +0200 Original-Received: from news1142 by friends.grml.info with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 12 Jul 2016 15:34:18 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 80 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:110792 Archived-At: * 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. I chose the path I understood and which tends to be the easiest one: manually converting everything to seconds, do the calculations, and re-convert the results to HH:MM format. Yes, I did not spend effort in error-proning, edge-cases and other cases than same-day scenarios. (defun my-extract-minutes-of-hm-string(hm-string) "returns the minutes of a string like 9:42 -> 42 (and 0 if there are no minutes)" (let ( ;; minutes is the second element after splitting with ":" (minutes (nth 1 (split-string hm-string ":"))) ) ;; if there is no second element, return "0" (instead of nil) (if (eq minutes 'nil) 0 (string-to-number minutes) ) ) ) (defun my-extract-hours-of-hm-string(hm-string) "returns the hours of a string like 9:42 -> 9" (string-to-number (car (split-string hm-string ":") ) ) ) (defun my-hm-string-to-minutes(hm-string) "returns the minutes of a string like 2:42 -> 162" (let ( ;; minutes is the second element after splitting with ":" (minutes (my-extract-minutes-of-hm-string hm-string)) (hours (my-extract-hours-of-hm-string hm-string)) ) (+ minutes (* hours 60)) ) ) (defun my-calculate-office-hour-total(officestart officeend lunchstart lunchend) "calculates the total hours:minutes of a work-day depending on time of arrival/leave and lunch break in HH:MM" (let ( (officestartminutes (my-hm-string-to-minutes officestart));; integer of minutes (officeendminutes (my-hm-string-to-minutes officeend));; integer of minutes (lunchstartminutes (my-hm-string-to-minutes lunchstart));; integer of minutes (lunchendminutes (my-hm-string-to-minutes lunchend));; integer of minutes ) (let* ( (officeminutes (- (- officeendminutes officestartminutes) (- lunchendminutes lunchstartminutes))) (officeminutesstring (format-time-string "%H:%M" (seconds-to-time (* 60 officeminutes)) t)) ) ;;(message (concat "Minutes epoch: " (number-to-string officeminutes))) ;;(message (concat "Minutes string: " officeminutesstring)) (symbol-value 'officeminutesstring) ) ) ) ;; (my-calculate-office-hour-total "09:57" "17:22" "11:35" "12:08") -> Minutes epoch: 412 | Minutes string: 06:52 Thanks for all the other approaches and snippets - I copied them to my personal knowledge-collection to learn from them! -- 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