Subject: bug#43677: 27.1; Calc holidays off-by-one error, incorrect business day calculation

From: Lars Ingebrigtsen <[email-masked]>
Date: Wed, 30 Sep 2020 04:50:25 +0200

Aaron Zeng <[email-masked]> writes:

> I observe the following incorrect behavior starting from `emacs -Q`:
>
> 1. M-x calc
> 2. ' <2020-11-01> RET --- displays "<Sun Nov 1, 2020>"
> 3. 1 t - --- should have subtracted one business day, but it displays
> "<Sat Oct 31, 2020>" instead of "<Fri Oct 30, 2020>"
>
> Adding a business day to Saturday gives Tuesday instead of Monday.  It
> seems like this is would be an off-by-one error.  This appears to be
> confirmed by setting the Holidays vector in calc (`s e Holidays`) to
> be `[fri, sat]` and trying the above procedure again.
>
> After applying the workaround, instead of seeing Saturday and Tuesday
> as the previous and next business days, respectively, I see Friday and
> Monday as I would expect.

Indeed -- applying the following patch (which numbers Monday as 0 and
Sunday as 6) makes the test case work correctly for me.  But trying to
follow the code makes it unclear...  er...  what's going on, and there
aren't many comments in the Calc code to explain what they were
thinking.

I wondered whether this might be a US/non-US thing (with a difference in
what day starts the week), but I couldn't really find anything in calc
talking about that...

Does anybody have any insight here?

diff --git a/lisp/calc/calc-forms.el b/lisp/calc/calc-forms.el
index 5a8f0a38d2..5d3629ea0a 100644
--- a/lisp/calc/calc-forms.el
+++ b/lisp/calc/calc-forms.el
@@ -1928,8 +1928,8 @@ math-sh-year
 (defun math-setup-holidays (&optional date)
   (or (eq (calc-var-value 'var-Holidays) math-holidays-cache-tag)
       (let ((h (calc-var-value 'var-Holidays))
-	    (wdnames '( (sun . 0) (mon . 1) (tue . 2) (wed . 3)
-			(thu . 4) (fri . 5) (sat . 6) ))
+	    (wdnames '( (mon . 0) (tue . 1) (wed . 2)
+			(thu . 3) (fri . 4) (sat . 5) (sun . 6) ))
 	    (days nil) (weekdays nil) (exprs nil) (limit nil) (hours nil))
 	(or (math-vectorp h)
 	    (math-reject-arg h "*Holidays variable must be a vector"))

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