* elisp Q: round & display time in 5-min. increments
@ 2007-11-28 0:01 Edward
2007-11-30 7:51 ` Ari Roponen
0 siblings, 1 reply; 2+ messages in thread
From: Edward @ 2007-11-28 0:01 UTC (permalink / raw)
To: help-gnu-emacs
Currently I'm using the following snippet to generate the time
rounded
to the nearest 5-min. increment:
(concat
(format-time-string "%H" (current-time))
":"
(int-to-string
(* (round (string-to-number (format-time-string
"%M" (current-
time))) 5) 5))
" ")
This works for most times, but it doesn't cover any edge conditions.
For example, between the 55th minute and the end of the hour, it
rounds to "60" and doesn't carry to the hour. Around the 5th minute,
it generates "5", but it doesn't pad with a zero.
Does anyone know of some pre-existing code in Emacs that does what
I'm
trying to do? Or failing that, does anyone have an elegant snippet
of code that accomplishes this task while meeting these edge-
conditions? I'm not worried about the midnight edge-condition, but
if you know of code that does that too, so much the better.
Thanks,
Edward
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: elisp Q: round & display time in 5-min. increments
2007-11-28 0:01 elisp Q: round & display time in 5-min. increments Edward
@ 2007-11-30 7:51 ` Ari Roponen
0 siblings, 0 replies; 2+ messages in thread
From: Ari Roponen @ 2007-11-30 7:51 UTC (permalink / raw)
To: help-gnu-emacs
Hi,
Edward <edward.dodge@gmail.com> writes:
> (concat
> (format-time-string "%H" (current-time))
> ":"
> (int-to-string
> (* (round (string-to-number (format-time-string
> "%M" (current-
> time))) 5) 5))
> " ")
>
> This works for most times, but it doesn't cover any edge conditions.
> For example, between the 55th minute and the end of the hour, it
> rounds to "60" and doesn't carry to the hour. Around the 5th minute,
> it generates "5", but it doesn't pad with a zero.
>
>
> Does anyone know of some pre-existing code in Emacs that does what
> I'm
> trying to do? Or failing that, does anyone have an elegant snippet
> of code that accomplishes this task while meeting these edge-
> conditions? I'm not worried about the midnight edge-condition, but
> if you know of code that does that too, so much the better.
I don't know about the existing code, but this seems to do what you
want:
(format-time-string
"%H:%M "
(apply 'encode-time
(let* ((time (decode-time
(time-add (current-time)
(seconds-to-time 150)))) ;2m30s
(mins (cadr time)))
(setcar (cdr time) (- mins (mod mins 5)))
time)))
--
Ari Roponen
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2007-11-30 7:51 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-11-28 0:01 elisp Q: round & display time in 5-min. increments Edward
2007-11-30 7:51 ` Ari Roponen
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).