Add repl.scm (can't believe I forgot it before!) and use it in irc.scm
[8sync.git] / eightsync / agenda.scm
index 8ffe6f18a489a94520f0a1058f0d9a8e3ca1da8a..2b46324bfe7a30f2c94af2281c28906374d585f6 100644 (file)
@@ -150,6 +150,17 @@ Generally done automatically for the user through (make-agenda)."
   (time time-segment-time)
   (queue time-segment-queue))
 
+;; @@: This seems to be the same as srfi-18's seconds->time procedure?
+;;   Maybe double check and switch to that?  (Thanks amz3!)
+
+(define (time-from-float-or-fraction time)
+  "Produce a (sec . usec) pair from TIME, a float or fraction"
+  (let* ((mixed-whole (floor time))
+         (mixed-rest (- time mixed-whole))  ; float or fraction component
+         (sec mixed-whole)
+         (usec (floor (* 1000000 mixed-rest))))
+    (cons (inexact->exact sec) (inexact->exact usec))))
+
 (define (time-segment-right-format time)
   "Ensure TIME is in the right format.
 
@@ -161,6 +172,8 @@ If an integer, will convert appropriately."
     (((? integer? s) . (? integer? u)) time)
     ;; time was just an integer (just the second)
     ((? integer? _) (cons time 0))
+    ((or (? rational? _) (? inexact? _))
+     (time-from-float-or-fraction time))
     (_ (throw 'invalid-time "Invalid time" time))))
 
 (define* (make-time-segment time #:optional (queue (make-q)))
@@ -199,12 +212,14 @@ run (time-segment-right-format) first."
   (sec time-delta-sec)
   (usec time-delta-usec))
 
-(define* (make-time-delta sec #:optional (usec 0))
+(define* (make-time-delta time)
   "Make a <time-delta> of SEC seconds and USEC microseconds.
 
 This is used primarily so the agenda can recognize RUN-REQUEST objects
-which are meant "
-  (make-time-delta-intern sec usec))
+which are meant to delay computation"
+  (match (time-segment-right-format time)
+    ((sec . usec)
+     (make-time-delta-intern sec usec))))
 
 (define tdelta make-time-delta)
 
@@ -233,13 +248,13 @@ Will produce (0 . 0) instead of a negative number, if needed."
   "Subtract TIME2 from TIME1"
   (time-carry-correct
    (cons (- (car time1) (car time2))
-         (- (cdr time2) (cdr time2)))))
+         (- (cdr time1) (cdr time2)))))
 
 (define (time-plus time1 time2)
   "Add TIME1 and TIME2"
   (time-carry-correct
    (cons (+ (car time1) (car time2))
-         (+ (cdr time2) (cdr time2)))))
+         (+ (cdr time1) (cdr time2)))))
 
 
 (define-record-type <schedule>