From: Lars Ingebrigtsen Date: Wed, 31 Jul 2019 13:17:04 +0000 (+0200) Subject: Rewrite `parse-iso8601-time-string' to use `iso8601-parse' X-Git-Tag: emacs-27.0.90~1794 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=296002ba5aefa60f8a1f97c4d28808683247ce8b;p=emacs.git Rewrite `parse-iso8601-time-string' to use `iso8601-parse' * lisp/calendar/parse-time.el (parse-iso8601-time-string): Use `iso8601-parse'. (parse-time-iso8601-regexp): Remove. --- diff --git a/lisp/calendar/parse-time.el b/lisp/calendar/parse-time.el index 68d6ce05d6c..e28df97918e 100644 --- a/lisp/calendar/parse-time.el +++ b/lisp/calendar/parse-time.el @@ -36,6 +36,8 @@ ;;; Code: (require 'cl-lib) +(require 'iso8601) +(eval-when-compile (require 'subr-x)) ;; Byte-compiler warnings (defvar parse-time-elt) @@ -193,75 +195,17 @@ unknown DST value is returned as -1." (setf (nth (pop slots) time) new-val)))))))) time)) -(defconst parse-time-iso8601-regexp - (let* ((dash "-?") - (colon ":?") - (4digit "\\([0-9][0-9][0-9][0-9]\\)") - (2digit "\\([0-9][0-9]\\)") - (date-fullyear 4digit) - (date-month 2digit) - (date-mday 2digit) - (time-hour 2digit) - (time-minute 2digit) - (time-second 2digit) - (time-secfrac "\\(\\.[0-9]+\\)?") - (time-numoffset (concat "\\([-+]\\)" time-hour ":?" time-minute "?")) - (partial-time (concat time-hour colon time-minute colon time-second - time-secfrac)) - (full-date (concat date-fullyear dash date-month dash date-mday))) - (list (concat "^" full-date) - (concat "T" partial-time) - (concat "\\(Z\\|" time-numoffset "\\)"))) - "List of regular expressions matching ISO 8601 dates. -1st regular expression matches the date. -2nd regular expression matches the time. -3rd regular expression matches the (optional) timezone specification.") - (defun parse-iso8601-time-string (date-string) "Parse an ISO 8601 time string, such as 2016-12-01T23:35:06-05:00. If DATE-STRING cannot be parsed, it falls back to `parse-time-string'." - (let* ((date-re (nth 0 parse-time-iso8601-regexp)) - (time-re (nth 1 parse-time-iso8601-regexp)) - (tz-re (nth 2 parse-time-iso8601-regexp)) - re-start - time seconds minute hour - day month year day-of-week (dst -1) tz) - ;; We need to populate 'time' with - ;; (SEC MIN HOUR DAY MON YEAR DOW DST TZ) - - ;; Nobody else handles iso8601 correctly, let's do it ourselves. - (when (string-match date-re date-string re-start) - (setq year (string-to-number (match-string 1 date-string)) - month (string-to-number (match-string 2 date-string)) - day (string-to-number (match-string 3 date-string)) - re-start (match-end 0)) - (when (string-match time-re date-string re-start) - (setq hour (string-to-number (match-string 1 date-string)) - minute (string-to-number (match-string 2 date-string)) - seconds (string-to-number (match-string 3 date-string)) - re-start (match-end 0)) - (when (string-match tz-re date-string re-start) - (setq dst nil) - (setq tz (if (string= "Z" (match-string 1 date-string)) - 0 ;; UTC timezone indicated by Z - (let ((tz (+ - (* 3600 - (string-to-number - (match-string 3 date-string))) - (* 60 - (string-to-number - (or (match-string 4 date-string) "0")))))) - (if (string= "-" (match-string 2 date-string)) - (- tz) tz))))) - (setq time (list seconds minute hour day month year day-of-week dst tz)))) - - ;; Fall back to having `parse-time-string' do fancy things for us. - (when (not time) - (setq time (parse-time-string date-string))) - - (and time - (encode-time time)))) + (when-let ((time + (if (iso8601-valid-p date-string) + (decoded-time-set-defaults (iso8601-parse date-string)) + ;; Fall back to having `parse-time-string' do fancy + ;; things for us. + (parse-time-string date-string)))) + (encode-time time))) (provide 'parse-time)