From 4bfec9b09d18fd1f78c664de8f1a5b34bad989bf Mon Sep 17 00:00:00 2001 From: fitnr Date: Sun, 13 Sep 2026 14:37:10 -0400 Subject: [PATCH 1/4] Simplify bahai conversion code And add new tests --- src/convertdate/bahai.py | 13 +++++-------- tests/test_bahai.py | 14 +++++++++++++- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/convertdate/bahai.py b/src/convertdate/bahai.py index 453de24..d61d31f 100644 --- a/src/convertdate/bahai.py +++ b/src/convertdate/bahai.py @@ -8,9 +8,9 @@ """ The Bahá'í (Badí) calendar is a solar calendar with 19 months of 19 days. -Every four years, an intercalary period, Ayyam-i-Há, occurs between the 18th and 19th -months. Dates in this period are returned as month 19, and the month of ‘Alá is always -reported as month 20. +An intercalary period, Ayyam-i-Há, occurs between the 18th and 19th months, has +either four or five days. Dates in this period are returned as month 19, and +the month of ‘Alá is always reported as month 20. .. code-block:: python @@ -149,8 +149,7 @@ def to_jd(year, month, day): def from_jd(jd): '''Calculate Bahai date from Julian day''' jd = trunc(jd) + 0.5 - g = gregorian.from_jd(jd) - gy = g[0] + gy = gregorian.from_jd(jd)[0] n_month, n_day = gregorian_nawruz(gy) bstarty = EPOCH_GREGORIAN_YEAR @@ -160,9 +159,7 @@ def from_jd(jd): else: x = 0 # verify this next line... - bys = gy - (bstarty + (((gregorian.to_jd(gy, 1, 1) <= jd) and x))) - - year = bys + 1 + year = gy - (bstarty + x) + 1 days = jd - to_jd(year, 1, 1) bld = to_jd(year, n_day - 1, 1) diff --git a/tests/test_bahai.py b/tests/test_bahai.py index 736ac46..9678163 100644 --- a/tests/test_bahai.py +++ b/tests/test_bahai.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- """Test the Bahá’í calendar""" + import time import unittest @@ -9,8 +10,8 @@ class TestBahai(CalTestCase): - pairs = { + (2016, 2, 26): (172, bahai.AYYAMIHA, 1), (2041, 11, 27): (198, bahai.QAWL, 6), # ascension of Abdu'l-Bahá 2041 (2043, 11, 28): (200, bahai.QAWL, 6), # ascension of Abdu'l-Bahá 2043 (2038, 3, 1): (194, bahai.ALA, 1), # beginning of fast 2038 @@ -149,6 +150,17 @@ def test_from_gregorian(self): for g, b in self.pairs.items(): self.assertEqual(b, bahai.from_gregorian(*g)) + def test_near_ayyam_i_ha(self): + pairs = [ + ((2016, 2, 25), (172, 18, 19)), + ((2016, 2, 26), (172, 19, 1)), + ((2016, 2, 29), (172, 19, 4)), + ((2016, 3, 1), (172, 20, 1)) + ] + with self.subTest(): + for gdate, bdate in pairs: + self.assertEqual(bdate, bahai.from_gregorian(*gdate)) + def test_month_length(self): for x in range(1, 19): self.assertEqual(bahai.month_length(2019, x), 19) From c067057f61da3452092e4024fe1b0b9083c1c553 Mon Sep 17 00:00:00 2001 From: fitnr Date: Sun, 13 Sep 2026 15:02:42 -0400 Subject: [PATCH 2/4] Fix for Bahai mismatch --- src/convertdate/bahai.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/convertdate/bahai.py b/src/convertdate/bahai.py index d61d31f..494984a 100644 --- a/src/convertdate/bahai.py +++ b/src/convertdate/bahai.py @@ -161,7 +161,7 @@ def from_jd(jd): # verify this next line... year = gy - (bstarty + x) + 1 days = jd - to_jd(year, 1, 1) - bld = to_jd(year, n_day - 1, 1) + bld = to_jd(year, 20, 1) if jd >= bld: month = 20 From 615765316ecb7ab039910ea30ef07fdb7dee7e6d Mon Sep 17 00:00:00 2001 From: fitnr Date: Sun, 13 Sep 2026 15:05:20 -0400 Subject: [PATCH 3/4] Further simplify conversion --- src/convertdate/bahai.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/convertdate/bahai.py b/src/convertdate/bahai.py index 494984a..2341aec 100644 --- a/src/convertdate/bahai.py +++ b/src/convertdate/bahai.py @@ -152,18 +152,11 @@ def from_jd(jd): gy = gregorian.from_jd(jd)[0] n_month, n_day = gregorian_nawruz(gy) - bstarty = EPOCH_GREGORIAN_YEAR - - if jd <= gregorian.to_jd(gy, n_month, 20): - x = 1 - else: - x = 0 - # verify this next line... - year = gy - (bstarty + x) + 1 + x = 1 if jd <= gregorian.to_jd(gy, n_month, 20) else 0 + year = gy - (EPOCH_GREGORIAN_YEAR + x) + 1 days = jd - to_jd(year, 1, 1) - bld = to_jd(year, 20, 1) - if jd >= bld: + if jd >= to_jd(year, 20, 1): month = 20 else: month = trunc(days / 19) + 1 From f489c6aa909a0a9a7069c2cd10e1f207799d3eae Mon Sep 17 00:00:00 2001 From: fitnr Date: Sun, 13 Sep 2026 15:09:32 -0400 Subject: [PATCH 4/4] Add regression test for issue flagged by copilot --- tests/test_bahai.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_bahai.py b/tests/test_bahai.py index 9678163..34bf60f 100644 --- a/tests/test_bahai.py +++ b/tests/test_bahai.py @@ -12,6 +12,7 @@ class TestBahai(CalTestCase): pairs = { (2016, 2, 26): (172, bahai.AYYAMIHA, 1), + (2016, 3, 20): (173, bahai.BAHA, 1), (2041, 11, 27): (198, bahai.QAWL, 6), # ascension of Abdu'l-Bahá 2041 (2043, 11, 28): (200, bahai.QAWL, 6), # ascension of Abdu'l-Bahá 2043 (2038, 3, 1): (194, bahai.ALA, 1), # beginning of fast 2038