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

Fix hijri parser #1174

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
57 changes: 57 additions & 0 deletions dateparser/calendars/hijri_parser.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from collections import OrderedDict
from functools import reduce
from hijri_converter import convert

from dateparser.calendars import non_gregorian_parser
Expand Down Expand Up @@ -47,6 +49,61 @@ class hijri_parser(non_gregorian_parser):
"pm": ["مساءً"],
}

_digits = {
"۰": 0,
"۱": 1,
"۲": 2,
"۳": 3,
"۴": 4,
"۵": 5,
"۶": 6,
"۷": 7,
"۸": 8,
"۹": 9,
}

_months = OrderedDict(
[
("01", ["مُحرم", "محرم"]),
("02", ["صفر"]),
("03", ["ربيع الأول", "ربيع الاول"]),
("04", ["ربيع الثاني", "ربيع الاخر`"]),
("05", ["جمادي الأول", "جمادي الاول"]),
("06", ["جمادي الثاني", "جمادي الاخر"]),
("07", ["رجب"]),
("08", ["شعبان"]),
("09", ["رمضان"]),
("10", ["شوال"]),
("11", ["ذو القعدة"]),
("12", ["ذو الحجة"]),
]
)


@classmethod
def _replace_digits(cls, source):
result = source
for pers_digit, number in cls._digits.items():
result = result.replace(pers_digit, str(number))
return result


@classmethod
def _replace_months(cls, source):
print("Source is ", source)
result = source
for arabic, number in reduce(
lambda a, b: a + b,
[
[(value, month) for value in rpl]
for month, rpl in cls._months.items()
],
):
print("arabic", arabic, " latin", number)
result = result.replace(arabic, number)
return result


@classmethod
def _replace_time_conventions(cls, source):
result = source
Expand Down
3 changes: 1 addition & 2 deletions dateparser/data/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
from dateparser.data import date_translation_data

from .languages_info import language_locale_dict, language_order
from .languages_info import language_order, language_locale_dict
8 changes: 8 additions & 0 deletions tests/test_hijri.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ 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="06 ربيع الأول 1433 هـ, 16:32 مساءً",
dt_obj=datetime(2012, 1, 29, 16, 32),
),
param(
dt_string="13 شعبان 1441 هـ, 03:۱۲ صباحاً",
dt_obj=datetime(2020, 4, 6, 3, 12),
),
]
)
def test_datetime_parsing(
Expand Down