Skip to content

Commit 1fcb012

Browse files
committed
Parse timestamp strings in heuristicate-types
1 parent a5cef84 commit 1fcb012

2 files changed

Lines changed: 25 additions & 3 deletions

File tree

src/properties.lisp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,18 @@ Often when reading in a data set, the types will be inconsistent in a variable.
1010
(let ((name (name df)))
1111
(map nil #'(lambda (key)
1212
(let* ((data (column df key))
13-
(col-type (column-type data))
14-
(sym (find-symbol (symbol-name key) (find-package name))))
15-
(setf (get sym :type) col-type)))
13+
(col-type (column-type data))
14+
(sym (find-symbol (symbol-name key) (find-package name))))
15+
(setf (get sym :type) col-type)
16+
;; If a column is :temporal, replace strings in it with timestamps.
17+
;; If local-time fails to parse a value then leave it as is.
18+
(when (eql col-type :temporal)
19+
(map-into data
20+
#'(lambda (val)
21+
(or (when (stringp val)
22+
(local-time:parse-timestring val :fail-on-error nil))
23+
val))
24+
data))))
1625
(keys df))))
1726

1827
(defun set-properties (df property prop-values)

src/utils.lisp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@
99
;;; cl:type-of is under-constrained and returns implementation
1010
;;; specific results, so we use our own version
1111
;;; See: https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node53.html
12+
13+
14+
(defun timestamp-string-p (string)
15+
"Return T if STRING can be parsed as a timestamp by LOCAL-TIME."
16+
(and (stringp string)
17+
(when (local-time:parse-timestring string :fail-on-error nil)
18+
t)))
19+
20+
(deftype timestamp-string ()
21+
'(satisfies timestamp-string-p))
22+
1223
(defun get-type (x)
1324
"Return the most specific type symbol for x"
1425
(typecase x
@@ -22,6 +33,7 @@
2233
;; (rational 'cl:rational) ;SBCL doesn't recognise this return type
2334
;; (real 'real) ;SBCL doesn't recognise this return type
2435

36+
(timestamp-string 'temporal)
2537
(simple-string 'simple-string)
2638
(string 'string)
2739

@@ -54,6 +66,7 @@
5466
((member 'fixnum type-list) :fixnum)
5567
((member 'integer type-list) :integer)
5668
((member 'string type-list) :string)
69+
((member 'temporal type-list) :temporal)
5770
((member 'bit type-list) :bit)
5871
((member 'symbol type-list) :symbol)
5972
(t 'string ))))

0 commit comments

Comments
 (0)