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.
This commit is contained in:
Dimitri Fontaine 2015-09-07 17:05:10 +02:00
parent bd50ba45ea
commit 04b2779239
2 changed files with 27 additions and 12 deletions

View File

@ -42,12 +42,19 @@
(string= day "00")) (string= day "00"))
nil nil
(with-output-to-string (s) (with-output-to-string (s)
(format s "~a-~a-~a ~a:~a:~a" ;; when given a full date format, format the date part
(let ((yint (parse-integer year))) (when (and (assoc :year format)
;; process 2-digits year formats specially (assoc :month format)
(if (<= (length year) 2) (+ *century* yint) yint)) (assoc :day format))
month (format s "~a-~a-~a "
day (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))) (let ((hint (parse-integer hour)))
(cond ((and am (= hint 12)) "00") (cond ((and am (= hint 12)) "00")
((and pm (= hint 12)) "12") ((and pm (= hint 12)) "12")
@ -55,6 +62,8 @@
(t hour))) (t hour)))
minute minute
seconds) seconds)
;; and maybe format the micro seconds part
(if usecs (format s ".~a" usecs) (if usecs (format s ".~a" usecs)
(when msecs (format s ".~a" msecs))))))))) (when msecs (format s ".~a" msecs)))))))))

View File

@ -1,6 +1,11 @@
LOAD CSV LOAD CSV
FROM inline ("row num", ts [date format 'YYYY-MM-DD HH24-MI-SS.US']) FROM inline
INTO postgresql:///pgloader?dateformat ("row num", ts) (
"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, WITH truncate,
fields optionally enclosed by '"', fields optionally enclosed by '"',
@ -15,10 +20,11 @@ LOAD CSV
$$ drop table if exists dateformat; $$, $$ drop table if exists dateformat; $$,
$$ create table dateformat ( $$ create table dateformat (
"row num" smallint, "row num" smallint,
ts timestamp ts timestamptz,
hr time
); );
$$; $$;
1,1999-10-02 00-33-12.123456 1,1999-10-02 00-33-12.123456,"00.05.02"
2,2014-10-02 00-33-13.123456 2,2014-10-02 00-33-13.123456,"18.25.52"
3,2014-10-02 00-33-14.123456 3,2014-10-02 00-33-14.123456,13.14.15