unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: <tomas@tuxteam.de>
To: help-gnu-emacs@gnu.org
Subject: Re: How to get time difference with Elisp?
Date: Tue, 12 Jul 2016 13:14:09 +0200	[thread overview]
Message-ID: <20160712111409.GA25909@tuxteam.de> (raw)
In-Reply-To: <2016-07-12T12-41-55@devnull.Karl-Voit.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-----



  reply	other threads:[~2016-07-12 11:14 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 [this message]
2016-07-12 11:48   ` Karl Voit
2016-07-12 12:25     ` tomas
2016-07-12 13:34 ` Karl Voit
     [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

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160712111409.GA25909@tuxteam.de \
    --to=tomas@tuxteam.de \
    --cc=help-gnu-emacs@gnu.org \
    /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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).