Skip to content

Commit

Permalink
feat: Recognize RFC 3339 formatted date-time + offset
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushalmodi committed Apr 29, 2022
1 parent 1d65064 commit 91800b2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
17 changes: 12 additions & 5 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ the Emacs core library [[https://git.savannah.gnu.org/cgit/emacs.git/tree/lisp/j

It will then be gradually refactored so that it meets the
specification defined below.
* Library Completion Status [0/6]
- [-] Scalar
* Library Completion Status [1/7]
- [X] Scalar
- [X] Boolean
- [X] Integer
- [X] Float
- [X] String
- [X] Date
- [ ] Date + Time with Offset
- [ ] Nil
- [X] Date + Time with Offset
- [ ] Nil
- [ ] Arrays
- [ ] Array of Arrays
- [ ] Tables
Expand Down Expand Up @@ -208,7 +208,8 @@ ld1 = 1979-05-27
: {
: "ld1": "1979-05-27"
: }
*** Date + Time with Offset
*** DONE Date + Time with Offset
CLOSED: [2022-04-28 Thu 22:55]
https://toml.io/en/v1.0.0#offset-date-time
**** S-expression
#+begin_src emacs-lisp :eval no :noweb-ref scalar-odt
Expand All @@ -217,6 +218,12 @@ https://toml.io/en/v1.0.0#offset-date-time
(odt3 . "1979-05-27T00:32:00.999999-07:00"))
#+end_src
**** TOML
#+begin_src emacs-lisp :noweb yes :exports results :wrap src toml
(tomelr-encode
<<scalar-odt>>)
#+end_src

#+RESULTS:
#+begin_src toml
odt1 = 1979-05-27T07:32:00Z
odt2 = 1979-05-27T00:32:00-07:00
Expand Down
14 changes: 14 additions & 0 deletions test/tscalar.el
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ Violets are blue\"\"\""
(push (tomelr-encode el) out))
(should (equal ref (nreverse out)))))

;;;; Scalar - Date + Time
(ert-deftest test-scalar-date-time ()
(let ((inp '(((odt1 . "1979-05-27T07:32:00Z"))
((odt2 . "1979-05-27 07:32:00Z"))
((odt3 . "1979-05-27T00:32:00-07:00"))
((odt4 . "1979-05-27T00:32:00.999999+04:00"))))
(ref '("odt1 = 1979-05-27T07:32:00Z"
"odt2 = 1979-05-27 07:32:00Z"
"odt3 = 1979-05-27T00:32:00-07:00"
"odt4 = 1979-05-27T00:32:00.999999+04:00"))
out)
(dolist (el inp)
(push (tomelr-encode el) out))
(should (equal ref (nreverse out)))))


(provide 'tscalar)
22 changes: 10 additions & 12 deletions tomelr.el
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,11 @@ If TRIM-INIT-CHARS is positive, those many initial characters
of the STRING are not inserted.
Return the same STRING passed as input."
;; (message "[tomelr--print-string DBG] string = `%s'" string)
(let ((special-chars '((?b . ?\b) ;U+0008
(?f . ?\f) ;U+000C
(?\\ . ?\\)))
special-chars-re
(special-chars-re (rx (in ?\" ?\\ cntrl ?\u007F))) ;cntrl is same as (?\u0000 . ?\u001F)
begin-q end-q)
(cond
((string-match-p tomelr--date-time-regexp string)) ;RFC 3339 formatted date-time with offset
Expand All @@ -176,26 +177,23 @@ Return the same STRING passed as input."
(setq begin-q "\"\"\"\n")
(setq end-q "\"\"\""))
(t ;Basic quotation "STRING"
(setq special-chars-re (rx (in ?\" ?\\ cntrl ?\u007F))) ;cntrl is same as (?\u0000 . ?\u001F)
(push '(?\" . ?\") special-chars)
(push '(?t . ?\t) special-chars) ;U+0009
(push '(?n . ?\n) special-chars) ;U+000A
(push '(?r . ?\r) special-chars) ;U+000D
(setq begin-q "\"")
(setq end-q begin-q)))
;; (message "[tomelr--print-string DBG] string = `%s'" string)
(and begin-q (insert begin-q))
(goto-char (prog1 (point) (princ string)))
(and trim-init-chars (delete-char trim-init-chars))
(when special-chars-re
(while (re-search-forward special-chars-re nil :noerror)
(let ((char (preceding-char)))
(delete-char -1)
(insert ?\\ (or
;; Escape special characters
(car (rassq char special-chars))
;; Fallback: UCS code point in \uNNNN form.
(format "u%04x" char))))))
(while (re-search-forward special-chars-re nil :noerror)
(let ((char (preceding-char)))
(delete-char -1)
(insert ?\\ (or
;; Escape special characters
(car (rassq char special-chars))
;; Fallback: UCS code point in \uNNNN form.
(format "u%04x" char)))))
(and end-q (insert end-q))
string))

Expand Down

0 comments on commit 91800b2

Please sign in to comment.