From: Karl Voit <devnull@Karl-Voit.at>
To: help-gnu-emacs@gnu.org
Subject: Re: How to get time difference with Elisp?
Date: Tue, 12 Jul 2016 15:34:07 +0200 [thread overview]
Message-ID: <2016-07-12T15-17-56@devnull.Karl-Voit.at> (raw)
In-Reply-To: 2016-07-12T12-41-55@devnull.Karl-Voit.at
* Karl Voit <devnull@Karl-Voit.at> 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
next prev parent reply other threads:[~2016-07-12 13:34 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-07-12 10:46 How to get time difference with Elisp? Karl Voit
2016-07-12 11:14 ` tomas
2016-07-12 11:48 ` Karl Voit
2016-07-12 12:25 ` tomas
2016-07-12 13:34 ` Karl Voit [this message]
[not found] <mailman.1166.1468320413.26859.help-gnu-emacs@gnu.org>
2016-07-12 11:48 ` Emanuel Berg
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=2016-07-12T15-17-56@devnull.Karl-Voit.at \
--to=devnull@karl-voit.at \
--cc=help-gnu-emacs@gnu.org \
--cc=news1142@Karl-Voit.at \
/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.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.