~rgrjr/rgr-hacks

7340c9acb0d048dbb7899dd793ca1b84776d20b1 — Bob Rogers 5 months ago bb3c4b4
Make parse-date &optional work, add basic testing

* parse-date.el:
   + (parse-date-2):  Rename the generic function from parse-date to
     parse-date-2 because &optional doesn't work with generics.
   + (parse-date):  The entrypoint is now an ordinary function with an
     &optional format argument.
* test/test-parse-date.el (added):
   + New test script, based on Emacs date parsing.
2 files changed, 54 insertions(+), 27 deletions(-)

M parse-date.el
A test/test-parse-date.el
M parse-date.el => parse-date.el +28 -27
@@ 431,8 431,33 @@ Protocol', STD 12, RFC 1119, September 1989).")
                          (list "Unrecognized token" token)))))))
    time))

(cl-defmethod parse-date-2 (time-string (_format (eql 'iso-8601)))
  (iso8601-parse time-string))

(cl-defmethod parse-date-2 (time-string (_format (eql 'rfc5322)))
  (parse-date--x822 time-string nil))

(cl-defmethod parse-date-2 (time-string (_format (eql 'rfc-email)))
  (parse-date--x822 time-string nil))

(cl-defmethod parse-date-2 (time-string (_format (eql 'rfc822)))
  (parse-date--x822 time-string t))

(cl-defmethod parse-date-2 (time-string (_format (eql 'us-date)))
  (parse-date--default time-string t t))

(cl-defmethod parse-date-2 (time-string (_format (eql 'euro-date)))
  (parse-date--default time-string nil t))

(cl-defmethod parse-date-2 (time-string (_format (eql nil)))
  (parse-date--default time-string t nil))

(cl-defmethod parse-date-2 (time-string _format)
  ;; Re-dispatch after guessing the format.
  (parse-date-2 time-string (parse-date--guess-format time-string)))

;;;###autoload
(cl-defgeneric parse-date (time-string &optional format)
(defun parse-date (time-string &optional format)
  "Parse TIME-STRING according to FORMAT, returning a list.
The FORMAT value is a symbol that may be one of the following:



@@ 526,32 551,8 @@ e.g. `decoded-time-year' to extract the year, and turned into an
Emacs timestamp by `encode-time'.  The values returned are
identical to those of `decode-time', but any unknown values other
than DST are returned as nil, and an unknown DST value is
returned as -1.")

(cl-defmethod parse-date (time-string (_format (eql 'iso-8601)))
  (iso8601-parse time-string))

(cl-defmethod parse-date (time-string (_format (eql 'rfc5322)))
  (parse-date--x822 time-string nil))

(cl-defmethod parse-date (time-string (_format (eql 'rfc-email)))
  (parse-date--x822 time-string nil))

(cl-defmethod parse-date (time-string (_format (eql 'rfc822)))
  (parse-date--x822 time-string t))

(cl-defmethod parse-date (time-string (_format (eql 'us-date)))
  (parse-date--default time-string t t))

(cl-defmethod parse-date (time-string (_format (eql 'euro-date)))
  (parse-date--default time-string nil t))

(cl-defmethod parse-date (time-string (_format (eql nil)))
  (parse-date--default time-string t nil))

(cl-defmethod parse-date (time-string _format)
  ;; Re-dispatch after guessing the format.
  (parse-date time-string (parse-date--guess-format time-string)))
returned as -1."
  (parse-date-2 time-string format))

(provide 'parse-date)


A test/test-parse-date.el => test/test-parse-date.el +26 -0
@@ 0,0 1,26 @@
;;;; Test the parse-date.el code.
;;;
;;; [created.  -- rgr, 5-Nov-23.]
;;;

(require 'ert)

(require 'parse-date)

(ert-deftest parse-date-tests ()
  (should (equal (parse-date "Mon, 22 Feb 2016 19:35:42 +0100")
                 '(42 35 19 22 2 2016 1 -1 3600)))
  (should (equal (parse-date "22 Feb 2016 19:35:42 +0100")
                 '(42 35 19 22 2 2016 nil -1 3600)))
  (should (equal (parse-date "22 Feb 2016 +0100")
                 '(nil nil nil 22 2 2016 nil -1 3600)))
  (should (equal (parse-date "Mon, 22 February 2016 19:35:42 +0100")
                 '(42 35 19 22 2 2016 1 -1 3600)))
  (should (equal (parse-date "Mon, 22 feb 2016 19:35:42 +0100")
                 '(42 35 19 22 2 2016 1 -1 3600)))
  (should (equal (parse-date "Monday, 22 february 2016 19:35:42 +0100")
                 '(42 35 19 22 2 2016 1 -1 3600)))
  (should (equal (parse-date "Monday, 22 february 2016 19:35:42 PST")
                 '(42 35 19 22 2 2016 1 nil -28800)))
  (should (equal (parse-date "Friday, 21 Sep 2018 13:47:58 PDT")
                 '(58 47 13 21 9 2018 5 t -25200))))