]> git.eshelyaron.com Git - emacs.git/commitdiff
Use iso8601-parse in nnrss
authorLars Ingebrigtsen <larsi@gnus.org>
Tue, 30 Jul 2019 15:01:35 +0000 (17:01 +0200)
committerLars Ingebrigtsen <larsi@gnus.org>
Wed, 31 Jul 2019 19:47:29 +0000 (21:47 +0200)
* lisp/gnus/nnrss.el (nnrss-normalize-date): Use iso8601-parse
instead of hand-rolled parser.

* test/lisp/gnus/nnrss-tests.el: New file.

lisp/gnus/nnrss.el
test/lisp/gnus/nnrss-tests.el [new file with mode: 0644]

index 0bfecb28e098acd73efd0ca9faefa9f5a8b9bdab..f2c86ee44e87a48ab3c3aee670e08b3623a79bf4 100644 (file)
@@ -36,6 +36,7 @@
 (require 'rfc2231)
 (require 'mm-url)
 (require 'rfc2047)
+(require 'iso8601)
 (require 'mml)
 (require 'xml)
 
@@ -468,49 +469,25 @@ which RSS 2.0 allows."
                        (not (string-match "\\`[A-Z+-]" zone)))
               (setq zone nil))))
          ;; ISO 8601
-         ((string-match
-           (eval-when-compile
-             (concat
-              ;; 1. year
-              "\\(199[0-9]\\|20[0-9][0-9]\\)"
-              "\\(?:-"
-              ;; 2. month
-              "\\([01][0-9]\\)"
-              "\\(?:-"
-              ;; 3. day
-              "\\([0-3][0-9]\\)"
-              "\\)?\\)?\\(?:T"
-              ;; 4. hh:mm
-              "\\([012][0-9]:[0-5][0-9]\\)"
-              "\\(?:"
-              ;; 5. :ss
-              "\\(:[0-5][0-9]\\)"
-              "\\(?:\\.[0-9]+\\)?\\)?\\)?"
-              ;; 6+7,8,9. zone
-              "\\(?:\\(?:\\([+-][012][0-9]\\):\\([0-5][0-9]\\)\\)"
-              "\\|\\([+-][012][0-9][0-5][0-9]\\)"
-              "\\|\\(Z\\)\\)?"))
-           date)
-          (setq year (string-to-number (match-string 1 date))
-                month (string-to-number (or (match-string 2 date) "1"))
-                day (string-to-number (or (match-string 3 date) "1"))
-                time (if (match-beginning 5)
-                         (substring date (match-beginning 4) (match-end 5))
-                       (concat (or (match-string 4 date) "00:00") ":00"))
-                zone (cond ((match-beginning 6)
-                            (concat (match-string 6 date)
-                                    (match-string 7 date)))
-                           ((match-beginning 9) ;; Z
-                            "+0000")
-                           (t ;; nil if zone is not provided.
-                            (match-string 8 date))))))
+         ((iso8601-valid-p date)
+          (let ((decoded (decoded-time-set-defaults (iso8601-parse date))))
+            (setq year (decoded-time-year decoded)
+                  month (decoded-time-month decoded)
+                  day (decoded-time-day decoded)
+                  time (format "%02d:%02d:%02d"
+                               (decoded-time-hour decoded)
+                               (decoded-time-minute decoded)
+                               (decoded-time-second decoded))
+                  zone (if (equal (decoded-time-zone decoded) "Z")
+                           0
+                         (decoded-time-zone decoded))))))
     (if month
        (progn
          (setq cts (current-time-string (encode-time 0 0 0 day month year)))
          (format "%s, %02d %s %04d %s%s"
                  (substring cts 0 3) day (substring cts 4 7) year time
                  (if zone
-                     (concat " " zone)
+                     (concat " " (time-zone-format zone t))
                    "")))
       (message-make-date given))))
 
diff --git a/test/lisp/gnus/nnrss-tests.el b/test/lisp/gnus/nnrss-tests.el
new file mode 100644 (file)
index 0000000..184c592
--- /dev/null
@@ -0,0 +1,29 @@
+;;; nnrss-tests.el --- tests for gnus/nnrss.el    -*- lexical-binding:t -*-
+
+;; Copyright (C) 2019 Free Software Foundation, Inc.
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
+
+;;; Code:
+
+(require 'ert)
+(require 'nnrss)
+
+(ert-deftest test-nnrss-normalize ()
+  (should (equal (nnrss-normalize-date "2004-09-17T05:09:49.001+00:00")
+                 "Fri, 17 Sep 2004 05:09:49 +0000")))
+
+;;; nnrss-tests.el ends here