chicken-users
[Top][All Lists]
Advanced

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

[Chicken-users] Date/time problem (my code I am sure)


From: Jeremy Cowgar
Subject: [Chicken-users] Date/time problem (my code I am sure)
Date: Wed, 20 Dec 2006 22:01:58 -0500
User-agent: Mutt/1.5.13 (2006-08-11)

I have written a few functions to help me in dealing with dates and times.
vector->date was created because postgresql egg returns a vector for the
date. The original (not stripped for including here) supports date, date and
time depending on vector length. to-time was created so I can send a few
different types to my functions, then finally, the source of the problem,
distance-of-time-in-words is a copy of (don't get mad) ruby on rails function
that displays things like "about a minute", "12 days", "13 hours", etc... for
difference. It too has been severly stripped for including here.

My problem is that when I use (currrent-date) the calculation from a date
object using time-difference is **way** off. I am not sure if I am not
creating a date object correctly via (make-date) or what. When I compare two
vectors #(2006 12 10) to say #(2006 12 9) which is converted to a date type
via make-date, that works fine, but comparing a #(2006 12 20) to
(current-date) everything goes wacky.

I have stripped the code to as little as possible to display the problem, can
anyone offer any advice?

----

(use srfi-19 srfi-19-io)

(define (vector->date vec)
  (make-date
           0 0 0 0
                    (vector-ref vec 2)
                                   (vector-ref vec 1)
                                            (vector-ref vec 0)
                                                           0))

(define (to-time obj)
  (cond
           ((time? obj)   obj)
                    ((date? obj)   (date->time-utc obj))
                                   ((vector? obj) (date->time-utc (vector->date 
obj)))))

(define (distance-of-time-in-words from #!optional (to (current-date)))
  (let* ((from-time (to-time from))
                 (to-time (to-time to))
                                                  (distance-in-seconds 
(time-second (time-difference
                                                                                
from-time to-time))))
            distance-in-seconds))

(define vec1 (vector 2006 12 20))
(define vec2 (vector 2006 12 19))
(print (distance-of-time-in-words vec1))       ;; 675162109
(print (distance-of-time-in-words vec1 vec2))  ;; 86400
(exit)

----

As you can see, the  675162109 number is wacky. That's 7,814 days. In order
for this sample to work, you need to chicken-setup srfi-19 to get the
date/time stuff.

Oh, again, realize that these functiosn in my code do much more than
presented here, i.e. the distance-of-time-in-words is useless basically in
this example, but the real function continues much beyond the current
example, just trimed for ease of finding the problem.

Thanks for any help!

Jeremy





reply via email to

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