diff --git a/src/convertdate/bahai.py b/src/convertdate/bahai.py index 453de24..2341aec 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,24 +149,14 @@ 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 - - if jd <= gregorian.to_jd(gy, n_month, 20): - x = 1 - else: - x = 0 - # verify this next line... - bys = gy - (bstarty + (((gregorian.to_jd(gy, 1, 1) <= jd) and x))) - - year = bys + 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, n_day - 1, 1) - if jd >= bld: + if jd >= to_jd(year, 20, 1): month = 20 else: month = trunc(days / 19) + 1 diff --git a/tests/test_bahai.py b/tests/test_bahai.py index 736ac46..34bf60f 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,9 @@ 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 @@ -149,6 +151,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)