From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: "Guu, Jin-Cheng" Newsgroups: gmane.emacs.devel Subject: Shorter and more flexible implementation for parse-time.el Date: Fri, 23 Jul 2021 12:03:33 -0500 Message-ID: Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="000000000000df914105c7cd627c" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="5351"; mail-complaints-to="usenet@ciao.gmane.io" To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Fri Jul 23 19:04:53 2021 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m6ybV-0001DM-2N for ged-emacs-devel@m.gmane-mx.org; Fri, 23 Jul 2021 19:04:53 +0200 Original-Received: from localhost ([::1]:35538 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m6ybU-0000hl-2C for ged-emacs-devel@m.gmane-mx.org; Fri, 23 Jul 2021 13:04:52 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:57798) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m6yaU-0007zE-Lg for emacs-devel@gnu.org; Fri, 23 Jul 2021 13:03:50 -0400 Original-Received: from mail-ed1-x534.google.com ([2a00:1450:4864:20::534]:34732) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1m6yaS-00059G-TO for emacs-devel@gnu.org; Fri, 23 Jul 2021 13:03:50 -0400 Original-Received: by mail-ed1-x534.google.com with SMTP id da26so2612075edb.1 for ; Fri, 23 Jul 2021 10:03:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=ZJXcwNQHucAmg5/KAjFDakV2qYZ8ByBWj0IcP1xbkDE=; b=c3EMLEJmpZmNfuAh8d0r3bBpRK1FKiLnkkIQcMdUFMfFoMOf1l4hKs4XvtTYSSLId7 BwFqAE8oRqWHuAkC1cnXbAy3mXJ1lMlZtpdHosiSMoJpILH+CBXO/ncyJrlEm75KkEZ4 +FI4pjhkwAysddw+W7MvrjhNcC9NR3ygpWIPvHZJDdTFQC3ySTYeZ8qZpMxpMThmHryj ts6tuibD1rK5Jz53L3J9lJPFmTLSz9EH46kj5iowdpKQGn0t1Lic/ihk5XZfVt3bmfZ5 8Je68ccIu9upihKDbwvb3RF8fT5IkbITO2uXAHP0N+hlqpSQg8YrJRLKQqYN2DoaDrZ6 J6xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=ZJXcwNQHucAmg5/KAjFDakV2qYZ8ByBWj0IcP1xbkDE=; b=JQ6pKl0F825knGomQfpvOmuKm/AluiBD54DbNZhOp8u2XRJ5JkgzOS1KK6MOliqF38 fPeOXNhbbOhD3UrFONq21IrO2qxxZO4RqYtMN9PVsFAS75drsDyW788YaG8qK+VdfOQc qny/x3Wf1BwBtZaYLMCnzuc4VSgEfbxrINn5IiPGK/dvT++Whr4Zy2z7GUcsn4oO9ZVg cDt7c56kojXkrQSREu/oIBp1OOTG9/4YrtnAor085bUrNkrFNoF9+AcP40tpgKPFPXq2 o68D4IyA93eY9Ci6nErYTuCJDaUNHxDKVNwZpopZP+0oM9WVMdL6XFNq5J7fr23iuV+8 bjkw== X-Gm-Message-State: AOAM530tDrptjyseGX3r6D6u2Lz48Qk/hy6cSKHSfTlWnFUW+mdNnBNd yQ26B6+mPe34ur0L+vYOKjwaNrzgacCd72EEaNa7fWeRFX4TCw== X-Google-Smtp-Source: ABdhPJzQ/+k+eY0QKI7uVQO7CoOPWcGBh0cANul1kNMbs3jilOwyNRUAC7P9wpbklb9H3gOdbWtqwy+8oOa7fy/tymc= X-Received: by 2002:a05:6402:548:: with SMTP id i8mr6851468edx.250.1627059824593; Fri, 23 Jul 2021 10:03:44 -0700 (PDT) Received-SPF: pass client-ip=2a00:1450:4864:20::534; envelope-from=jcguu95@gmail.com; helo=mail-ed1-x534.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:271503 Archived-At: --000000000000df914105c7cd627c Content-Type: text/plain; charset="UTF-8" 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 --000000000000df914105c7cd627c Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hello,

(This is my first post. Please correct me if I miss= ed anything. =3D) )

Currently, the built-in time parsing functions onl= y allow us to parse timestrings with respect to some given formats (ISO-860= 1, 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:

``` ema= cs-lisp
(my/parse-time "<=
/span>20200718-201504"
               '((:year 4) (:month =
2) (:day=
 2) "-"
                 (:hour 2) (:mi=
nute 2) (:second=
 2)))
;; =3D> ((:second . 4)=
 (:minute . 15) (:hour . 20)
;;     (:day . 18) (:mont=
h . 7) (:year . 2020))
```

One can even parse org tim= estamps easily with the format=C2=A0

``` emacs-lisp
'("["= ; (:year 4) "-" (:month 2) "-" (:day 2) "]")<= /div>
= ```

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 <=
span class=3D"gmail-pl-v" style=3D"box-sizing:border-box">format)
  "Parse time stri=
ng with customized format, and return an alist.
A format is a li=
st of directives. A directive is either a string
or a list (A B),=
 where A is a keyword, and B is an integer."
  (flet =
((parse-step
          (directive str)
          (if (atom directive)
              (i=
f (strin=
g-match (format "^<=
span class=3D"gmail-pl-c1" style=3D"box-sizing:border-box">%s=
" =
directive) str)
                  (list (substring str (length directive)))
                (error "Pa=
rsing failure~ directive: %s; str: %s." directive str))
            (let=
* ((key (=
car directive))
                   (int (car (cdr directive)))
                   (to-parse (substring str 0 int))
                   ;; TOD=
O For natural lang, replace
                   ;; par=
se-integer by any customized
                   ;; tra=
nsformers.
                   (value (cl-parse-integer to-parse)))
              (l=
ist (sub=
string str int) key value)))))
    (let =
(result)
      (while (not =
(equal st=
r ""))
        (let* ((return (parse-step (pop format) str))
               (new-str (car return))
               (key (car (cdr return)))
               (value (car (cddr return))))
          (setf<=
/span> str new-str)
          (when<=
/span> key (setf=
 (alist-=
get key result) value))))
```

--000000000000df914105c7cd627c--