diff options
| author | Ulrich Müller | 2024-08-14 13:57:16 +0200 |
|---|---|---|
| committer | Ulrich Müller | 2024-08-15 08:17:15 +0200 |
| commit | 49e7f1b92daaaa12e42de93d1f7604ae0a1bbeaa (patch) | |
| tree | 6aeaf5d534c9a05eee65029df89a5f590498726e | |
| parent | 2533a60e4256336eb30786a555a8922326c49a8a (diff) | |
| download | emacs-49e7f1b92daaaa12e42de93d1f7604ae0a1bbeaa.tar.gz emacs-49e7f1b92daaaa12e42de93d1f7604ae0a1bbeaa.zip | |
Drop fallback code in date-to-time, update documentation
* lisp/calendar/time-date.el (date-to-time): Drop fallback code.
Document that the default timezone is local time, rather than GMT.
* test/lisp/calendar/time-date-tests.el (test-date-to-time):
Add more test cases.
* doc/lispref/os.texi (Time Parsing): Document that 'date-to-time'
defaults to local time.
* etc/NEWS: Announce the change. (Bug#72570)
| -rw-r--r-- | doc/lispref/os.texi | 4 | ||||
| -rw-r--r-- | etc/NEWS | 9 | ||||
| -rw-r--r-- | lisp/calendar/time-date.el | 19 | ||||
| -rw-r--r-- | test/lisp/calendar/time-date-tests.el | 24 |
4 files changed, 37 insertions, 19 deletions
diff --git a/doc/lispref/os.texi b/doc/lispref/os.texi index 5839de4a650..ddaa1de221c 100644 --- a/doc/lispref/os.texi +++ b/doc/lispref/os.texi | |||
| @@ -1788,8 +1788,8 @@ Conversion}). | |||
| 1788 | This function parses the time-string @var{string} and returns the | 1788 | This function parses the time-string @var{string} and returns the |
| 1789 | corresponding Lisp timestamp. The argument @var{string} should represent | 1789 | corresponding Lisp timestamp. The argument @var{string} should represent |
| 1790 | a date-time, and should be in one of the forms recognized by | 1790 | a date-time, and should be in one of the forms recognized by |
| 1791 | @code{parse-time-string} (see below). This function assumes Universal | 1791 | @code{parse-time-string} (see below). This function assumes local time |
| 1792 | Time if @var{string} lacks explicit time zone information, | 1792 | if @var{string} lacks explicit time zone information, |
| 1793 | and assumes earliest values if @var{string} lacks month, day, or time. | 1793 | and assumes earliest values if @var{string} lacks month, day, or time. |
| 1794 | The operating system limits the range of time and zone values. | 1794 | The operating system limits the range of time and zone values. |
| 1795 | @end defun | 1795 | @end defun |
| @@ -257,6 +257,15 @@ language A. | |||
| 257 | If supplied, 'string-pixel-width' will use any face remappings from | 257 | If supplied, 'string-pixel-width' will use any face remappings from |
| 258 | BUFFER when computing the string's width. | 258 | BUFFER when computing the string's width. |
| 259 | 259 | ||
| 260 | +++ | ||
| 261 | ** 'date-to-time' now defaults to local time | ||
| 262 | The function now assumes local time instead of Universal Time when | ||
| 263 | its argument lacks explicit time zone information. This has been the | ||
| 264 | de-facto behavior since Emacs 24 although documentation said otherwise. | ||
| 265 | Also, the fallback on 'timezone-make-date-arpa-standard' has been | ||
| 266 | removed because its supported date styles can be handled by | ||
| 267 | 'parse-time-string'. | ||
| 268 | |||
| 260 | 269 | ||
| 261 | * Changes in Emacs 31.1 on Non-Free Operating Systems | 270 | * Changes in Emacs 31.1 on Non-Free Operating Systems |
| 262 | 271 | ||
diff --git a/lisp/calendar/time-date.el b/lisp/calendar/time-date.el index b441974b943..9a2fb45e3bc 100644 --- a/lisp/calendar/time-date.el +++ b/lisp/calendar/time-date.el | |||
| @@ -145,14 +145,10 @@ it is assumed that PICO was omitted and should be treated as zero." | |||
| 145 | (autoload 'timezone-make-date-arpa-standard "timezone") | 145 | (autoload 'timezone-make-date-arpa-standard "timezone") |
| 146 | 146 | ||
| 147 | ;;;###autoload | 147 | ;;;###autoload |
| 148 | ;; `parse-time-string' isn't sufficiently general or robust. It fails | ||
| 149 | ;; to grok some of the formats that timezone does (e.g. dodgy | ||
| 150 | ;; post-2000 stuff from some Elms) and either fails or returns bogus | ||
| 151 | ;; values. timezone-make-date-arpa-standard should help. | ||
| 152 | (defun date-to-time (date) | 148 | (defun date-to-time (date) |
| 153 | "Parse a string DATE that represents a date-time and return a time value. | 149 | "Parse a string DATE that represents a date-time and return a time value. |
| 154 | DATE should be in one of the forms recognized by `parse-time-string'. | 150 | DATE should be in one of the forms recognized by `parse-time-string'. |
| 155 | If DATE lacks timezone information, GMT is assumed." | 151 | If DATE lacks time zone information, local time is assumed." |
| 156 | (condition-case err | 152 | (condition-case err |
| 157 | ;; Parse DATE. If it contains a year, use defaults for other components. | 153 | ;; Parse DATE. If it contains a year, use defaults for other components. |
| 158 | ;; Then encode the result; this signals an error if the year is missing, | 154 | ;; Then encode the result; this signals an error if the year is missing, |
| @@ -164,16 +160,9 @@ If DATE lacks timezone information, GMT is assumed." | |||
| 164 | (decoded-time-set-defaults parsed)) | 160 | (decoded-time-set-defaults parsed)) |
| 165 | (encode-time parsed)) | 161 | (encode-time parsed)) |
| 166 | (error | 162 | (error |
| 167 | (let ((overflow-error '(error "Specified time is not representable"))) | 163 | (if (equal err '(error "Specified time is not representable")) |
| 168 | (if (equal err overflow-error) | 164 | (signal (car err) (cdr err)) |
| 169 | (signal (car err) (cdr err)) | 165 | (error "Invalid date: %s" date))))) |
| 170 | (condition-case err | ||
| 171 | (encode-time (parse-time-string | ||
| 172 | (timezone-make-date-arpa-standard date))) | ||
| 173 | (error | ||
| 174 | (if (equal err overflow-error) | ||
| 175 | (signal (car err) (cdr err)) | ||
| 176 | (error "Invalid date: %s" date))))))))) | ||
| 177 | 166 | ||
| 178 | ;;;###autoload | 167 | ;;;###autoload |
| 179 | (defalias 'time-to-seconds #'float-time) | 168 | (defalias 'time-to-seconds #'float-time) |
diff --git a/test/lisp/calendar/time-date-tests.el b/test/lisp/calendar/time-date-tests.el index 6512dd0bd07..f8e434e17b1 100644 --- a/test/lisp/calendar/time-date-tests.el +++ b/test/lisp/calendar/time-date-tests.el | |||
| @@ -42,8 +42,28 @@ | |||
| 42 | '(1 2 3 4)))) | 42 | '(1 2 3 4)))) |
| 43 | 43 | ||
| 44 | (ert-deftest test-date-to-time () | 44 | (ert-deftest test-date-to-time () |
| 45 | (should (equal (format-time-string "%F %T" (date-to-time "2021-12-04")) | 45 | (let ((date-list |
| 46 | "2021-12-04 00:00:00"))) | 46 | '(("2021-12-04" (00 00 00 04 12 2021 nil -1 nil)) |
| 47 | ("2006-05-04T03:02:01Z" (01 02 03 04 05 2006 nil nil 0)) | ||
| 48 | ;; Test cases from timezone-parse-date docstring | ||
| 49 | ("14 Apr 89 03:20" (00 20 03 14 04 1989 nil -1 nil)) | ||
| 50 | ("14 Apr 89 03:20:12 GMT" (12 20 03 14 04 1989 nil nil 0)) | ||
| 51 | ("Fri, 17 Mar 89 4:01" (00 01 04 17 03 1989 nil -1 nil)) | ||
| 52 | ("Fri, 17 Mar 89 4:01:33 GMT" (33 01 04 17 03 1989 nil nil 0)) | ||
| 53 | ("Mon Jan 16 16:12 1989" (00 12 16 16 01 1989 nil -1 nil)) | ||
| 54 | ("Mon Jan 16 16:12:37 GMT 1989" (37 12 16 16 01 1989 nil nil 0)) | ||
| 55 | ("Thu, 11 Apr 16:17:12 91" (12 17 16 11 04 1991 nil -1 nil)) | ||
| 56 | ("Mon, 6 Jul 16:47:20 T 1992" (20 47 16 06 07 1992 nil -1 nil)) | ||
| 57 | ("1996-06-24 21:13:12" (12 13 21 24 06 1996 nil -1 nil)) | ||
| 58 | ("19960624t211312" (12 13 21 24 06 1996 nil -1 nil)) | ||
| 59 | ;; These are parsed incorrectly: | ||
| 60 | ;; "6 May 1992 1641-JST (Wednesday)" | ||
| 61 | ;; "22-AUG-1993 10:59:12.82" | ||
| 62 | ;; "1996-06-24 21:13-ZONE" | ||
| 63 | ))) | ||
| 64 | (dolist (date date-list) | ||
| 65 | (should (equal (date-to-time (car date)) | ||
| 66 | (encode-time (cadr date))))))) | ||
| 47 | 67 | ||
| 48 | (ert-deftest test-days-between () | 68 | (ert-deftest test-days-between () |
| 49 | (should (equal (days-between "2021-10-22" "2020-09-29") 388))) | 69 | (should (equal (days-between "2021-10-22" "2020-09-29") 388))) |