diff options
| author | Paul Eggert | 2020-01-15 17:36:59 -0800 |
|---|---|---|
| committer | Paul Eggert | 2020-01-15 17:42:11 -0800 |
| commit | 07a4dd8e6aa2787f809d12aa99b8914af91ae2b3 (patch) | |
| tree | 10e020a7600bfd4f431198cbb40682707c91c594 | |
| parent | a70feb0d730e5c70d7f75cff4fea66ec2ddf38dd (diff) | |
| download | emacs-07a4dd8e6aa2787f809d12aa99b8914af91ae2b3.tar.gz emacs-07a4dd8e6aa2787f809d12aa99b8914af91ae2b3.zip | |
parse-time-string now parses ISO 8601 format strings
* lisp/calendar/parse-time.el (parse-time-string):
Parse strings in ISO 8601 format too (Bug#39001).
(parse-time--rfc-822ish): New internal function,
containing most of the old parse-time-string implementation.
(parse-iso8601-time-string): Simplify, now that
parse-time-string groks ISO 8601.
| -rw-r--r-- | etc/NEWS | 3 | ||||
| -rw-r--r-- | lisp/calendar/parse-time.el | 25 |
2 files changed, 17 insertions, 11 deletions
| @@ -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, | ||
| 74 | such 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). |
| 152 | STRING should be something resembling an RFC 822 (or later) date-time, e.g., | 152 | STRING 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 | 153 | or something resembling an RFC 822 (or later) date-time, e.g., |
| 154 | \"Wed, 15 Jan 2020 16:12:21 -0800\". This function is | ||
| 154 | somewhat liberal in what format it accepts, and will attempt to | 155 | somewhat liberal in what format it accepts, and will attempt to |
| 155 | return a \"likely\" value even for somewhat malformed strings. | 156 | return a \"likely\" value even for somewhat malformed strings. |
| 156 | The values returned are identical to those of `decode-time', but | 157 | The values returned are identical to those of `decode-time', but |
| 157 | any unknown values other than DST are returned as nil, and an | 158 | any unknown values other than DST are returned as nil, and an |
| 158 | unknown DST value is returned as -1." | 159 | unknown 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\". |
| 200 | If DATE-STRING cannot be parsed, it falls back to | 207 | Fall back on parsing something resembling an RFC 822 (or later) date-time. |
| 201 | `parse-time-string'." | 208 | This function is like `parse-time-string' except that it returns |
| 202 | (when-let ((time | 209 | a 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) |