(if (or (< yr 1582)
(and (= yr 1582) (or (< m 10) (and (= m 10) (< d 5)))))
(setq b 0) ; Julian calendar
(setq a (fix (/ y 100)) ; Gregorian calendar
b (+ (- 2 a) (fix (/ a 4)))
)
)
(+ (fix (+ (* 365.25 (+ y 4716)) (fix (* 30.6001 (+ m 1)))))
d b -1524.0 (/ (+ (* (+ (* hh 60) mm) 60) ss) (* 24.0 60 60)))
)
;;; (DTOJ <calendar date>) -- convert calendar date/time to Julian
(defun dtoj (cdate / c f yr ys m d)
(setq ys (if (< cdate 0) -1 1) ; Sign on year
c (fix (abs cdate)) ; Date in unsigned digits
yr (* (/ c 10000) ys) ; Get year
m (rem (/ c 100) 100) ; Get month
d (rem c 100) ; Get day
f (rem (abs cdate) 1) ; Fraction of day
)
(ctoj yr m d (fix (+ (* f 100) 0.1))
(rem (fix (+ (* f 10000) 0.1)) 100)
(+ (rem (fix (+ (* f 1000000) 0.1)) 100)
(/ (rem (fix (+ (* f 1000000000) 0.1)) 1000) 1000.0)))
)
;;; (JTOC <Julian date>) -- convert Julian date/time to calendar date list
(defun jtoc (td / time a b c d e alpha z m hh mm)
(setq time (* 86400.0 (- td (setq z (fix td)))))
(if (< z 2299161)
(setq a z) ; Julian calendar
(setq alpha (fix (/ (- z 1867216.25) 36524.25)) ; Gregorian calendar
a (- (+ z 1 alpha) (fix (/ alpha 4)))
)
)
(setq b (+ a 1524)
c (fix (/ (- b 122.1) 365.25))
d (fix (* 365.25 c))
e (fix (/ (- b d) 30.6001))
)
(setq m (fix (if (< e 14) (1- e) (- e 13))))
; Determine the clock time from the fraction of a day
(setq hh (fix (/ time 3600.0))
time (- time (* hh 3600.0))
mm (fix (/ time 60.0))
)
; Return calendar date as list
(list (fix (- c (if (> m 2) 4716 4715))) m (fix (- b d (fix (* 30.6001 e))))
hh mm
(- time (* mm 60))
)
)