unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: "Guu, Jin-Cheng" <jcguu95@gmail.com>
To: emacs-devel@gnu.org
Subject: Shorter and more flexible implementation for parse-time.el
Date: Fri, 23 Jul 2021 12:03:33 -0500	[thread overview]
Message-ID: <CAErjG5cqB4_-__kQjKmgb_RW+-kHFtOyUpCdGF+-AoWb_bKPRw@mail.gmail.com> (raw)

[-- 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 --]

             reply	other threads:[~2021-07-23 17:03 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-23 17:03 Guu, Jin-Cheng [this message]
2021-07-24 12:12 ` Shorter and more flexible implementation for parse-time.el 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAErjG5cqB4_-__kQjKmgb_RW+-kHFtOyUpCdGF+-AoWb_bKPRw@mail.gmail.com \
    --to=jcguu95@gmail.com \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).