unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Shorter and more flexible implementation for parse-time.el
@ 2021-07-23 17:03 Guu, Jin-Cheng
  2021-07-24 12:12 ` Lars Ingebrigtsen
  0 siblings, 1 reply; 7+ messages in thread
From: Guu, Jin-Cheng @ 2021-07-23 17:03 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 2271 bytes --]

Hello,

(This is my first post. Please correct me if I missed anything. =) )

Currently, the built-in time parsing functions only allow us to parse
timestrings with respect to some given formats (ISO-8601, RFC-822) [1]. I
have a parsing function that allows users to parse with customized formats
which are easy to write. The end result is:

``` emacs-lisp

(my/parse-time "20200718-201504"
               '((:year 4) (:month 2) (:day 2) "-"
                 (:hour 2) (:minute 2) (:second 2)));; => ((:second .
4) (:minute . 15) (:hour . 20);;     (:day . 18) (:month . 7) (:year .
2020))

```

One can even parse org timestamps easily with the format

``` emacs-lisp
'("[" (:year 4) "-" (:month 2) "-" (:day 2) "]")
```

The code is ~25 lines long, which is easily extendable (see below). I
wonder if there's any interest to add this into emacs. I can write tests
and benchmarks to make sure that it doesn't change the user space.

``` emacs-lisp

;;; Actual code

(defun my/parse-time (str format)
  "Parse time string with customized format, and return an alist.A
format is a list of directives. A directive is either a stringor a
list (A B), where A is a keyword, and B is an integer."
  (flet ((parse-step
          (directive str)
          (if (atom directive)
              (if (string-match (format "^%s" directive) str)
                  (list (substring str (length directive)))
                (error "Parsing failure~ directive: %s; str: %s."
directive str))
            (let* ((key (car directive))
                   (int (car (cdr directive)))
                   (to-parse (substring str 0 int))
                   ;; TODO For natural lang, replace
                   ;; parse-integer by any customized
                   ;; transformers.
                   (value (cl-parse-integer to-parse)))
              (list (substring str int) key value)))))
    (let (result)
      (while (not (equal str ""))
        (let* ((return (parse-step (pop format) str))
               (new-str (car return))
               (key (car (cdr return)))
               (value (car (cddr return))))
          (setf str new-str)
          (when key (setf (alist-get key result) value))))

```

[1]
https://github.com/emacs-mirror/emacs/blob/master/lisp/calendar/parse-time.el

[-- Attachment #2: Type: text/html, Size: 10136 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2021-07-25 10:14 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-23 17:03 Shorter and more flexible implementation for parse-time.el Guu, Jin-Cheng
2021-07-24 12:12 ` Lars Ingebrigtsen
2021-07-24 15:03   ` Guu, Jin-Cheng
2021-07-25  6:34     ` Lars Ingebrigtsen
2021-07-25  9:38       ` Guu, Jin-Cheng
2021-07-25  9:58         ` Yuri Khan
2021-07-25 10:14           ` Arthur Miller

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).