Skip to content

Commit

Permalink
Support two-digit years in non-Gregorian calendars (#1187)
Browse files Browse the repository at this point in the history
  • Loading branch information
sm-ghavami authored Nov 22, 2023
1 parent a4032c3 commit 3955530
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions dateparser/calendars/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ def to_latin(cls, source):

return result

def handle_two_digit_year(self, year):
raise ValueError

def _get_datetime_obj(self, **params):
day = params["day"]
year = params["year"]
Expand Down Expand Up @@ -129,6 +132,8 @@ def _get_date_obj(self, token, directive):
day = int(token)
elif directive == "%Y" and token_len == 4 and is_digit:
year = int(token)
elif directive == "%Y" and token_len == 2 and is_digit:
year = self.handle_two_digit_year(int(token))
else:
raise ValueError
return self.non_gregorian_date_cls(year, month, day)
Expand Down
6 changes: 6 additions & 0 deletions dateparser/calendars/hijri_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,9 @@ def _replace_time_conventions(cls, source):
for arabic in arabics:
result = result.replace(arabic, latin)
return result

def handle_two_digit_year(self, year):
if year >= 90:
return year + 1300
else:
return year + 1400
7 changes: 7 additions & 0 deletions dateparser/calendars/jalali_parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
from collections import OrderedDict
from datetime import datetime
from functools import reduce

from convertdate import persian
Expand Down Expand Up @@ -176,3 +177,9 @@ def comp_key(tup):
):
result = result.replace(persian_number, str(number))
return result

def handle_two_digit_year(self, year):
if year > 60:
return year + 1300
else:
return year + 1400
2 changes: 2 additions & 0 deletions tests/test_hijri.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ def then_parsed_datetime_is(self, dt):
dt_string="04-03-1433 هـ, 10:08 مساءً",
dt_obj=datetime(2012, 1, 27, 22, 8),
),
param(dt_string="30-02-33", dt_obj=datetime(2012, 1, 24)),
param(dt_string="10-03-90", dt_obj=datetime(1970, 5, 16)),
]
)
def test_datetime_parsing(
Expand Down
8 changes: 8 additions & 0 deletions tests/test_jalali.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,14 @@ def then_date_parsed_is(self, datetime):
date_string="پنجشنبه 26 شهریور 1394 ساعت ساعت 11 و 01 دقیقه",
dt=datetime(2015, 9, 17, 11, 1),
),
param(
date_string="پنجشنبه 26 شهریور 94 ساعت ساعت 11 و 01 دقیقه",
dt=datetime(2015, 9, 17, 11, 1),
),
param(
date_string="چهارشنبه 22 شهریور 02 ساعت ساعت 11 و 01 دقیقه",
dt=datetime(2023, 9, 13, 11, 1),
),
]
)
def test_get_date(self, date_string, dt):
Expand Down

0 comments on commit 3955530

Please sign in to comment.