Skip to content

Commit b0f56f7

Browse files
authored
Merge pull request #123 from dh-tech/feature/rename-hijri
Use `islamic` for Islamic/Hijri calendar classes #120
2 parents 7e3dd84 + f442724 commit b0f56f7

17 files changed

Lines changed: 120 additions & 120 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ Currently available converters are "ISO8601" and "EDTF" and supported calendars.
158158

159159
### Calendars
160160

161-
All `Undate` objects are calendar aware, and date converters include support for parsing and working with dates from other calendars. The Gregorian calendar is used by default; currently `undate` supports the Hijri Islamic calendar and the Anno Mundi Hebrew calendar based on calendar convertion logic implemented in the [convertdate](https://convertdate.readthedocs.io/en/latest/)package.
161+
All `Undate` objects are calendar aware, and date converters include support for parsing and working with dates from other calendars. The Gregorian calendar is used by default; currently `undate` supports the Islamic Hijri calendar and the Hebrew Anno Mundi calendar based on calendar conversion logic implemented in the [convertdate](https://convertdate.readthedocs.io/en/latest/) package.
162162

163163
Dates are stored with the year, month, day and appropriate precision for the original calendar; internally, earliest and latest dates are calculated in Gregorian / Proleptic Gregorian calendar for standardized comparison across dates from different calendars.
164164

docs/undate/converters.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ Gregorian
3939
.. automodule:: undate.converters.calendars.gregorian
4040
:members:
4141

42-
Hijri (Islamic calendar)
43-
^^^^^^^^^^^^^^^^^^^^^^^^
42+
Hebrew Anno Mundi calendar
43+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4444

45-
.. automodule:: undate.converters.calendars.hijri.converter
45+
.. automodule:: undate.converters.calendars.hebrew.converter
4646
:members:
4747

48-
Anno Mundi (Hebrew calendar)
49-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
48+
Islamic Hijri calendar
49+
^^^^^^^^^^^^^^^^^^^^^^^^
5050

51-
.. automodule:: undate.converters.calendars.hebrew.converter
51+
.. automodule:: undate.converters.calendars.islamic.converter
5252
:members:
5353

src/undate/converters/base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
implementing date converters, which can provide support for
44
parsing and generating dates in different formats.
55
The converter subclass :class:`undate.converters.BaseCalendarConverter`
6-
provides additional functionaly needed for calendar conversion.
6+
provides additional functionality needed for calendar conversion.
77
88
To add support for a new date converter:
99
@@ -23,10 +23,10 @@
2323
2424
- Create a new file under ``undate/converters/calendars/``
2525
- For converters with sufficient complexity, you may want to create a submodule;
26-
see ``undate.converters.calendars.hijri`` for an example.
26+
see ``undate.converters.calendars.islamic`` for an example.
2727
- Extend ``BaseCalendarConverter`` and implement ``parse`` and ``to_string``
2828
formatter methods as desired/appropriate for your converter as well as the
29-
additional methods for ``max_month``, ``max_day``, and convertion ``to_gregorian``
29+
additional methods for ``max_month``, ``max_day``, and conversion ``to_gregorian``
3030
calendar.
3131
- Import your calendar in ``undate/converters/calendars/__init__.py`` and include in `__all__``
3232
- Add unit tests for the new calendar logic under ``tests/test_converters/calendars/``
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from undate.converters.calendars.gregorian import GregorianDateConverter
2-
from undate.converters.calendars.hijri import HijriDateConverter
32
from undate.converters.calendars.hebrew import HebrewDateConverter
3+
from undate.converters.calendars.islamic import IslamicDateConverter
44

5-
__all__ = ["HijriDateConverter", "GregorianDateConverter", "HebrewDateConverter"]
5+
__all__ = ["GregorianDateConverter", "HebrewDateConverter", "IslamicDateConverter"]

src/undate/converters/calendars/hijri/__init__.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/undate/converters/calendars/hijri/parser.py

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from undate.converters.calendars.islamic.converter import IslamicDateConverter
2+
3+
__all__ = ["IslamicDateConverter"]

src/undate/converters/calendars/hijri/converter.py renamed to src/undate/converters/calendars/islamic/converter.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@
55

66
from undate import Undate, UndateInterval
77
from undate.converters.base import BaseCalendarConverter
8-
from undate.converters.calendars.hijri.parser import hijri_parser
9-
from undate.converters.calendars.hijri.transformer import HijriDateTransformer
8+
from undate.converters.calendars.islamic.parser import islamic_parser
9+
from undate.converters.calendars.islamic.transformer import IslamicDateTransformer
1010

1111

12-
class HijriDateConverter(BaseCalendarConverter):
12+
class IslamicDateConverter(BaseCalendarConverter):
1313
"""
14-
Converter for Hijri / Islamic calendar.
14+
Converter for Islamic Hijri calendar.
1515
16-
Support for parsing Hijri dates and converting to Undate and UndateInterval
16+
Support for parsing Islamic Hijri dates and converting to Undate and UndateInterval
1717
objects in the Gregorian calendar.
1818
"""
1919

20-
#: converter name: Hijri
21-
name: str = "Hijri"
22-
calendar_name: str = "Hijrī"
20+
#: converter name: Islamic
21+
name: str = "Islamic"
22+
calendar_name: str = "Islamic"
2323

2424
def __init__(self):
25-
self.transformer = HijriDateTransformer()
25+
self.transformer = IslamicDateTransformer()
2626

2727
def max_day(self, year: int, month: int) -> int:
2828
"""maximum numeric day for the specified year and month in this calendar"""
@@ -44,24 +44,24 @@ def to_gregorian(self, year: int, month: int, day: int) -> tuple[int, int, int]:
4444

4545
def parse(self, value: str) -> Union[Undate, UndateInterval]:
4646
"""
47-
Parse a Hijri date string and return an :class:`~undate.undate.Undate` or
47+
Parse an Islamic/Hijri date string and return an :class:`~undate.undate.Undate` or
4848
:class:`~undate.undate.UndateInterval`.
49-
The Hijri date string is preserved in the undate label.
49+
The Islamic/Hijri date string is preserved in the undate label.
5050
"""
5151
if not value:
5252
raise ValueError("Parsing empty string is not supported")
5353

5454
# parse the input string, then transform to undate object
5555
try:
56-
# parse the string with our Hijri date parser
57-
parsetree = hijri_parser.parse(value)
56+
# parse the string with our Islamic Hijri date parser
57+
parsetree = islamic_parser.parse(value)
5858
# transform the parse tree into an undate or undate interval
5959
undate_obj = self.transformer.transform(parsetree)
6060
# set the original date as a label, with the calendar name
6161
undate_obj.label = f"{value} {self.calendar_name}"
6262
return undate_obj
6363
except UnexpectedCharacters as err:
64-
raise ValueError(f"Could not parse '{value}' as a Hijri date") from err
64+
raise ValueError(f"Could not parse '{value}' as an Islamic date") from err
6565

6666
# do we need to support conversion the other direction?
67-
# i.e., generate a Hijri date from an abitrary undate or undate interval?
67+
# i.e., generate an Islamic Hijri date from an arbitrary undate or undate interval?

src/undate/converters/calendars/hijri/hijri.lark renamed to src/undate/converters/calendars/islamic/islamic.lark

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
// only support day month year format for now
55
// parser requires numeric day and year to be distinguished based on order
6-
hijri_date: day month year | month year | year
6+
islamic_date: day month year | month year | year
77

88
// TODO: handle date ranges?
99

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import pathlib
2+
3+
from lark import Lark
4+
5+
grammar_path = pathlib.Path(__file__).parent / "islamic.lark"
6+
7+
with open(grammar_path) as grammar:
8+
# NOTE: LALR parser is faster but can't be used due to ambiguity between years and days
9+
islamic_parser = Lark(grammar.read(), start="islamic_date", strict=True)

0 commit comments

Comments
 (0)