unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#43759: Emacs Calc date conversion
@ 2020-10-02 10:53 Vincent Belaïche
  2020-10-02 14:22 ` Lars Ingebrigtsen
  0 siblings, 1 reply; 5+ messages in thread
From: Vincent Belaïche @ 2020-10-02 10:53 UTC (permalink / raw)
  To: 43759

[-- Attachment #1: Type: text/plain, Size: 488 bytes --]

My Emacs version is GNU Emacs 28.0.50 (build 1, x86_64-apple-darwin19.6.0, NS appkit-1894.60 Version 10.15.6 (Build 19G73)) of 2020-07-29

I do the following expériment under Emacs CC

;; Set date format as Unix
dd U RET
;; Enter 1991-01-09T06:00 in Unix format, cf. date -d '1991-01-09T06:00+00' '+%s'
' <663400800> RET
;; Set format as ISO
dd YYYY-MM-DD HH:mm:SS RET

Then the display is <1991-01-10 06:00>, but I expected the 9th and not the 10th of January.

  Vincent.

[-- Attachment #2: Type: text/html, Size: 2287 bytes --]

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

* bug#43759: Emacs Calc date conversion
  2020-10-02 10:53 bug#43759: Emacs Calc date conversion Vincent Belaïche
@ 2020-10-02 14:22 ` Lars Ingebrigtsen
  2020-10-02 17:31   ` Mattias Engdegård
  0 siblings, 1 reply; 5+ messages in thread
From: Lars Ingebrigtsen @ 2020-10-02 14:22 UTC (permalink / raw)
  To: Vincent Belaïche; +Cc: Mattias Engdegård, 43759

Vincent Belaïche <vincent.b.1@hotmail.fr> writes:

> I do the following expériment under Emacs CC
>
> ;; Set date format as Unix
> dd U RET
> ;; Enter 1991-01-09T06:00 in Unix format, cf. date -d '1991-01-09T06:00+00'
> '+%s'
> ' <663400800> RET
> ;; Set format as ISO
> dd YYYY-MM-DD HH:mm:SS RET
>
> Then the display is <1991-01-10 06:00>, but I expected the 9th and not
> the 10th of January.

I wondered whether this somehow had also been fixed by Mattias' recent
calc change to business days, but it hasn't -- I'm able to reproduce it
with the current trunk.

This is another off-by-one-day bug, though, so perhaps it's related to
the same change from a few years ago that changed day zero?

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





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

* bug#43759: Emacs Calc date conversion
  2020-10-02 14:22 ` Lars Ingebrigtsen
@ 2020-10-02 17:31   ` Mattias Engdegård
  2020-10-02 20:13     ` Vincent Belaïche
  0 siblings, 1 reply; 5+ messages in thread
From: Mattias Engdegård @ 2020-10-02 17:31 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: Vincent Belaïche, 43759

