unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#52209: 28.0.60; [PATCH] date-to-time fails on pure dates
@ 2021-11-30 20:55 Bob Rogers
  2021-12-01  4:17 ` Lars Ingebrigtsen
  2021-12-04 18:58 ` Paul Eggert
  0 siblings, 2 replies; 40+ messages in thread
From: Bob Rogers @ 2021-11-30 20:55 UTC (permalink / raw)
  To: 52209

[-- Attachment #1: message body text --]
[-- Type: text/plain, Size: 1538 bytes --]

   In the emacs-28 branch at 0dd3883def:

   Imagine my surprise when evaluating

	(days-between "2021-10-22" "2020-09-29")

returned zero.  The root cause is that passing any date string without a
time to date-to-time produces the same return value:

	(date-to-time "2021-10-22") => (14445 17280)
	(date-to-time "2020-09-29") => (14445 17280)

But:

	(date-to-time "2020-09-29 23:15") => (24435 63540)

There are really two bugs here (or maybe three, depending on how you
look at it):

   1.  If parsing throws an error that is not an overflow, it passes the
date through timezone-make-date-arpa-standard to try to fix some cases
that parse-time-string can't handle.  But the condition-case is also
wrapped around the encode-time call, which gets a wrong-type-argument
error when it sees nil time values for HH:MM, so the fallback gets used
for something other than a parsing error.

   2.  When timezone-make-date-arpa-standard gets something it can't
handle, it "canonicalizes" the value to "31 Dec 1999 19:00:00 -0500",
which is the source of the constant result.  That may be worth another
bug report, but I'm not sure of its charter; maybe that's correct
behavior in context.

   The attached patch adds decoded-time-set-defaults, moves that and the
encode-time call outside the condition-case, and disables the fallback
to timezone-make-date-arpa-standard if the date appears not to have a
time value.  And I can now tell you there are 388 days between
2020-09-29 and 2021-10-22.

					-- Bob Rogers
					   http://www.rgrjr.com/


[-- Attachment #2: Type: text/x-patch, Size: 1854 bytes --]

diff --git a/lisp/calendar/time-date.el b/lisp/calendar/time-date.el
index 155c34927f..6407138953 100644
--- a/lisp/calendar/time-date.el
+++ b/lisp/calendar/time-date.el
@@ -153,19 +153,25 @@ date-to-time
   "Parse a string DATE that represents a date-time and return a time value.
 DATE should be in one of the forms recognized by `parse-time-string'.
 If DATE lacks timezone information, GMT is assumed."
-  (condition-case err
-      (encode-time (parse-time-string date))
-    (error
-     (let ((overflow-error '(error "Specified time is not representable")))
-       (if (equal err overflow-error)
-	   (signal (car err) (cdr err))
-	 (condition-case err
-	     (encode-time (parse-time-string
-			   (timezone-make-date-arpa-standard date)))
-	   (error
-	    (if (equal err overflow-error)
-		(signal (car err) (cdr err))
-	      (error "Invalid date: %s" date)))))))))
+  ;; Pass the result of parsing through decoded-time-set-defaults
+  ;; because encode-time signals if HH:MM:SS are not filled in.
+  (encode-time
+    (decoded-time-set-defaults
+      (condition-case err
+          (parse-time-string date)
+        (error
+         (let ((overflow-error '(error "Specified time is not representable")))
+           (if (or (equal err overflow-error)
+                   ;; timezone-make-date-arpa-standard misbehaves if
+                   ;; not given at least HH:MM as part of the date.
+                   (not (string-match ":" date)))
+               (signal (car err) (cdr err))
+             (condition-case err
+                 (parse-time-string (timezone-make-date-arpa-standard date))
+               (error
+                (if (equal err overflow-error)
+                    (signal (car err) (cdr err))
+                  (error "Invalid date: %s" date)))))))))))
 
 ;;;###autoload
 (defalias 'time-to-seconds 'float-time)

^ permalink raw reply related	[flat|nested] 40+ messages in thread

end of thread, other threads:[~2022-02-25 12:03 UTC | newest]

Thread overview: 40+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-11-30 20:55 bug#52209: 28.0.60; [PATCH] date-to-time fails on pure dates Bob Rogers
2021-12-01  4:17 ` Lars Ingebrigtsen
2021-12-03  5:19   ` Katsumi Yamaoka
2021-12-03 16:29     ` Lars Ingebrigtsen
2021-12-03 18:38     ` Michael Heerdegen
2021-12-04 18:58 ` Paul Eggert
2021-12-19 21:11   ` Bob Rogers
2021-12-20 10:08     ` Lars Ingebrigtsen
2021-12-20 15:57       ` Bob Rogers
2021-12-20 16:34         ` Bob Rogers
2021-12-21 11:01         ` Lars Ingebrigtsen
2021-12-23 19:48           ` Bob Rogers
2021-12-24  9:29             ` Lars Ingebrigtsen
2021-12-24 15:58               ` Bob Rogers
2021-12-25 11:58                 ` Lars Ingebrigtsen
2021-12-25 22:50                   ` Bob Rogers
2021-12-26 11:31                     ` Lars Ingebrigtsen
2021-12-28 15:52                       ` Bob Rogers
2021-12-29 15:19                         ` Lars Ingebrigtsen
2021-12-29 19:29                           ` Paul Eggert
2021-12-29 22:01                             ` Bob Rogers
2021-12-30  5:32                               ` Bob Rogers
2021-12-30 21:08                           ` Bob Rogers
2022-01-01 14:47                             ` Lars Ingebrigtsen
2022-01-01 14:56                               ` Andreas Schwab
2022-01-02  0:41                                 ` Bob Rogers
2022-01-03 11:34                                   ` Lars Ingebrigtsen
2022-01-04  4:45                                     ` Bob Rogers
2022-01-05 15:46                                       ` Lars Ingebrigtsen
2022-01-05 22:49                                         ` Bob Rogers
     [not found]                                           ` <25105.33397.961104.269676@orion.rgrjr.com>
2022-02-20 12:25                                             ` Lars Ingebrigtsen
2022-02-20 13:03                                             ` Andreas Schwab
     [not found]                                               ` <87ilt9vicd.fsf@gnus.org>
2022-02-20 22:14                                                 ` Bob Rogers
2022-02-23 23:15                                                   ` Bob Rogers
2022-02-24  9:19                                                     ` Lars Ingebrigtsen
2022-02-25  0:49                                                       ` Bob Rogers
2022-02-25  2:16                                                         ` Lars Ingebrigtsen
2022-02-25  2:32                                                           ` Bob Rogers
2022-02-25  2:58                                                             ` Bob Rogers
2022-02-25 12:03                                                               ` Lars Ingebrigtsen

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).