From 04b27792393bb71d1852deee0a57f93a8d6eea60 Mon Sep 17 00:00:00 2001 From: Dimitri Fontaine Date: Mon, 7 Sep 2015 17:05:10 +0200 Subject: [PATCH] Allow date format parsing to support time. A useful use case for date parsing at tine input level is to parse time (hour, minutes, seconds) rather than a full date (timestamp). Improve the code so that it's possible to use the date format facility even when the data field lacks the year/month/day information. Fix #288. --- src/parsers/date-format.lisp | 21 +++++++++++++++------ test/csv-parse-date.load | 18 ++++++++++++------ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/parsers/date-format.lisp b/src/parsers/date-format.lisp index 8a31a68..35014ba 100644 --- a/src/parsers/date-format.lisp +++ b/src/parsers/date-format.lisp @@ -42,12 +42,19 @@ (string= day "00")) nil (with-output-to-string (s) - (format s "~a-~a-~a ~a:~a:~a" - (let ((yint (parse-integer year))) - ;; process 2-digits year formats specially - (if (<= (length year) 2) (+ *century* yint) yint)) - month - day + ;; when given a full date format, format the date part + (when (and (assoc :year format) + (assoc :month format) + (assoc :day format)) + (format s "~a-~a-~a " + (let ((yint (parse-integer year))) + ;; process 2-digits year formats specially + (if (<= (length year) 2) (+ *century* yint) yint)) + month + day)) + + ;; now format the time part + (format s "~a:~a:~a" (let ((hint (parse-integer hour))) (cond ((and am (= hint 12)) "00") ((and pm (= hint 12)) "12") @@ -55,6 +62,8 @@ (t hour))) minute seconds) + + ;; and maybe format the micro seconds part (if usecs (format s ".~a" usecs) (when msecs (format s ".~a" msecs))))))))) diff --git a/test/csv-parse-date.load b/test/csv-parse-date.load index bde10ca..db74ba8 100644 --- a/test/csv-parse-date.load +++ b/test/csv-parse-date.load @@ -1,6 +1,11 @@ LOAD CSV - FROM inline ("row num", ts [date format 'YYYY-MM-DD HH24-MI-SS.US']) - INTO postgresql:///pgloader?dateformat ("row num", ts) + FROM inline + ( + "row num", + ts [date format 'YYYY-MM-DD HH24-MI-SS.US'], + hr [date format 'HH24.MI.SS'] + ) + INTO postgresql:///pgloader?dateformat ("row num", ts, hr) WITH truncate, fields optionally enclosed by '"', @@ -15,10 +20,11 @@ LOAD CSV $$ drop table if exists dateformat; $$, $$ create table dateformat ( "row num" smallint, - ts timestamp + ts timestamptz, + hr time ); $$; -1,1999-10-02 00-33-12.123456 -2,2014-10-02 00-33-13.123456 -3,2014-10-02 00-33-14.123456 +1,1999-10-02 00-33-12.123456,"00.05.02" +2,2014-10-02 00-33-13.123456,"18.25.52" +3,2014-10-02 00-33-14.123456,13.14.15