format timestamps as iso 8601 upon org sqlite db insertion

This commit is contained in:
ndwarshuis 2018-12-25 23:24:25 -05:00
parent 687342f8ea
commit 82e6e8b198
1 changed files with 39 additions and 25 deletions

View File

@ -2705,16 +2705,22 @@ convert the final number to a string of the number. If IGNORE-ERR is t,
do not throw an error if the string is not recognized." do not throw an error if the string is not recognized."
(when effort-str (when effort-str
(let ((effort-str (string-trim effort-str))) (let ((effort-str (string-trim effort-str)))
(cond (save-match-data
((string-match "^\\([0-9]+\\):\\([0-6][0-9]\\)$" effort-str) (cond
(let* ((hours (string-to-number (match-string 1 effort-str))) ((string-match "^\\([0-9]+\\):\\([0-6][0-9]\\)$" effort-str)
(minutes (string-to-number (match-string 2 effort-str))) (let* ((hours (string-to-number (match-string 1 effort-str)))
(sum (+ (* 60 hours) minutes))) (minutes (string-to-number (match-string 2 effort-str)))
(if to-string (number-to-string sum) sum))) (sum (+ (* 60 hours) minutes)))
((string-match-p "^[0-9]+$" effort-str) (if to-string (number-to-string sum) sum)))
(if to-string effort-str (string-to-number effort-str))) ((string-match-p "^[0-9]+$" effort-str)
(t (unless ignore-err (if to-string effort-str (string-to-number effort-str)))
(error (concat "Unknown effort format: '" effort-str "'")))))))) (t (unless ignore-err
(error (concat "Unknown effort format: '" effort-str "'")))))))))
(defun nd/org-ts-format-to-iso (ts)
"Return org timestamp TS to as string in ISO 8601 format."
;; org-parse-time-string (used in org-2ft) does not save match data
(when ts (format-time-string "%Y-%m-%dT%H:%M:00" (save-match-data (org-2ft ts)))))
#+END_SRC #+END_SRC
**** org sql schemas **** org sql schemas
#+BEGIN_SRC emacs-lisp #+BEGIN_SRC emacs-lisp
@ -2811,11 +2817,14 @@ PRIMARY KEY (archive_file_path ASC, entry_file_offset ASC));"
**** org element functions **** org element functions
These are functions that operate on org-element objects to parse for insertion into the db. These are functions that operate on org-element objects to parse for insertion into the db.
#+BEGIN_SRC emacs-lisp #+BEGIN_SRC emacs-lisp
(defun nd/org-element-timestamp-raw (prop obj) (defun nd/org-element-timestamp-raw (prop obj &optional iso)
"Return the raw-value of the timestamp PROP in OBJ if exists." "Return the raw-value of the timestamp PROP in OBJ if exists.
If ISO is t, return the timestamp in ISO 8601 format."
(when obj (when obj
(let ((ts (org-element-property prop obj))) (let ((ts (org-element-property prop obj)))
(when ts (org-element-property :raw-value ts))))) (when ts
(let ((raw-ts (org-element-property :raw-value ts)))
(if iso (nd/org-ts-format-to-iso raw-ts) raw-ts))))))
(defun nd/org-element-find-type (type obj) (defun nd/org-element-find-type (type obj)
"Find and return the first instance of TYPE in OBJ. "Find and return the first instance of TYPE in OBJ.
@ -3018,9 +3027,10 @@ ARCHIVE-FILE-PATH is the file path to the currently parsed archive file."
(source-tree-path (nd/org-element-property-inherited :ARCHIVE_OLPATH headline)) (source-tree-path (nd/org-element-property-inherited :ARCHIVE_OLPATH headline))
(headline-text (org-element-property :raw-value headline)) (headline-text (org-element-property :raw-value headline))
(time-created (org-element-property :CREATED headline)) (time-created (org-element-property :CREATED headline))
(time-closed (nd/org-element-timestamp-raw :closed headline)) (time-created (nd/org-ts-format-to-iso time-created))
(time-scheduled (nd/org-element-timestamp-raw :scheduled headline)) (time-closed (nd/org-element-timestamp-raw :closed headline t))
(time-deadline (nd/org-element-timestamp-raw :deadline headline)) (time-scheduled (nd/org-element-timestamp-raw :scheduled headline t))
(time-deadline (nd/org-element-timestamp-raw :deadline headline t))
(keyword (org-element-property :todo-keyword headline)) (keyword (org-element-property :todo-keyword headline))
(effort (org-element-property :EFFORT headline)) (effort (org-element-property :EFFORT headline))
(effort (nd/org-effort-to-int effort t)) (effort (nd/org-effort-to-int effort t))
@ -3083,8 +3093,10 @@ ARCHIVE-FILE-PATH is the file path to the currently parsed archive file."
;; should never happen ;; should never happen
(t (error (concat "unknown timestamp type: " (t (error (concat "unknown timestamp type: "
(symbol-name timestamp-type))))) (symbol-name timestamp-type)))))
(setq time-start (org-element-property :raw-value time-start) (setq time-start (nd/org-ts-format-to-iso
time-end (org-element-property :raw-value time-end)) (org-element-property :raw-value time-start))
time-end (nd/org-ts-format-to-iso
(org-element-property :raw-value time-end)))
(nd/sql-insert nd/org-sqlite-db-path (nd/sql-insert nd/org-sqlite-db-path
"clocking" "clocking"
(list archive-file-path (list archive-file-path
@ -3146,7 +3158,8 @@ ARCHIVE-FILE-PATH is the path to the archive file."
((memq note-type '(reschedule delschedule redeadline deldeadline)) 3) ((memq note-type '(reschedule delschedule redeadline deldeadline)) 3)
((eq note-type 'state) 5) ((eq note-type 'state) 5)
(t (error (concat "Unknown type: " (symbol-name note-type)))))) (t (error (concat "Unknown type: " (symbol-name note-type))))))
(setq time-logged (match-string time-index entry-str))) (setq time-logged (nd/org-ts-format-to-iso
(match-string time-index entry-str))))
;; insert into general logbook table first ;; insert into general logbook table first
(nd/sql-insert nd/org-sqlite-db-path (nd/sql-insert nd/org-sqlite-db-path
@ -3161,10 +3174,8 @@ ARCHIVE-FILE-PATH is the path to the archive file."
(when note-type (when note-type
(cond (cond
((eq note-type 'state) ((eq note-type 'state)
(let* ((state-old (or (match-string 3 entry-str) (let* ((state-old (match-string 3 entry-str))
(match-string 4 entry-str))) (state-new (match-string 1 entry-str)))
(state-new (or (match-string 1 entry-str)
(match-string 2 entry-str))))
(nd/sql-insert nd/org-sqlite-db-path (nd/sql-insert nd/org-sqlite-db-path
"state_changes" "state_changes"
(list archive-file-path (list archive-file-path
@ -3173,11 +3184,14 @@ ARCHIVE-FILE-PATH is the path to the archive file."
state-new)))) state-new))))
((memq note-type '(reschedule delschedule redeadline deldeadline)) ((memq note-type '(reschedule delschedule redeadline deldeadline))
(let* ((time-old (match-string 1 entry-str)) (let* ((time-old (nd/org-ts-format-to-iso
(match-string 1 entry-str)))
(schedule (memq note-type '(reschedule delschedule))) (schedule (memq note-type '(reschedule delschedule)))
(time-new (nd/org-element-timestamp-raw (time-new (nd/org-element-timestamp-raw
(if schedule :scheduled :deadline) (if schedule :scheduled :deadline)
parent-headline)) parent-headline
t))
(time-new (nd/org-ts-format-to-iso time-new))
(planning-type (if schedule "s" "d"))) (planning-type (if schedule "s" "d")))
(nd/sql-insert nd/org-sqlite-db-path (nd/sql-insert nd/org-sqlite-db-path
"planning_changes" "planning_changes"