Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 7 additions & 17 deletions src/convertdate/bahai.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment thread
fitnr marked this conversation as resolved.
the month of ‘Alá is always reported as month 20.

.. code-block:: python

Expand Down Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion tests/test_bahai.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
"""Test the Bahá’í calendar"""

import time
import unittest

Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Loading