bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#40217: 28.0.50; Add check for valid month to date-days-in-month


From: Alex Branham
Subject: bug#40217: 28.0.50; Add check for valid month to date-days-in-month
Date: Tue, 24 Mar 2020 19:39:44 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Hello -

Here's a patch that adds a check for whether the month argument is
valid in the new date-days-in-month function. It currently returns 30
for a month value of, say, 15.

Thanks,
Alex

>From f9c3040d516314209d64738fd23deafc5eb59faa Mon Sep 17 00:00:00 2001
From: Alex Branham <alex.branham@gmail.com>
Date: Tue, 24 Mar 2020 19:34:14 -0400
Subject: [PATCH] Add check for valid month to date-days-in-month

* lisp/calendar/time-date.el (date-days-in-month): Add check.
* test/lisp/calendar/time-date-tests.el (test-days-in-month): Add test
for new error.
---
 lisp/calendar/time-date.el            | 2 ++
 test/lisp/calendar/time-date-tests.el | 3 ++-
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/lisp/calendar/time-date.el b/lisp/calendar/time-date.el
index e2402de801..9b58a4884b 100644
--- a/lisp/calendar/time-date.el
+++ b/lisp/calendar/time-date.el
@@ -355,6 +355,8 @@ seconds-to-string
 
 (defun date-days-in-month (year month)
   "The number of days in MONTH in YEAR."
+  (unless (<= 1 month 12)
+    (error "Month %s invalid" month))
   (if (= month 2)
       (if (date-leap-year-p year)
           29
diff --git a/test/lisp/calendar/time-date-tests.el 
b/test/lisp/calendar/time-date-tests.el
index 4c8f18a7a9..9c90300cfe 100644
--- a/test/lisp/calendar/time-date-tests.el
+++ b/test/lisp/calendar/time-date-tests.el
@@ -31,7 +31,8 @@ test-leap-year
 (ert-deftest test-days-in-month ()
   (should (= (date-days-in-month 2004 2) 29))
   (should (= (date-days-in-month 2004 3) 31))
-  (should-not (= (date-days-in-month 1900 3) 28)))
+  (should-not (= (date-days-in-month 1900 3) 28))
+  (should-error (date-days-in-month 2020 15)))
 
 (ert-deftest test-ordinal ()
   (should (equal (date-ordinal-to-time 2008 271)
-- 
2.25.1


reply via email to

[Prev in Thread] Current Thread [Next in Thread]