[-- Attachment #1: Type: text/plain, Size: 736 bytes --]

2 okt. 2020 kl. 16.22 skrev Lars Ingebrigtsen <larsi@gnus.org>:

> This is another off-by-one-day bug, though, so perhaps it's related to
> the same change from a few years ago that changed day zero?

Actually this error is unrelated. It has been there since Calc was added to the Emacs tree in 2001: the number of days from the Calc epoch to start of Unix time was incorrectly given as 719164 but the correct number was 719162 before the change in Calc epoch in 2012, and since then it should be 719163.

The "t U" command was fixed in 2015 (e368697ce36) along with the documentation. The attached patch fixes the remaining uses of the wrong constant, in the date parsing and formatting code. Vincent, is this patch helpful?


[-- Attachment #2: 0001-Calc-fix-formatting-and-parsing-Unix-time-bug-43759.patch --]
[-- Type: application/octet-stream, Size: 5208 bytes --]

From fb718a57123036e40cb8b3b813d734e590a104fa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mattias=20Engdeg=C3=A5rd?= <mattiase@acm.org>
Date: Fri, 2 Oct 2020 18:50:50 +0200
Subject: [PATCH] Calc: fix formatting and parsing Unix time (bug#43759)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The number of days from epoch to Jan 1, 1970 that was used in parsing
and formatting Unix time was incorrect.  The previous fix
(in e368697ce36) was incomplete.

Reported by Vincent Belaïche.

* lisp/calc/calc-forms.el (math-unix-epoch): New constant.
(math-format-date-part, math-parse-standard-date, calcFunc-unixtime):
Use math-unix-epoch instead of a constant that is sometimes wrong.
---
 lisp/calc/calc-forms.el      | 13 ++++++++----
 test/lisp/calc/calc-tests.el | 41 ++++++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+), 4 deletions(-)

diff --git a/lisp/calc/calc-forms.el b/lisp/calc/calc-forms.el
index 6d70126c09..a2f6696866 100644
--- a/lisp/calc/calc-forms.el
+++ b/lisp/calc/calc-forms.el
@@ -709,6 +709,10 @@ math-julian-date-beginning-int
   "The beginning of the Julian date calendar,
 as measured in the integer number of days before December 31, 1 BC (Gregorian).")
 
+(defconst math-unix-epoch 719163
+  "The beginning of Unix time: days from December 31, 1 BC (Gregorian)
+to Jan 1, 1970 AD.")
+
 (defun math-format-date-part (x)
   (cond ((stringp x)
 	 x)
@@ -730,7 +734,8 @@ math-format-date-part
                               (math-floor math-fd-date)
                               math-julian-date-beginning-int)))
 	((eq x 'U)
-	 (math-format-number (nth 1 (math-date-parts math-fd-date 719164))))
+	 (math-format-number (nth 1 (math-date-parts math-fd-date
+                                                     math-unix-epoch))))
         ((memq x '(IYYY Iww w))
          (progn
            (or math-fd-iso-dt
@@ -1173,7 +1178,7 @@ math-parse-standard-date
 		      (setq num (math-match-substring math-pd-str 0)
 			    math-pd-str (substring math-pd-str (match-end 0))
 			    num (math-date-to-dt
-				 (math-add 719164
+				 (math-add math-unix-epoch
 					   (math-div (math-read-number num)
 						     '(float 864 2))))
 			    hour (nth 3 num)
@@ -1434,11 +1439,11 @@ calcFunc-julian
 (defun calcFunc-unixtime (date &optional zone)
   (if (math-realp date)
       (progn
-	(setq date (math-add 719163 (math-div date '(float 864 2))))
+	(setq date (math-add math-unix-epoch (math-div date '(float 864 2))))
 	(list 'date (math-sub date (math-div (calcFunc-tzone zone date)
 					     '(float 864 2)))))
     (if (eq (car date) 'date)
-	(math-add (nth 1 (math-date-parts (nth 1 date) 719163))
+	(math-add (nth 1 (math-date-parts (nth 1 date) math-unix-epoch))
 		  (calcFunc-tzone zone date))
       (math-reject-arg date 'datep))))
 
diff --git a/test/lisp/calc/calc-tests.el b/test/lisp/calc/calc-tests.el
index 4dded007f7..6eb0788052 100644
--- a/test/lisp/calc/calc-tests.el
+++ b/test/lisp/calc/calc-tests.el
@@ -534,6 +534,47 @@ calc-business-days
       )
   ))
 
+(ert-deftest calc-unix-date ()
+  (cl-flet ((m (s) (math-parse-date s)))
+    ;; calcFunc-unixtime (command "t U") converts a date value to Unix time,
+    ;; and a number to a date.
+    (should (equal (m "1970-01-01") '(date 719163)))
+    (should (equal (calcFunc-unixtime (m "1970-01-01") 0) 0))
+    (should (equal (calc-tests--calc-to-number (cadr (calcFunc-unixtime 0 0)))
+                   (cadr (m "1970-01-01"))))
+    (should (equal (calcFunc-unixtime (m "2020-09-07") 0)
+                   (* (- (cadr (m "2020-09-07"))
+                         (cadr (m "1970-01-01")))
+                      86400)))
+    (should (equal (calcFunc-unixtime (m "1991-01-09 06:00") 0)
+                   663400800))
+    (should (equal (calc-tests--calc-to-number
+                    (cadr (calcFunc-unixtime 663400800 0)))
+                   726841.25))
+
+    (let* ((d-1970-01-01 (m "1970-01-01"))
+           (d-2020-09-07 (m "2020-09-07"))
+           (d-1991-01-09-0600 (m "1991-01-09 06:00"))
+           (calc-date-format '(U)))
+      ;; Test parsing Unix time.
+      (should (equal (calc-tests--calc-to-number
+                      (cadr (math-parse-date "0")))
+                     719163))
+      (should (equal (calc-tests--calc-to-number
+                      (cadr (math-parse-date "469324800")))
+                     (+ 719163 (/ 469324800 86400))))
+      (should (equal (calc-tests--calc-to-number
+                      (cadr (math-parse-date "663400800")))
+                     726841.25))
+
+      ;; Test formatting Unix time.
+      (should (equal (math-format-date d-1970-01-01) "0"))
+      (should (equal (math-format-date d-2020-09-07)
+                     (number-to-string (* (- (cadr d-2020-09-07)
+                                             (cadr d-1970-01-01))
+                                          86400))))
+      (should (equal (math-format-date d-1991-01-09-0600) "663400800")))))
+
 (provide 'calc-tests)
 ;;; calc-tests.el ends here
 
-- 
2.21.1 (Apple Git-122.3)


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

* bug#43759: Emacs Calc date conversion
  2020-10-02 17:31   ` Mattias Engdegård
@ 2020-10-02 20:13     ` Vincent Belaïche
  2020-10-02 20:27       ` Mattias Engdegård
  0 siblings, 1 reply; 5+ messages in thread
From: Vincent Belaïche @ 2020-10-02 20:13 UTC (permalink / raw)
  To: Mattias Engdegård, Lars Ingebrigtsen; +Cc: 43759@debbugs.gnu.org

[-- Attachment #1: Type: text/plain, Size: 1122 bytes --]

Definitely, I applied the patch manually, and it fixed the issue.

VBR,
  V.
________________________________
De : Mattias Engdegård <mattiase@acm.org>
Envoyé : vendredi 2 octobre 2020 19:31
À : Lars Ingebrigtsen <larsi@gnus.org>
Cc : Vincent Belaïche <vincent.b.1@hotmail.fr>; 43759@debbugs.gnu.org <43759@debbugs.gnu.org>
Objet : Re: bug#43759: Emacs Calc date conversion

2 okt. 2020 kl. 16.22 skrev Lars Ingebrigtsen <larsi@gnus.org>:

> This is another off-by-one-day bug, though, so perhaps it's related to
> the same change from a few years ago that changed day zero?

Actually this error is unrelated. It has been there since Calc was added to the Emacs tree in 2001: the number of days from the Calc epoch to start of Unix time was incorrectly given as 719164 but the correct number was 719162 before the change in Calc epoch in 2012, and since then it should be 719163.

The "t U" command was fixed in 2015 (e368697ce36) along with the documentation. The attached patch fixes the remaining uses of the wrong constant, in the date parsing and formatting code. Vincent, is this patch helpful?


[-- Attachment #2: Type: text/html, Size: 2250 bytes --]

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

* bug#43759: Emacs Calc date conversion
  2020-10-02 20:13     ` Vincent Belaïche
@ 2020-10-02 20:27       ` Mattias Engdegård
  0 siblings, 0 replies; 5+ messages in thread
From: Mattias Engdegård @ 2020-10-02 20:27 UTC (permalink / raw)
  To: Vincent Belaïche; +Cc: 43759-done, Lars Ingebrigtsen

2 okt. 2020 kl. 22.13 skrev Vincent Belaïche <vincent.b.1@hotmail.fr>:

> Definitely, I applied the patch manually, and it fixed the issue.

Thank you for the prompt confirmation (and the original report, of course)!
Pushed to master; closing bug.






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

end of thread, other threads:[~2020-10-02 20:27 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-10-02 10:53 bug#43759: Emacs Calc date conversion Vincent Belaïche
2020-10-02 14:22 ` Lars Ingebrigtsen
2020-10-02 17:31   ` Mattias Engdegård
2020-10-02 20:13     ` Vincent Belaïche
2020-10-02 20:27       ` Mattias Engdegård

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).