arabic-datetime 0.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- arabic_datetime-0.0.0/LICENSE +21 -0
- arabic_datetime-0.0.0/PKG-INFO +5 -0
- arabic_datetime-0.0.0/README.md +237 -0
- arabic_datetime-0.0.0/arabic_datetime/__init__.py +2 -0
- arabic_datetime-0.0.0/arabic_datetime/arabic_date.py +139 -0
- arabic_datetime-0.0.0/arabic_datetime/arabic_time.py +28 -0
- arabic_datetime-0.0.0/arabic_datetime/constants.py +183 -0
- arabic_datetime-0.0.0/arabic_datetime.egg-info/PKG-INFO +5 -0
- arabic_datetime-0.0.0/arabic_datetime.egg-info/SOURCES.txt +12 -0
- arabic_datetime-0.0.0/arabic_datetime.egg-info/dependency_links.txt +1 -0
- arabic_datetime-0.0.0/arabic_datetime.egg-info/top_level.txt +1 -0
- arabic_datetime-0.0.0/pyproject.toml +7 -0
- arabic_datetime-0.0.0/setup.cfg +22 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT Lincense
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Khaled Auwad
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
# arabic-datetime - Arabic datetime formatter
|
|
2
|
+
|
|
3
|
+
_Author_: Khaldevmedia
|
|
4
|
+
|
|
5
|
+
_Description_: A python installable package that helps you output dates in Arabic as strings with a formatting that is specific to each Arab county.
|
|
6
|
+
|
|
7
|
+
## Month names in Arabic
|
|
8
|
+
|
|
9
|
+
There are 4 groups of month names in Arabic.
|
|
10
|
+
|
|
11
|
+
### Syriac names:
|
|
12
|
+
|
|
13
|
+
The **Syriac** names of months are used in Syria Palestine Lebanon Jordan and Iraq:
|
|
14
|
+
|
|
15
|
+
> <p style=dir=rtl>كانون الثاني، شباط، آذار، نيسان، أيار، حزيران، تموز، آب، أيلول، تشرين الأول، تشرين الثاني، كانون الأول.</p>
|
|
16
|
+
|
|
17
|
+
### Roman names (group 1):
|
|
18
|
+
|
|
19
|
+
The **Roman names (group 1)** of months are used in Egypt Yemen Sudan Libya Djibouti Comoros Somalia and the Gulf countries:
|
|
20
|
+
|
|
21
|
+
> <p style=dir=rtl>يناير، فبراير، مارس، أبريل، مايو، يونيو، يوليو، أغسطس، سبتمبر، أكتوبر، نوفمبر، ديسمبر.</p>
|
|
22
|
+
|
|
23
|
+
### Roman names (group 2):
|
|
24
|
+
|
|
25
|
+
The **Roman names (group 2)** of months are used in Morocco and Mauritania:
|
|
26
|
+
|
|
27
|
+
> <p style=dir=rtl>يناير، فبراير، مارس، أبريل، ماي، يونيو، يوليوز، غشت، شتنبر، أكتوبر، نونبر، دجنبر.</p>
|
|
28
|
+
|
|
29
|
+
### French names:
|
|
30
|
+
|
|
31
|
+
The **French** names of months are used in Algeria and Tunisia:
|
|
32
|
+
|
|
33
|
+
> <p style=dir=rtl>جانفي، فيفري، مارس، أفريل، ماي، جوان، جويلية، أوت، سبتمبر، أكتوبر، نوفمبر، ديسمبر.</p>
|
|
34
|
+
|
|
35
|
+
## Arabic Numerals
|
|
36
|
+
|
|
37
|
+
There are two groups of **Arabic numerals** used in the Arab countries.
|
|
38
|
+
|
|
39
|
+
### Eastern Arabic Numerals
|
|
40
|
+
|
|
41
|
+
They are used in all Arabe countries except Algeria Tunisia Morocco and Mauritania:
|
|
42
|
+
|
|
43
|
+
> ٠ ١ ٢ ٣ ٤ ٥ ٦ ٧ ٨ ٩
|
|
44
|
+
|
|
45
|
+
### Western Arabic Numerals
|
|
46
|
+
|
|
47
|
+
They are used in Algeria Tunisia Morocco and Mauritania. However, they are also used in the other Arabe countries that use the eastern Arabic numerals:
|
|
48
|
+
|
|
49
|
+
> 0 1 2 3 4 5 6 7 8 9
|
|
50
|
+
|
|
51
|
+
### Numeral Options
|
|
52
|
+
|
|
53
|
+
You can use the eastern or the western Arabic numeral regardless of the month names group you use. As demonstrated below, there is always a default numerals' type depending on the method used.
|
|
54
|
+
|
|
55
|
+
## Installation
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
pip install arabic-datetime
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Example & Usage
|
|
62
|
+
|
|
63
|
+
### Date
|
|
64
|
+
|
|
65
|
+
The Arabic_Date class has several methods that return a date object as string using.
|
|
66
|
+
|
|
67
|
+
#### A specific method for every month names group
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
import datetime
|
|
71
|
+
from arabic_datetime import Arabic_Date
|
|
72
|
+
|
|
73
|
+
# Create arabic_date object from a python date object
|
|
74
|
+
dt = datetime.date(1980, 8, 16)
|
|
75
|
+
arabic_date = Arabic_Date(dt)
|
|
76
|
+
|
|
77
|
+
# Use each method to creat the arabic date strings
|
|
78
|
+
syria_date_fromat = arabic_date.syriac_names()
|
|
79
|
+
egypt_date_fromat = arabic_date.roman1_names()
|
|
80
|
+
morrocco_date_fromat = arabic_date.roman2_names()
|
|
81
|
+
algeria_date_fromat = arabic_date.french_names()
|
|
82
|
+
|
|
83
|
+
print(syriac_date_fromat) # output 16 آب 1980
|
|
84
|
+
print(egypt_date_fromat) # output 16 أغسطس 1980
|
|
85
|
+
print(morrocco_date_fromat) # output 16 غشت 1980
|
|
86
|
+
print(algeria_date_fromat) # output 16 أوت 1980
|
|
87
|
+
|
|
88
|
+
# To use Eastern Arabic Numerals pass True to the function you use
|
|
89
|
+
syria_date_fromat_easter = arabic_date.syriac_names(True)
|
|
90
|
+
print(syria_date_fromat_easter) # output ١٦ آب ١٩٨٠
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
> ##### Note:
|
|
94
|
+
>
|
|
95
|
+
> When you combine Western arabic numerals or western ponctuations with arabic characters they appear messed up if the text direction in the target is set to `left to right` or `dir=ltr` in `HTML`.
|
|
96
|
+
>
|
|
97
|
+
> In order for the arabic date to appear properly especially, if it uses western arabic numeral the direction of the text in the interface you are using must be `right to left`. If you insert the date into an `HTML` elemnt set text direction to `rtl`. If the text direction in the whole `HTML` document is set `rtl` you should be OK.
|
|
98
|
+
|
|
99
|
+
```html
|
|
100
|
+
<p style="dir" ="rtl">16 آب 1980</p>
|
|
101
|
+
<p style="dir" ="rtl">16 أوت 1980</p>
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
> The resut will look like this:
|
|
105
|
+
>
|
|
106
|
+
> <p style=dir=rtl>16 آب 1980</p>
|
|
107
|
+
> <p style=dir=rtl>16 أوت 1980</p>
|
|
108
|
+
|
|
109
|
+
#### A dual date name method
|
|
110
|
+
|
|
111
|
+
It's very common that two month names are used at the same time to display the date, with the second one put between parentheses, like this:
|
|
112
|
+
|
|
113
|
+
> <p style=dir=rtl>16 آب (أغسطس) 1980</p>
|
|
114
|
+
> <p style=dir=rtl>١٦ آب (أغسطس) ١٩٨٠</p>
|
|
115
|
+
|
|
116
|
+
To get the Arabic date formated like this, use the `dual_names` method:
|
|
117
|
+
|
|
118
|
+
```python
|
|
119
|
+
import datetime
|
|
120
|
+
from arabic_datetime import Arabic_Date
|
|
121
|
+
|
|
122
|
+
# Create arabic_date object from a python date object
|
|
123
|
+
dt = datetime.date(1980, 8, 16)
|
|
124
|
+
arabic_date = Arabic_Date(dt)
|
|
125
|
+
|
|
126
|
+
# Use the dual_names() method to create Arabic date string that shows two names of the month.
|
|
127
|
+
# This method takes 3 arguments: 2 strings for the month group names (the second one
|
|
128
|
+
# will be put between parentheses), and a boolean to determine whether to format the
|
|
129
|
+
# Arabic date with the Eastern Numerals or not:
|
|
130
|
+
dual_date_ro_sy = arabic_date.dual_names("syriac", "roman1", False)
|
|
131
|
+
print(dual_date_ro_sy) # output 16 أغسطس (آب) 1980
|
|
132
|
+
|
|
133
|
+
# If you don't pass the third argument, the default is False. Also you can use
|
|
134
|
+
# keyword arguments to make them more readable:
|
|
135
|
+
# arabic_date.dual_names(first="syriac", second="roman1", east_nums=False)
|
|
136
|
+
|
|
137
|
+
# To use Eastern Arabic Numerals, pass True to the function you use as a third argument.
|
|
138
|
+
# We will also switch the month names in this example:
|
|
139
|
+
dual_date_sy_ro = arabic_date.dual_names(first="syriac", second="roman1", east_nums=True)
|
|
140
|
+
print(dual_date_sy_ro) # output ١٦ آب (أغسطس) ١٩٨٠
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
##### Accepted values for group names (Look above to see which Arab country uses which names):
|
|
144
|
+
|
|
145
|
+
```
|
|
146
|
+
"syriac" : For Syriac names
|
|
147
|
+
"roman1" : For Roman names (group 1)
|
|
148
|
+
"roman2" : For Roman names (group 2)
|
|
149
|
+
"french" : For French names
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
#### A method that takes the country code to return the corresponding date format
|
|
153
|
+
|
|
154
|
+
```python
|
|
155
|
+
import datetime
|
|
156
|
+
from arabic_datetime import Arabic_Date
|
|
157
|
+
|
|
158
|
+
# Create arabic_date object from a python date object
|
|
159
|
+
dt = datetime.date(1980, 8, 16)
|
|
160
|
+
arabic_date = Arabic_Date(dt)
|
|
161
|
+
|
|
162
|
+
# Use of by_country_code() method. It takes country_code as a string
|
|
163
|
+
# If you don't pass True or False besides the county code string
|
|
164
|
+
# the method will return the numeral type that is most common in that country
|
|
165
|
+
syria_date_fromat = arabic_date.by_country_code("SY")
|
|
166
|
+
algeria_date_fromat_easter = arabic_date.by_country_code("DZ")
|
|
167
|
+
print(syriac_date_fromat) # output ١٦ آب ١٩٨٠
|
|
168
|
+
print(algeria_date_fromat) # output 16 أوت 1980
|
|
169
|
+
|
|
170
|
+
# But if you want to force the numeral type, pass as a second argument
|
|
171
|
+
# True for eastern numerals and False for western numerals
|
|
172
|
+
syria_date_fromat_western = arabic_date.by_country_code("SY", True)
|
|
173
|
+
algeria_date_fromat_easter = arabic_date.by_country_code("DZ", False)
|
|
174
|
+
print(syriac_date_fromat) # output 16 آب 1980
|
|
175
|
+
print(algeria_date_fromat_easter) # output ١٦ أوت ١٩٨٠
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
##### List of country codes:
|
|
179
|
+
|
|
180
|
+
```
|
|
181
|
+
DZ: Algeria
|
|
182
|
+
BH: Bahrain
|
|
183
|
+
KM: Comoros
|
|
184
|
+
DJ: Djibouti
|
|
185
|
+
EG: Egypt
|
|
186
|
+
IQ: Iraq
|
|
187
|
+
JO: Jordan
|
|
188
|
+
KW: Kuwait
|
|
189
|
+
LB: Lebanon
|
|
190
|
+
LY: Libya
|
|
191
|
+
MR: Mauritania
|
|
192
|
+
MA: Morocco
|
|
193
|
+
OM: Oman
|
|
194
|
+
PS: Palestine
|
|
195
|
+
QA: Qatar
|
|
196
|
+
SA: Saudi Arabia
|
|
197
|
+
SO: Somalia
|
|
198
|
+
SD: Sudan
|
|
199
|
+
SY: Syria
|
|
200
|
+
TN: Tunisia
|
|
201
|
+
AE: United Arab Emirates
|
|
202
|
+
YE: Yemen
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### Time
|
|
206
|
+
|
|
207
|
+
The Arabic_Time class has one method that returns a time object as string using the Eastern Arabic numerals.
|
|
208
|
+
|
|
209
|
+
```python
|
|
210
|
+
import datetime
|
|
211
|
+
from arabic_datetime import Arabic_Time
|
|
212
|
+
dt = datetime.time(12, 30, 45)
|
|
213
|
+
arabic_time = Arabic_Time(dt)
|
|
214
|
+
print(arabic_time.time()) # output ١٢:٣٠:٤٥
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
> #### Note:
|
|
218
|
+
>
|
|
219
|
+
> As explained above, make sure that the direction of the text in which the Arabic time is inserted is `right to left`.
|
|
220
|
+
|
|
221
|
+
```html
|
|
222
|
+
<p style="dir" ="rtl">١٢:٣٠:٤٥</p>
|
|
223
|
+
<p style="dir" ="rtl">الساعة حالياً: ١٢:٣٠:٤٥</p>
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
> The resut will look like this:
|
|
227
|
+
>
|
|
228
|
+
> <p style=dir=rtl>١٢:٣٠:٤٥</p>
|
|
229
|
+
> <p style=dir=rtl>الساعة حالياً: ١٢:٣٠:٤٥</p>
|
|
230
|
+
|
|
231
|
+
## License
|
|
232
|
+
|
|
233
|
+
MIT license.
|
|
234
|
+
|
|
235
|
+
## Contact
|
|
236
|
+
|
|
237
|
+
<khaldevmedia@gmail.com>
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
from typing import Union
|
|
2
|
+
import datetime
|
|
3
|
+
|
|
4
|
+
# Import constants
|
|
5
|
+
from . constants import MONTH_GROUPS, AR_NUMS
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Arabic_Date:
|
|
9
|
+
def __init__(self, date_object: Union[datetime.date, datetime.datetime]) -> None:
|
|
10
|
+
if not isinstance(date_object, datetime.date) and not isinstance(date_object, datetime.datetime):
|
|
11
|
+
raise TypeError(
|
|
12
|
+
"Arabic_Date class error: The parameter provided to the instance is not a datetime.date object nor a datetime.datetime object.")
|
|
13
|
+
|
|
14
|
+
self.date_object = date_object
|
|
15
|
+
|
|
16
|
+
self.__year = str(self.date_object.year)
|
|
17
|
+
self.__month = int(self.date_object.month)
|
|
18
|
+
# convert first to `int` to turn 01 into 1
|
|
19
|
+
self.__day = str(int(self.date_object.day))
|
|
20
|
+
|
|
21
|
+
# string keys in translate table must be of length 1
|
|
22
|
+
self.num_trans_table = str.maketrans(AR_NUMS)
|
|
23
|
+
|
|
24
|
+
# Group Name Methods
|
|
25
|
+
def syriac_names(self, east_nums: bool = False) -> str:
|
|
26
|
+
if not isinstance(east_nums, bool):
|
|
27
|
+
raise TypeError(
|
|
28
|
+
f"Arabic_Date class error: east_nums must be a boolean. '{east_nums}' is not boolean and was passed to the class method '{self.syriac_names.__name__}'.")
|
|
29
|
+
|
|
30
|
+
if east_nums:
|
|
31
|
+
return self.__day.translate(self.num_trans_table) + " " + MONTH_GROUPS["syriac"]["months"][self.__month-1] + " " + self.__year.translate(self.num_trans_table)
|
|
32
|
+
else:
|
|
33
|
+
return self.__day + " " + MONTH_GROUPS["syriac"]["months"][self.__month-1] + " " + self.__year
|
|
34
|
+
|
|
35
|
+
def roman1_names(self, east_nums: bool = False) -> str:
|
|
36
|
+
if not isinstance(east_nums, bool):
|
|
37
|
+
raise TypeError(
|
|
38
|
+
f"Arabic_Date class error: east_nums must be a boolean. '{east_nums}' is not boolean and was passed to the class method '{self.roman1_names.__name__}'.")
|
|
39
|
+
|
|
40
|
+
if east_nums:
|
|
41
|
+
return self.__day.translate(self.num_trans_table) + " " + MONTH_GROUPS["roman1"]["months"][self.__month-1] + " " + self.__year.translate(self.num_trans_table)
|
|
42
|
+
else:
|
|
43
|
+
return self.__day + " " + MONTH_GROUPS["roman1"]["months"][self.__month-1] + " " + self.__year
|
|
44
|
+
|
|
45
|
+
def roman2_names(self, east_nums: bool = False) -> str:
|
|
46
|
+
if not isinstance(east_nums, bool):
|
|
47
|
+
raise TypeError(
|
|
48
|
+
f"Arabic_Date class error: east_nums must be a boolean. '{east_nums}' is not boolean and was passed to the class method '{self.roman2_names.__name__}'.")
|
|
49
|
+
|
|
50
|
+
if east_nums:
|
|
51
|
+
return self.__day.translate(self.num_trans_table) + " " + MONTH_GROUPS["roman2"]["months"][self.__month-1] + " " + self.__year.translate(self.num_trans_table)
|
|
52
|
+
else:
|
|
53
|
+
return self.__day + " " + MONTH_GROUPS["roman2"]["months"][self.__month-1] + " " + self.__year
|
|
54
|
+
|
|
55
|
+
def french_names(self, east_nums: bool = False) -> str:
|
|
56
|
+
if not isinstance(east_nums, bool):
|
|
57
|
+
raise TypeError(
|
|
58
|
+
f"Arabic_Date class error: east_nums must be a boolean. '{east_nums}' is not boolean and was passed to the class method '{self.french_names.__name__}'.")
|
|
59
|
+
|
|
60
|
+
if east_nums:
|
|
61
|
+
return self.__day.translate(self.num_trans_table) + " " + MONTH_GROUPS["french"]["months"][self.__month-1] + " " + self.__year.translate(self.num_trans_table)
|
|
62
|
+
else:
|
|
63
|
+
return self.__day + " " + MONTH_GROUPS["french"]["months"][self.__month-1] + " " + self.__year
|
|
64
|
+
|
|
65
|
+
# Dual Name Method
|
|
66
|
+
def dual_names(self, first: str, second: str, east_nums: bool = False) -> str:
|
|
67
|
+
if not isinstance(first, str):
|
|
68
|
+
raise TypeError(
|
|
69
|
+
f"Arabic_Date class error: Unknown month group name: '{first}' passed as a parameter to the method '{self.dual_names.__name__}'.")
|
|
70
|
+
elif not isinstance(second, str):
|
|
71
|
+
raise TypeError(
|
|
72
|
+
f"Arabic_Date class error: Unknown month group name: '{second}' passed as a parameter to the method '{self.dual_names.__name__}'.")
|
|
73
|
+
elif not isinstance(east_nums, bool):
|
|
74
|
+
raise TypeError(
|
|
75
|
+
f"Arabic_Date class error: east_nums must be a boolean. '{east_nums}' is not boolean and was passed to the method '{self.dual_names.__name__}'.")
|
|
76
|
+
|
|
77
|
+
if first.strip().lower() == second.strip().lower():
|
|
78
|
+
raise ValueError(
|
|
79
|
+
f"Arabic_Date class error: The first group name and the second group name sould not be identicial: '{first}' was passed as the first and the second parameter to the method {self.dual_names.__name__}. Note that these particular parameters are not case sensitive.")
|
|
80
|
+
valid_first_group = False
|
|
81
|
+
valid_second_group = False
|
|
82
|
+
for group, _ in MONTH_GROUPS.items():
|
|
83
|
+
if first.lower() in group:
|
|
84
|
+
valid_first_group = True
|
|
85
|
+
for group, _ in MONTH_GROUPS.items():
|
|
86
|
+
if second.lower() in group:
|
|
87
|
+
valid_second_group = True
|
|
88
|
+
if not valid_first_group or not valid_second_group:
|
|
89
|
+
error_submessage = ""
|
|
90
|
+
if not valid_first_group:
|
|
91
|
+
error_submessage += f"Unknown first groupe name '{
|
|
92
|
+
first}'"
|
|
93
|
+
if not valid_second_group:
|
|
94
|
+
if error_submessage == "":
|
|
95
|
+
error_submessage += f"Unknown second groupe name '{
|
|
96
|
+
second}'"
|
|
97
|
+
else:
|
|
98
|
+
error_submessage += f" and also unknown second groupe name '{
|
|
99
|
+
second}'"
|
|
100
|
+
raise ValueError(
|
|
101
|
+
f"Arabic_Date class error: {error_submessage} in the parameters passed to the method '{self.dual_names.__name__}'.")
|
|
102
|
+
|
|
103
|
+
if east_nums == True:
|
|
104
|
+
return self.__day.translate(self.num_trans_table) + " " + MONTH_GROUPS[first]["months"][self.__month-1] + " (" + MONTH_GROUPS[second]["months"][self.__month-1] + ") " + self.__year.translate(self.num_trans_table)
|
|
105
|
+
else:
|
|
106
|
+
return self.__day + " " + MONTH_GROUPS[first]["months"][self.__month-1] + " (" + MONTH_GROUPS[second]["months"][self.__month-1] + ") " + self.__year
|
|
107
|
+
|
|
108
|
+
# Date By Country Code Method
|
|
109
|
+
def by_country_code(self, country_code: str, east_nums: bool = None) -> str:
|
|
110
|
+
if not isinstance(country_code, str):
|
|
111
|
+
raise TypeError(
|
|
112
|
+
f"Arabic_Date class error: The 'country_code' parameter passed to the class method '{self.by_country_code.__name__}' is not a string.")
|
|
113
|
+
|
|
114
|
+
elif east_nums is not None and not isinstance(east_nums, bool):
|
|
115
|
+
raise TypeError(
|
|
116
|
+
f"Arabic_Date class error: east_nums must be a boolean. '{east_nums}' is not boolean and was passed to the class method '{self.french_names.__name__}'.")
|
|
117
|
+
|
|
118
|
+
for _, group in MONTH_GROUPS.items():
|
|
119
|
+
if country_code.upper() in group["countries"]:
|
|
120
|
+
if east_nums is not None and east_nums:
|
|
121
|
+
return self.__day.translate(self.num_trans_table) + " " + group["months"][self.__month-1] + " " + self.__year.translate(self.num_trans_table)
|
|
122
|
+
elif east_nums is not None and not east_nums:
|
|
123
|
+
return self.__day + " " + group["months"][self.__month-1] + " " + self.__year
|
|
124
|
+
else:
|
|
125
|
+
if group["east_nums"]:
|
|
126
|
+
return self.__day.translate(self.num_trans_table) + " " + group["months"][self.__month-1] + " " + self.__year.translate(self.num_trans_table)
|
|
127
|
+
else:
|
|
128
|
+
return self.__day + " " + group["months"][self.__month-1] + " " + self.__year
|
|
129
|
+
|
|
130
|
+
else:
|
|
131
|
+
raise ValueError(
|
|
132
|
+
f"Arabic_Date class error: Unknown country code '{country_code}' passed to the class method '{self.by_country_code.__name__}'.")
|
|
133
|
+
|
|
134
|
+
# Eastern Numeric Date Method
|
|
135
|
+
def eastern_numeric_date(self, separator: str = "/") -> str:
|
|
136
|
+
if not isinstance(separator, str):
|
|
137
|
+
raise TypeError(
|
|
138
|
+
f"Arabic_Date class error: The 'separator' parameter passed to the class method '{self.eastern_numeric_date.__name__}' is not a string.")
|
|
139
|
+
return self.__day.translate(self.num_trans_table) + separator + str(self.__month).translate(self.num_trans_table) + separator + self.__year.translate(self.num_trans_table)
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
from typing import Union
|
|
2
|
+
import datetime
|
|
3
|
+
|
|
4
|
+
# Import constants
|
|
5
|
+
from . constants import AR_NUMS
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Arabic_Time:
|
|
9
|
+
def __init__(self, time_object: Union[datetime.time, datetime.datetime]) -> None:
|
|
10
|
+
if not isinstance(time_object, datetime.time) and not isinstance(time_object, datetime.datetime):
|
|
11
|
+
raise TypeError(
|
|
12
|
+
f"Arabic_Time class error: The parameter passed to the instance is not a datetime.time object nor a datetime.datetime object.")
|
|
13
|
+
|
|
14
|
+
self.time_object = time_object
|
|
15
|
+
|
|
16
|
+
# string keys in translate table must be of length 1
|
|
17
|
+
self.num_trans_table = str.maketrans(AR_NUMS)
|
|
18
|
+
|
|
19
|
+
# convert first to `int` to turn 01 into 1
|
|
20
|
+
self.__hour = str(int(self.time_object.hour))
|
|
21
|
+
self.__minute = str(int(self.time_object.minute))
|
|
22
|
+
self.__second = str(int(self.time_object.second))
|
|
23
|
+
|
|
24
|
+
def time(self, separator: str = ":") -> str:
|
|
25
|
+
if not isinstance(separator, str):
|
|
26
|
+
raise TypeError(
|
|
27
|
+
f"Arabic_Date class error: The 'separator' parameter passed to the class method '{self.time.__name__}' is not a string.")
|
|
28
|
+
return self.__hour.translate(self.num_trans_table) + separator + self.__minute.translate(self.num_trans_table) + separator + self.__second.translate(self.num_trans_table)
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
# Constants
|
|
2
|
+
AR_NUMS = {
|
|
3
|
+
"0": "٠",
|
|
4
|
+
"1": "١",
|
|
5
|
+
"2": "٢",
|
|
6
|
+
"3": "٣",
|
|
7
|
+
"4": "٤",
|
|
8
|
+
"5": "٥",
|
|
9
|
+
"6": "٦",
|
|
10
|
+
"7": "٧",
|
|
11
|
+
"8": "٨",
|
|
12
|
+
"9": "٩"
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
ALL_COUNTRY_CODES = [
|
|
16
|
+
"DZ",
|
|
17
|
+
"BH",
|
|
18
|
+
"KM",
|
|
19
|
+
"DJ",
|
|
20
|
+
"EG",
|
|
21
|
+
"IQ",
|
|
22
|
+
"JO",
|
|
23
|
+
"KW",
|
|
24
|
+
"LB",
|
|
25
|
+
"LY",
|
|
26
|
+
"MR",
|
|
27
|
+
"MA",
|
|
28
|
+
"OM",
|
|
29
|
+
"PS",
|
|
30
|
+
"QA",
|
|
31
|
+
"SA",
|
|
32
|
+
"SO",
|
|
33
|
+
"SD",
|
|
34
|
+
"SY",
|
|
35
|
+
"TN",
|
|
36
|
+
"AE",
|
|
37
|
+
"YE"
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
MONTH_GROUPS = {
|
|
41
|
+
"syriac": {
|
|
42
|
+
"east_nums": True,
|
|
43
|
+
"months": [
|
|
44
|
+
"كانون الثاني",
|
|
45
|
+
"شباط",
|
|
46
|
+
"آذار",
|
|
47
|
+
"نيسان",
|
|
48
|
+
"أيار",
|
|
49
|
+
"حزيران",
|
|
50
|
+
"تموز",
|
|
51
|
+
"آب",
|
|
52
|
+
"أيلول",
|
|
53
|
+
"تشرين الأول",
|
|
54
|
+
"تشرين الثاني",
|
|
55
|
+
"كانون الأول"
|
|
56
|
+
],
|
|
57
|
+
"countries": [
|
|
58
|
+
"IQ",
|
|
59
|
+
"JO",
|
|
60
|
+
"LB",
|
|
61
|
+
"PS",
|
|
62
|
+
"SO",
|
|
63
|
+
"SY",
|
|
64
|
+
],
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
"roman1": {
|
|
68
|
+
"east_nums": True,
|
|
69
|
+
"months": [
|
|
70
|
+
"يناير",
|
|
71
|
+
"فبراير",
|
|
72
|
+
"مارس",
|
|
73
|
+
"أبريل",
|
|
74
|
+
"مايو",
|
|
75
|
+
"يونيو",
|
|
76
|
+
"يوليو",
|
|
77
|
+
"أغسطس",
|
|
78
|
+
"سبتمبر",
|
|
79
|
+
"أكتوبر",
|
|
80
|
+
"نوفمبر",
|
|
81
|
+
"ديسمبر",
|
|
82
|
+
],
|
|
83
|
+
"countries": [
|
|
84
|
+
"BH",
|
|
85
|
+
"KM",
|
|
86
|
+
"DJ",
|
|
87
|
+
"EG",
|
|
88
|
+
"KW",
|
|
89
|
+
"LY",
|
|
90
|
+
"OM",
|
|
91
|
+
"QA",
|
|
92
|
+
"SA",
|
|
93
|
+
"SO",
|
|
94
|
+
"SD",
|
|
95
|
+
"AE",
|
|
96
|
+
"YE",
|
|
97
|
+
],
|
|
98
|
+
},
|
|
99
|
+
"roman2": {
|
|
100
|
+
"east_nums": False,
|
|
101
|
+
"months": [
|
|
102
|
+
"يناير",
|
|
103
|
+
"فبراير",
|
|
104
|
+
"مارس",
|
|
105
|
+
"أبريل",
|
|
106
|
+
"ماي",
|
|
107
|
+
"يونيو",
|
|
108
|
+
"يوليوز",
|
|
109
|
+
"غشت",
|
|
110
|
+
"شتنبر",
|
|
111
|
+
"أكتوبر",
|
|
112
|
+
"نونبر",
|
|
113
|
+
"دجنبر",
|
|
114
|
+
],
|
|
115
|
+
"countries": [
|
|
116
|
+
"MA",
|
|
117
|
+
"MR",
|
|
118
|
+
]
|
|
119
|
+
},
|
|
120
|
+
|
|
121
|
+
"french": {
|
|
122
|
+
"east_nums": False,
|
|
123
|
+
"months": [
|
|
124
|
+
"جانفي",
|
|
125
|
+
"فيفري",
|
|
126
|
+
"مارس",
|
|
127
|
+
"أفريل",
|
|
128
|
+
"ماي",
|
|
129
|
+
"جوان",
|
|
130
|
+
"جويلية",
|
|
131
|
+
"أوت",
|
|
132
|
+
"سبتمبر",
|
|
133
|
+
"أكتوبر",
|
|
134
|
+
"نوفمبر",
|
|
135
|
+
"ديسمبر",
|
|
136
|
+
],
|
|
137
|
+
"countries": [
|
|
138
|
+
"DZ",
|
|
139
|
+
"TN",
|
|
140
|
+
]
|
|
141
|
+
},
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
COUNTRY_CODES_DICT = {
|
|
146
|
+
"DZ": "Algeria",
|
|
147
|
+
"BH": "Bahrain",
|
|
148
|
+
"KM": "Comoros",
|
|
149
|
+
"DJ": "Djibouti",
|
|
150
|
+
"EG": "Egypt",
|
|
151
|
+
"IQ": "Iraq",
|
|
152
|
+
"JO": "Jordan",
|
|
153
|
+
"KW": "Kuwait",
|
|
154
|
+
"LB": "Lebanon",
|
|
155
|
+
"LY": "Libya",
|
|
156
|
+
"MR": "Mauritania",
|
|
157
|
+
"MA": "Morocco",
|
|
158
|
+
"OM": "Oman",
|
|
159
|
+
"PS": "Palestine",
|
|
160
|
+
"QA": "Qatar",
|
|
161
|
+
"SA": "Saudi Arabia",
|
|
162
|
+
"SO": "Somalia",
|
|
163
|
+
"SD": "Sudan",
|
|
164
|
+
"SY": "Syria",
|
|
165
|
+
"TN": "Tunisia",
|
|
166
|
+
"AE": "United Arab Emirates",
|
|
167
|
+
"YE": "Yemen"
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
DUAL_MONTHS = {
|
|
171
|
+
"1": "كانون الثاني (يناير)",
|
|
172
|
+
"2": "شباط (فبراير)",
|
|
173
|
+
"3": "آذار (مارس)",
|
|
174
|
+
"4": "نيسان (أبريل)",
|
|
175
|
+
"5": "أيار (مايو)",
|
|
176
|
+
"6": "حزيران (يونيو)",
|
|
177
|
+
"7": "تموز (يوليو)",
|
|
178
|
+
"8": "آب (أغسطس)",
|
|
179
|
+
"9": "أيلول (سبتمبر)",
|
|
180
|
+
"10": "تشرين الأول (أكتوبر)",
|
|
181
|
+
"11": "تشرين الثاني (نوفمبر)",
|
|
182
|
+
"12": "كانون الأول (ديسمبر)"
|
|
183
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
setup.cfg
|
|
5
|
+
arabic_datetime/__init__.py
|
|
6
|
+
arabic_datetime/arabic_date.py
|
|
7
|
+
arabic_datetime/arabic_time.py
|
|
8
|
+
arabic_datetime/constants.py
|
|
9
|
+
arabic_datetime.egg-info/PKG-INFO
|
|
10
|
+
arabic_datetime.egg-info/SOURCES.txt
|
|
11
|
+
arabic_datetime.egg-info/dependency_links.txt
|
|
12
|
+
arabic_datetime.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
arabic_datetime
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
[metadate]
|
|
2
|
+
name = arabic-datetime
|
|
3
|
+
version = 0.0.1
|
|
4
|
+
author = Khaldevmedia
|
|
5
|
+
author_email = khaldevmedia@gmail.ocm
|
|
6
|
+
description = For formating arabic date and time
|
|
7
|
+
long_description = file: README.md
|
|
8
|
+
long_description_content_type = text/markdown
|
|
9
|
+
url = https://github.com/khaldevmedia/arabic-datetime
|
|
10
|
+
keywords = python, arabic, datetime, date, time, arabic numerals
|
|
11
|
+
classifiers =
|
|
12
|
+
Programming Language :: Python :: 3
|
|
13
|
+
License :: OSI Approved :: MIT License
|
|
14
|
+
Operating System :: OS Independent
|
|
15
|
+
|
|
16
|
+
[options]
|
|
17
|
+
python_requires = >=3.7
|
|
18
|
+
|
|
19
|
+
[egg_info]
|
|
20
|
+
tag_build =
|
|
21
|
+
tag_date = 0
|
|
22
|
+
|