Time Arithmetic
September 15, 2015
We’ll write new functions julian-time
and gregorian-time
that call julian
and gregorian
for the date part of the calculation; it’s always easier to let somebody else do the hard work.
We represent a date-time stamp as an integer date with an added fraction for the time, which is the number of seconds since midnight divided by 24 * 60 * 60 = 86400, the number of seconds in a full day. The number of seconds since midnight is 60 * 60 = 3600 times the number of hours since midnight, plus 60 times the number of minutes since the hour, plus the number of seconds since the minute:
(define (julian-time year month day hour minute second)
(+ (julian year month day)
(/ (+ (* hour 60 60) (* minute 60) second) 86400)))
Extracting hours, minutes and seconds from the time-stamp is just the reverse calculation:
(define (gregorian-time time-stamp)
(let* ((date (floor time-stamp))
(time (* (- time-stamp date) 86400))
(hour (quotient time 3600))
(minute (modulo (quotient time 60) 60))
(second (modulo time 60)))
(call-with-values
(lambda () (gregorian date))
(lambda (year month day)
(values year month day hour minute second)))))
Here are some examples:
> (julian-time 2015 9 14 10 39 42)
35384838397/14400
> (gregorian-time (julian-time 2015 9 14 10 39 42))
2015
9
14
10
39
42
We used the julian
and gregorian
functions from the Standard Prelude. You can run the program at http://ideone.com/Woug7l.