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

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

Re: `timer-relative-time' loses a day at -1.


From: Ehud Karni
Subject: Re: `timer-relative-time' loses a day at -1.
Date: Mon, 14 May 2001 23:08:49 +0300

On Mon, 14 May 2001 14:20:04 -0400 (EDT), address@hidden (Jonathan LF King) 
wrote:
> 
> Hello.  Executing the following snippet
> 
>     (defconst *SECS-IN-ONE-DAY* (* 24 60 60))
> 
>     (loop
>       for days-from-now from 2 downto -3 do
>       (print 
>        (format "days-from-now=%2d: %s"
>        days-from-now
>        (format-time-string
>         "%02d%03b"
>         (timer-relative-time (current-time)
>                              (* days-from-now *SECS-IN-ONE-DAY*) ) )
>        ) ) )
> 
> produces
> 
>     "days-from-now= 2: 16May"
> 
>     "days-from-now= 1: 15May"
> 
>     "days-from-now= 0: 14May"
> 
>     "days-from-now=-1: 14May"
> 
>     "days-from-now=-2: 13May"
> 
>     "days-from-now=-3: 12May"
>     nil

The problem is with the `timer-relative-time' function in timer.el.
It does not handle negative values (seconds and microseconds) properly.

Here is my definition of `timer-relative-time' that fixes this problem.

(defun timer-relative-time (time secs &optional usecs)
  "Advance TIME by SECS seconds and optionally USECS microseconds.
SECS may be a fraction."
  (let ((high (car time))
        (low (if (consp (cdr time)) (nth 1 time) (cdr time)))
        (micro (if (numberp (car-safe (cdr-safe (cdr time))))
                   (nth 2 time)
                 0)))
    ;; Add
    (if usecs (setq micro (+ micro usecs)))
    (if (floatp secs)
        (setq micro (+ micro (floor (* 1000000 (- secs (floor secs)))))))
    (setq low (+ low (floor secs)))

    ;; Normalize
    (setq low (+ low (/ micro 1000000)))
    ;; EK fix for negative numbers
    (if (< micro 0)
        (setq low (1- low)))
    (setq micro (mod micro 1000000))
    (setq high (+ high (/ low 65536)))
    ;; EK fix for negative numbers
    (if (< low 0)   
        (setq high (1- high)))
    (setq low (logand low 65535))
    (list high low (and (/= micro 0) micro))))


Ehud.


-- 
 Ehud Karni     Mivtach - Simon  Insurance   /"\
 Tel: +972-3-6212-757 Fax: +972-3-6292-544   \ /  ASCII Ribbon Campaign
 (USA) Fax and  voice  mail: 1-815-5509341    X   Against  HTML  Mail
     Better     Safe     Than     Sorry      / \
     mailto:address@hidden    http://www.simonwiesel.co.il



reply via email to

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