Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support two-digit year in non-gregorian calendars #1187

Merged
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
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