[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to get time difference with Elisp?

From: Karl Voit
Subject: Re: How to get time difference with Elisp?
Date: Tue, 12 Jul 2016 15:34:07 +0200
User-agent: slrn/pre1.0.0-18 (Linux)

* Karl Voit <address@hidden> 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 
  (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)
      (string-to-number minutes)

(defun my-extract-hours-of-hm-string(hm-string)
  "returns the hours of a string like 9:42 -> 9"
    (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 
    (officeendminutes (my-hm-string-to-minutes officeend));; integer of minutes
    (lunchstartminutes (my-hm-string-to-minutes lunchstart));; integer of 
    (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.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]