]> git.eshelyaron.com Git - emacs.git/commitdiff
Fix handling of delta values with negative month field
authorŁukasz Stelmach <stlman@poczta.fm>
Wed, 7 Feb 2024 13:37:39 +0000 (14:37 +0100)
committerEshel Yaron <me@eshelyaron.com>
Tue, 4 Mar 2025 20:56:27 +0000 (21:56 +0100)
* lisp/calendar/time-date.el (decoded-time-add): If the new
variable is less then zero, the year needs to be decremented
by quotient of new and 12 increased by one.
* test/lisp/calendar/time-date-tests.el (test-decoded-add):
Add applicable test cases.  (Bug#68969)

(cherry picked from commit bc33b70b280933470b252f2a877fd50e4aed42ba)

lisp/calendar/time-date.el
test/lisp/calendar/time-date-tests.el

index ce8c668c8cd0beb59ef1a7037c11a20c92990bcf..01f96305edb1b900f03e75ea125c178c463d9df0 100644 (file)
@@ -547,7 +547,7 @@ changes in daylight saving time are not taken into account."
     (when (decoded-time-month delta)
       (let ((new (+ (1- (decoded-time-month time)) (decoded-time-month delta))))
         (setf (decoded-time-month time) (1+ (mod new 12)))
-        (incf (decoded-time-year time) (/ new 12))))
+        (incf (decoded-time-year time) (- (/ new 12) (if (< new 0) 1 0)))))
 
     ;; Adjust for month length (as described in the doc string).
     (setf (decoded-time-day time)
index b8d3381528ea6039eed269cbafbef8145668634d..7df1e1b0da7384a5a7e0f988fbcf07951e904f2b 100644 (file)
     (should (equal (decoded-time-add time (mdec :month 10))
                    '(12 15 16 8 5 2020 1 t 7200)))
 
+    (should (equal (decoded-time-add time (mdec :month -1))
+                   '(12 15 16 8 6 2019 1 t 7200)))
+
+    (should (equal (decoded-time-add time (mdec :month -10))
+                   '(12 15 16 8 9 2018 1 t 7200)))
+
+    (should (equal (decoded-time-add time (mdec :month -14))
+                   '(12 15 16 8 5 2018 1 t 7200)))
+
+    (should (equal (decoded-time-add time (mdec :month -24))
+                   '(12 15 16 8 7 2017 1 t 7200)))
+
     (should (equal (decoded-time-add time (mdec :day 1))
                    '(12 15 16 9 7 2019 1 t 7200)))