[Top][All Lists]

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

Fix for `timer-relative-time' with negative increment

From: Ehud Karni
Subject: Fix for `timer-relative-time' with negative increment
Date: Tue, 15 May 2001 15:58:26 +0300

I'm sorry, The `timer-relative-time' definition I submitted last night
has a bug in border conditions (negative increment and the resulting
`micro' is BOTH negative and exact multiple of 1000000 or `low' is BOTH
negative and exact multiple of 65536). Here is a better (I hope)

(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)
    ;; 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 quotient (/ micro 1000000))
    (setq micro (mod micro 1000000))
    ;; Fix for possible negative micro   
    (and (< quotient 0)
         (!= micro 0)
         (setq quotient (1- quotient)))
    (setq low (+ low quotient))

    (setq quotient (/ low 65536))
    (setq low (logand low 65535))
    ;; Fix for possible negative low
    (and (< quotient 0)
         (!= low 0)
         (setq quotient (1- quotient)))
    (setq high (+ high quotient))

    (list high low (and (/= micro 0) micro))))


 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]