Skip to content

Commit

Permalink
Merge pull request #246 from samuel-jimenez/split-default-datetime-re…
Browse files Browse the repository at this point in the history
…aders

Split default datetime readers
  • Loading branch information
sabracrolleton authored Aug 18, 2020
2 parents b3576a9 + c27d797 commit cd01316
Showing 1 changed file with 46 additions and 20 deletions.
66 changes: 46 additions & 20 deletions cl-postgres/interpret.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,26 @@ Refers to a time of day, counting from midnight.
An interval is represented as several separate components. The reason that days
and microseconds are separated is that you might want to take leap seconds into
account.
Defaults are provided as follows:
#'default-date-reader
#'default-timestamp-reader
#'default-interval-reader
#'default-time-reader
e.g.
(defun make-temp-postgres-query-requiring-unix-timestamps ()
(flet ((temp-timestamp-reader (useconds-since-2000)
(- (+ +start-of-2000+ (floor useconds-since-2000 1000000))
(encode-universal-time 0 0 0 1 1 1970 0))))
(set-sql-datetime-readers
:date #'temp-timestamp-reader)
(let ((query (make-postgres-query-requiring-unix-timestamps))
(set-sql-datetime-readers
:date #'default-timestamp-reader)
query))))
"
(when date (set-date-reader date table))
(when timestamp (set-usec-reader oid:+timestamp+ timestamp table))
Expand All @@ -403,27 +423,33 @@ account.
(defconstant +start-of-2000+ (encode-universal-time 0 0 0 1 1 2000 0))
(defconstant +seconds-in-day+ (* 60 60 24))

(defun default-date-reader (days-since-2000)
(+ +start-of-2000+ (* days-since-2000 +seconds-in-day+)))

(defun default-timestamp-reader (useconds-since-2000)
(+ +start-of-2000+ (floor useconds-since-2000 1000000)))

(defun default-interval-reader (months days useconds)
(multiple-value-bind (sec us) (floor useconds 1000000)
`((:months ,months) (:days ,days) (:seconds ,sec)
(:useconds ,us))))

(defun default-time-reader (usecs)
(multiple-value-bind (seconds usecs)
(floor usecs 1000000)
(multiple-value-bind (minutes seconds)
(floor seconds 60)
(multiple-value-bind (hours minutes)
(floor minutes 60)
`((:hours ,hours) (:minutes ,minutes) (:seconds ,seconds)
(:microseconds ,usecs))))))

(set-sql-datetime-readers
:date (lambda (days-since-2000)
(+ +start-of-2000+ (* days-since-2000 +seconds-in-day+)))
:timestamp (lambda (useconds-since-2000)
(+ +start-of-2000+ (floor useconds-since-2000 1000000)))
:timestamp-with-timezone (lambda (useconds-since-2000)
(+ +start-of-2000+
(floor useconds-since-2000 1000000)))
:interval (lambda (months days useconds)
(multiple-value-bind (sec us) (floor useconds 1000000)
`((:months ,months) (:days ,days) (:seconds ,sec)
(:useconds ,us))))
:time (lambda (usecs)
(multiple-value-bind (seconds usecs)
(floor usecs 1000000)
(multiple-value-bind (minutes seconds)
(floor seconds 60)
(multiple-value-bind (hours minutes)
(floor minutes 60)
`((:hours ,hours) (:minutes ,minutes) (:seconds ,seconds)
(:microseconds ,usecs)))))))
:date #'default-date-reader
:timestamp #'default-timestamp-reader
:timestamp-with-timezone #'default-timestamp-reader
:interval #'default-interval-reader
:time #'default-time-reader)

;; Readers for a few of the array types

Expand Down

0 comments on commit cd01316

Please sign in to comment.