From 6a87416d61794af1bdde80f696a0595f215e7baa Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Tue, 30 Jul 2019 17:01:35 +0200 Subject: [PATCH] Use iso8601-parse in nnrss * 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 | 51 ++++++++++------------------------- test/lisp/gnus/nnrss-tests.el | 29 ++++++++++++++++++++ 2 files changed, 43 insertions(+), 37 deletions(-) create mode 100644 test/lisp/gnus/nnrss-tests.el diff --git a/lisp/gnus/nnrss.el b/lisp/gnus/nnrss.el index 0bfecb28e09..f2c86ee44e8 100644 --- a/lisp/gnus/nnrss.el +++ b/lisp/gnus/nnrss.el @@ -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 index 00000000000..184c592ea22 --- /dev/null +++ b/test/lisp/gnus/nnrss-tests.el @@ -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 . + +;;; 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 -- 2.39.2