aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--etc/NEWS3
-rw-r--r--lisp/calendar/parse-time.el25
2 files changed, 17 insertions, 11 deletions
diff --git a/etc/NEWS b/etc/NEWS
index 0e43c321d81..3d5915a3774 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -70,6 +70,9 @@ called when the function object is garbage-collected. Use
70'set_function_finalizer' to set the finalizer and 70'set_function_finalizer' to set the finalizer and
71'get_function_finalizer' to retrieve it. 71'get_function_finalizer' to retrieve it.
72 72
73** 'parse-time-string' can now parse ISO 8601 format strings,
74such as "2020-01-15T16:12:21-08:00".
75
73 76
74* Changes in Emacs 28.1 on Non-Free Operating Systems 77* Changes in Emacs 28.1 on Non-Free Operating Systems
75 78
diff --git a/lisp/calendar/parse-time.el b/lisp/calendar/parse-time.el
index 7110a81f0de..4d4f88efffb 100644
--- a/lisp/calendar/parse-time.el
+++ b/lisp/calendar/parse-time.el
@@ -149,13 +149,20 @@ letters, digits, plus or minus signs or colons."
149;;;###autoload 149;;;###autoload
150(defun parse-time-string (string) 150(defun parse-time-string (string)
151 "Parse the time in STRING into (SEC MIN HOUR DAY MON YEAR DOW DST TZ). 151 "Parse the time in STRING into (SEC MIN HOUR DAY MON YEAR DOW DST TZ).
152STRING should be something resembling an RFC 822 (or later) date-time, e.g., 152STRING should be an ISO 8601 time string, e.g., \"2020-01-15T16:12:21-08:00\",
153\"Fri, 25 Mar 2016 16:24:56 +0100\", but this function is 153or something resembling an RFC 822 (or later) date-time, e.g.,
154\"Wed, 15 Jan 2020 16:12:21 -0800\". This function is
154somewhat liberal in what format it accepts, and will attempt to 155somewhat liberal in what format it accepts, and will attempt to
155return a \"likely\" value even for somewhat malformed strings. 156return a \"likely\" value even for somewhat malformed strings.
156The values returned are identical to those of `decode-time', but 157The values returned are identical to those of `decode-time', but
157any unknown values other than DST are returned as nil, and an 158any unknown values other than DST are returned as nil, and an
158unknown DST value is returned as -1." 159unknown DST value is returned as -1."
160 (condition-case ()
161 (decoded-time-set-defaults (iso8601-parse string))
162 (wrong-type-argument
163 (parse-time--rfc-822ish string))))
164
165(defun parse-time--rfc-822ish (string)
159 (let ((time (list nil nil nil nil nil nil nil -1 nil)) 166 (let ((time (list nil nil nil nil nil nil nil -1 nil))
160 (temp (parse-time-tokenize (downcase string)))) 167 (temp (parse-time-tokenize (downcase string))))
161 (while temp 168 (while temp
@@ -196,15 +203,11 @@ unknown DST value is returned as -1."
196 time)) 203 time))
197 204
198(defun parse-iso8601-time-string (date-string) 205(defun parse-iso8601-time-string (date-string)
199 "Parse an ISO 8601 time string, such as 2016-12-01T23:35:06-05:00. 206 "Parse an ISO 8601 time string, such as \"2020-01-15T16:12:21-08:00\".
200If DATE-STRING cannot be parsed, it falls back to 207Fall back on parsing something resembling an RFC 822 (or later) date-time.
201`parse-time-string'." 208This function is like `parse-time-string' except that it returns
202 (when-let ((time 209a Lisp timestamp when successful."
203 (if (iso8601-valid-p date-string) 210 (when-let ((time (parse-time-string date-string)))
204 (decoded-time-set-defaults (iso8601-parse date-string))
205 ;; Fall back to having `parse-time-string' do fancy
206 ;; things for us.
207 (parse-time-string date-string))))
208 (encode-time time))) 211 (encode-time time)))
209 212
210(provide 'parse-time) 213(provide 'parse-time)