Skip to content

Commit

Permalink
beautify
Browse files Browse the repository at this point in the history
  • Loading branch information
majiidd committed Mar 24, 2024
1 parent 3803ffc commit 9a31661
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 31 deletions.
72 changes: 42 additions & 30 deletions persiantools/jdatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,69 +228,81 @@ def to_jalali(cls, year, month=None, day=None):
day = year.day
year = year.year

g_d_m = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
# Days in each month of the Gregorian calendar
gregorian_days_in_month = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]

jy = 0 if year <= 1600 else 979
# Determine the Jalali year
jalali_year = 0 if year <= 1600 else 979
year -= 621 if year <= 1600 else 1600
year2 = year + 1 if month > 2 else year
days = (365 * year) + int((year2 + 3) / 4) - int((year2 + 99) / 100)
days += int((year2 + 399) / 400) - 80 + day + g_d_m[month - 1]
jy += 33 * int(days / 12053)

# Determine if the year is a leap year
leap_year = year + 1 if month > 2 else year

# Calculate the number of days
days = (365 * year) + (leap_year + 3) // 4 - (leap_year + 99) // 100
days += (leap_year + 399) // 400 - 80 + day + gregorian_days_in_month[month - 1]

# Update the Jalali year
jalali_year += 33 * (days // 12053)
days %= 12053
jy += 4 * int(days / 1461)
jalali_year += 4 * (days // 1461)
days %= 1461
jy += int((days - 1) / 365)
jalali_year += (days - 1) // 365

if days > 365:
days = (days - 1) % 365

# Determine the Jalali month and day
if days < 186:
jm = 1 + int(days / 31)
jd = 1 + (days % 31)
jalali_month = 1 + days // 31
jalali_day = 1 + (days % 31)
else:
arit = days - 186
jm = 7 + int(arit / 30)
jd = 1 + (arit % 30)
days -= 186
jalali_month = 7 + days // 30
jalali_day = 1 + (days % 30)

return cls(jy, jm, jd)
return cls(jalali_year, jalali_month, jalali_day)

def to_gregorian(self):
"""based on jdf.scr.ir"""
month = self.month
day = self.day
year = self.year

gy = 621 if year <= 979 else 1600
# Determine the Gregorian year
gregorian_year = 621 if year <= 979 else 1600
year -= 0 if year <= 979 else 979

d = (month - 1) * 31 if month < 7 else ((month - 7) * 30) + 186
days = (365 * year) + (int(year / 33) * 8) + int(((year % 33) + 3) / 4)
days += 78 + day + d
# Calculate the number of days
days_in_month = (month - 1) * 31 if month < 7 else ((month - 7) * 30) + 186
days = (365 * year) + (year // 33) * 8 + ((year % 33) + 3) // 4
days += 78 + day + days_in_month

gy += 400 * int(days / 146097)
gregorian_year += 400 * (days // 146097)
days %= 146097

if days > 36524:
days -= 1
gy += 100 * int(days / 36524)
gregorian_year += 100 * (days // 36524)
days %= 36524

if days >= 365:
days += 1

gy += 4 * int(days / 1461)
gregorian_year += 4 * (days // 1461)
days %= 1461
gy += int((days - 1) / 365)
gregorian_year += (days - 1) // 365

if days > 365:
days = (days - 1) % 365

gd = days + 1
gregorian_day = days + 1

g_d_m = [
# Days in each month of the Gregorian calendar
gregorian_days_in_month = [
0,
31,
29 if (gy % 4 == 0 and gy % 100 != 0) or gy % 400 == 0 else 28,
29 if (gregorian_year % 4 == 0 and gregorian_year % 100 != 0) or gregorian_year % 400 == 0 else 28,
31,
30,
31,
Expand All @@ -303,13 +315,13 @@ def to_gregorian(self):
31,
]

_gm = 0
for _gm, g in enumerate(g_d_m):
if gd <= g:
# Determine the Gregorian month
for _gregorian_month, g in enumerate(gregorian_days_in_month):
if gregorian_day <= g:
break
gd -= g
gregorian_day -= g

return date(gy, _gm, gd)
return date(gregorian_year, _gregorian_month, gregorian_day)

@classmethod
def today(cls):
Expand Down
7 changes: 6 additions & 1 deletion tests/test_jalalidate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
class TestJalaliDate(TestCase):
def test_shamsi_to_gregorian(self):
self.assertEqual(JalaliDate(1367, 2, 14).to_gregorian(), date(1988, 5, 4))
self.assertEqual(JalaliDate(1369, 7, 1).to_gregorian(), date(1990, 9, 23))
self.assertEqual(JalaliDate(1395, 3, 21).to_gregorian(), date(2016, 6, 10))
self.assertEqual(JalaliDate(1395, 12, 9).to_gregorian(), date(2017, 2, 27))
self.assertEqual(JalaliDate(1395, 12, 30).to_gregorian(), date(2017, 3, 20))
Expand All @@ -23,6 +22,7 @@ def test_shamsi_to_gregorian(self):
self.assertEqual(JalaliDate(1399, 11, 23).to_gregorian(), date(2021, 2, 11))
self.assertEqual(JalaliDate(1400, 4, 25).to_gregorian(), date(2021, 7, 16))
self.assertEqual(JalaliDate(1400, 12, 20).to_gregorian(), date(2022, 3, 11))
self.assertEqual(JalaliDate(1403, 1, 5).to_gregorian(), date(2024, 3, 24))

self.assertEqual(JalaliDate.today().to_gregorian(), date.today())

Expand All @@ -39,6 +39,7 @@ def test_gregorian_to_shamsi(self):
self.assertEqual(JalaliDate.to_jalali(2018, 3, 20), JalaliDate(1396, 12, 29))
self.assertEqual(JalaliDate.to_jalali(2021, 2, 11), JalaliDate(1399, 11, 23))
self.assertEqual(JalaliDate.to_jalali(2021, 7, 16), JalaliDate(1400, 4, 25))
self.assertEqual(JalaliDate.to_jalali(2024, 3, 24), JalaliDate(1403, 1, 5))

self.assertEqual(JalaliDate(date.today()), JalaliDate.today())

Expand All @@ -57,6 +58,7 @@ def test_checkdate(self):
self.assertEqual(JalaliDate.check_date(1399, 11, 31), False)
self.assertEqual(JalaliDate.check_date(1400, 4, 25), True)
self.assertEqual(JalaliDate.check_date(1400, 12, 30), False)
self.assertEqual(JalaliDate.check_date(1403, 12, 30), True)

def test_completeday(self):
jdate = JalaliDate(1398, 3, 17)
Expand Down Expand Up @@ -97,6 +99,7 @@ def test_isocalendar(self):
self.assertEqual(JalaliDate(1398, 3, 17).isocalendar(), (1398, 12, 7))
self.assertEqual(JalaliDate(1398, 1, 1).isocalendar(), (1398, 1, 6))
self.assertEqual(JalaliDate(1399, 1, 2).isocalendar(), (1399, 2, 1))
self.assertEqual(JalaliDate(1403, 1, 5).isocalendar(), (1403, 2, 2))

def test_additions(self):
self.assertEqual(JalaliDate(JalaliDate(1395, 3, 21)), JalaliDate(1395, 3, 21))
Expand Down Expand Up @@ -224,6 +227,7 @@ def test_week(self):
self.assertEqual(JalaliDate(1396, 7, 8).week_of_year(), 29)
self.assertEqual(JalaliDate(1397, 11, 29).week_of_year(), 49)
self.assertEqual(JalaliDate(1399, 1, 2).week_of_year(), 2)
self.assertEqual(JalaliDate(1403, 1, 5).week_of_year(), 2)

self.assertEqual(JalaliDate(1367, 2, 14).weekday(), 4)
self.assertEqual(JalaliDate(1393, 1, 1).weekday(), 6)
Expand Down Expand Up @@ -266,6 +270,7 @@ def test_operators(self):
self.assertEqual(JalaliDate(1397, 12, 1) - JalaliDate(1397, 11, 29), timedelta(hours=48))
self.assertEqual(JalaliDate(1395, 3, 21) - date(2016, 5, 3), timedelta(days=38))
self.assertEqual(JalaliDate(1395, 12, 30) - JalaliDate(1395, 1, 1), timedelta(days=365))
self.assertEqual(JalaliDate(1403, 1, 1) - JalaliDate(1402, 12, 29), timedelta(days=1))

self.assertFalse(JalaliDate(1367, 2, 14) == (1367, 2, 14))
self.assertFalse(JalaliDate(1367, 2, 14) == "")
Expand Down

0 comments on commit 9a31661

Please sign in to comment.