help-gnu-emacs
[Top][All Lists]
Advanced

[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 13:48:39 +0200
User-agent: slrn/pre1.0.0-18 (Linux)

Hi Thomas,

* <tomas@tuxteam.de> <tomas@tuxteam.de> 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




reply via email to

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