Apparent divide-by-zero bug in date->julian-day John Clements (30 Jun 2004 06:25 UTC)
Re: Apparent divide-by-zero bug in date->julian-day Will Fitzgerald (01 Jul 2004 19:55 UTC)

Apparent divide-by-zero bug in date->julian-day John Clements 30 Jun 2004 06:25 UTC

date->julian-day can result in a divide-by-zero error:

(require (lib "time.ss" "srfi" "19"))
(date->julian-day (make-srfi:date 0 0 0 0 29 6 2004 0))

=>

/: Division by 0

The problem appears to be in date->julian-day:

(define (date->julian-day date)
   (let ( (nanosecond (date-nanosecond date))
	 (second (date-second date))
	 (minute (date-minute date))
	 (hour (date-hour date))
	 (day (date-day date))
	 (month (date-month date))
	 (year (date-year date))
	 (offset (date-zone-offset date)) )
     (+ (tm:encode-julian-day-number day month year)
        (- 1/2)
        (+ (/ (/ (+ (* hour 60 60)
		   (* minute 60) second (/ nanosecond tm:nano)) tm:sid)
	     (- offset))))))

Note that there's a division by (- offset), which means that any call
to date->julian-day with a zero time-zone offset causes an error.  It
seems clear that the (- offset) should appear inside the
summation-of-seconds addition.

It also means that the result of date->julian day is _always_ going to
be wrong, which suggests that there's no test case for this function at
all.  Sure enough, a grep through the test suite for 'date->julian-day'
comes up empty.  Was this function added late?  Any thought of updating
the test suite?

john clements