chicken-users
[Top][All Lists]
Advanced

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

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


From: Kon Lovett
Subject: Re: [Chicken-users] Date/time problem (my code I am sure)
Date: Thu, 21 Dec 2006 07:30:19 -0800

On Dec 20, 2006, at 7:01 PM, Jeremy Cowgar wrote:

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

Hi Jeremy,

What version of the srfi-19 egg are you using? With a slight modification to your example above & srfi-19 2.3:

... <same> ...

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

(define vec1 (vector 2006 12 20))
(define vec2 (vector 2006 12 19))
(print (distance-of-time-in-words vec1))
=> (#,(time time-utc 0 1166572800) #,(time time-utc 447000027 1166713173) #,(time time-duration 447000027 -140373) -140373)
(print (distance-of-time-in-words vec1 vec2))
=> (#,(time time-utc 0 1166572800) #,(time time-utc 0 1166486400) #, (time time-duration 0 86400) 86400)

-140373 is a sensible result.

Unfortunately there haven't been any changes to the date/time arithmetic, ever. If you are using 2.0 there have been bug fixes since but I don't think they are related to what you are seeing. I don't know what to tell you.

Sorry,
Kon




_______________________________________________
Chicken-users mailing list
address@hidden
http://lists.nongnu.org/mailman/listinfo/chicken-users





reply via email to

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