nepali 0.5.5__py3-none-any.whl → 1.2.0__py3-none-any.whl
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.
- nepali/__init__.py +1 -1
- nepali/char.py +102 -81
- nepali/constants.py +66 -0
- nepali/date_converter.py +334 -0
- nepali/datetime/__init__.py +13 -14
- nepali/datetime/_datetime.py +391 -402
- nepali/datetime/_formatter.py +325 -0
- nepali/datetime/_humanize.py +117 -132
- nepali/datetime/_nepalimonth.py +118 -0
- nepali/datetime/_nepaliweek.py +125 -0
- nepali/datetime/parser/__init__.py +4 -4
- nepali/datetime/parser/_parser.py +59 -50
- nepali/datetime/parser/validators.py +249 -159
- nepali/datetime/utils.py +38 -0
- nepali/exceptions.py +8 -3
- nepali/locations/__init__.py +3 -0
- nepali/locations/_data.py +4271 -0
- nepali/locations/_locations.py +38 -0
- nepali/locations/models.py +104 -0
- nepali/locations/utils.py +54 -0
- nepali/number/__init__.py +19 -0
- nepali/number/_nepalinumber.py +563 -0
- nepali/number/_number.py +53 -0
- nepali/number/utils.py +72 -0
- nepali/phone_number.py +183 -0
- nepali/templatetags/__init__.py +0 -0
- nepali/templatetags/nepalidatetime.py +194 -24
- nepali/templatetags/nepalinumber.py +97 -7
- nepali/tests/test_date_converter.py +148 -0
- nepali/tests/test_datetime.py +275 -29
- nepali/tests/test_humanize.py +78 -7
- nepali/tests/test_locations.py +154 -0
- nepali/tests/test_nepalimonth.py +152 -0
- nepali/tests/test_nepaliweek.py +154 -0
- nepali/tests/test_number.py +3152 -0
- nepali/tests/test_parser.py +82 -69
- nepali/tests/test_phone_number.py +254 -0
- nepali/tests/test_timezone.py +192 -0
- nepali/timezone.py +50 -7
- nepali/utils.py +9 -68
- nepali-1.2.0.dist-info/METADATA +476 -0
- nepali-1.2.0.dist-info/RECORD +46 -0
- {nepali-0.5.5.dist-info → nepali-1.2.0.dist-info}/WHEEL +1 -1
- {nepali-0.5.5.dist-info → nepali-1.2.0.dist-info/licenses}/LICENSE +1 -1
- {nepali-0.5.5.dist-info → nepali-1.2.0.dist-info}/top_level.txt +0 -0
- nepali/datetime/_converter.py +0 -394
- nepali/datetime/_formarter.py +0 -314
- nepali/datetime.py +0 -1169
- nepali/number.py +0 -51
- nepali-0.5.5.dist-info/METADATA +0 -220
- nepali-0.5.5.dist-info/RECORD +0 -27
|
@@ -1,54 +1,60 @@
|
|
|
1
|
-
|
|
1
|
+
"""
|
|
2
2
|
validates parsing
|
|
3
|
-
|
|
4
|
-
import
|
|
5
|
-
from datetime import date
|
|
3
|
+
"""
|
|
4
|
+
from __future__ import annotations
|
|
6
5
|
|
|
7
|
-
|
|
8
|
-
from nepali.datetime import nepalidatetime
|
|
6
|
+
import re
|
|
9
7
|
|
|
8
|
+
from nepali.char import nepali_to_english_text
|
|
9
|
+
from nepali.constants import NEPALI_MONTHS_EN, WEEKS_ABBR_EN, WEEKS_EN
|
|
10
|
+
from nepali.datetime import nepalidatetime, nepalimonth, nepaliweek
|
|
10
11
|
|
|
11
12
|
__nepali_time_re__CACHE = None
|
|
12
13
|
|
|
14
|
+
|
|
13
15
|
class NepaliTimeRE(dict):
|
|
14
|
-
|
|
15
16
|
def __init__(self):
|
|
16
17
|
"""Create keys/values.
|
|
17
18
|
Order of execution is important for dependency reasons.
|
|
18
19
|
"""
|
|
19
20
|
base = super()
|
|
20
|
-
base.__init__(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
21
|
+
base.__init__(
|
|
22
|
+
{
|
|
23
|
+
# The " [1-9]" part of the regex is to make %c from ANSI C work
|
|
24
|
+
"d": r"(?P<d>3[0-2]|[1-2]\d|0[1-9]|[1-9]| [1-9])",
|
|
25
|
+
"-d": r"(?P<d>3[0-2]|[1-2]\d|0[1-9]|[1-9]| [1-9])", # same as "d"
|
|
26
|
+
"f": r"(?P<f>[0-9]{1,6})",
|
|
27
|
+
"H": r"(?P<H>2[0-3]|[0-1]\d|\d)",
|
|
28
|
+
"-H": r"(?P<H>2[0-3]|[0-1]\d|\d)",
|
|
29
|
+
"I": r"(?P<I>1[0-2]|0[1-9]|[1-9])",
|
|
30
|
+
"-I": r"(?P<I>1[0-2]|0[1-9]|[1-9])",
|
|
31
|
+
"G": r"(?P<G>\d\d\d\d)",
|
|
32
|
+
"j": r"(?P<j>36[0-6]|3[0-5]\d|[1-2]\d\d|0[1-9]\d|00[1-9]|[1-9]\d|0[1-9]|[1-9])",
|
|
33
|
+
"m": r"(?P<m>1[0-2]|0[1-9]|[1-9])",
|
|
34
|
+
"-m": r"(?P<m>1[0-2]|0[1-9]|[1-9])", # same as "m"
|
|
35
|
+
"M": r"(?P<M>[0-5]\d|\d)",
|
|
36
|
+
"-M": r"(?P<M>[0-5]\d|\d)", # same as "M"
|
|
37
|
+
"S": r"(?P<S>6[0-1]|[0-5]\d|\d)",
|
|
38
|
+
"-S": r"(?P<S>6[0-1]|[0-5]\d|\d)", # same as "S"
|
|
39
|
+
"w": r"(?P<w>[0-6])",
|
|
40
|
+
"y": r"(?P<y>\d\d)",
|
|
41
|
+
"Y": r"(?P<Y>\d\d\d\d)",
|
|
42
|
+
"z": r"(?P<z>[+-]\d\d:?[0-5]\d(:?[0-5]\d(\.\d{1,6})?)?|(?-i:Z))",
|
|
43
|
+
"A": self.__seqToRE(WEEKS_EN, "A"),
|
|
44
|
+
"a": self.__seqToRE(WEEKS_ABBR_EN, "a"),
|
|
45
|
+
"B": self.__seqToRE(NEPALI_MONTHS_EN, "B"),
|
|
46
|
+
"b": self.__seqToRE(NEPALI_MONTHS_EN, "b"),
|
|
47
|
+
"p": self.__seqToRE(
|
|
48
|
+
(
|
|
49
|
+
"AM",
|
|
50
|
+
"PM",
|
|
51
|
+
),
|
|
52
|
+
"p",
|
|
53
|
+
),
|
|
54
|
+
"%": "%",
|
|
55
|
+
}
|
|
56
|
+
)
|
|
57
|
+
|
|
52
58
|
def __seqToRE(self, to_convert, directive):
|
|
53
59
|
"""Convert a list to a regex string for matching a directive.
|
|
54
60
|
Want possible matching values to be from longest to shortest. This
|
|
@@ -58,58 +64,64 @@ class NepaliTimeRE(dict):
|
|
|
58
64
|
"""
|
|
59
65
|
to_convert = sorted(to_convert, key=len, reverse=True)
|
|
60
66
|
for value in to_convert:
|
|
61
|
-
if value !=
|
|
67
|
+
if value != "":
|
|
62
68
|
break
|
|
63
69
|
else:
|
|
64
|
-
return
|
|
65
|
-
regex =
|
|
66
|
-
regex =
|
|
67
|
-
return
|
|
68
|
-
|
|
69
|
-
def pattern(self,
|
|
70
|
-
|
|
71
|
-
Handle conversion from format directives to
|
|
72
|
-
|
|
73
|
-
processed_format =
|
|
70
|
+
return ""
|
|
71
|
+
regex = "|".join(re.escape(stuff) for stuff in to_convert)
|
|
72
|
+
regex = f"(?P<{directive}>{regex}"
|
|
73
|
+
return f"{regex})"
|
|
74
|
+
|
|
75
|
+
def pattern(self, date_format):
|
|
76
|
+
"""
|
|
77
|
+
Handle conversion from format directives to regex.
|
|
78
|
+
"""
|
|
79
|
+
processed_format = ""
|
|
74
80
|
regex_chars = re.compile(r"([\\.^$*+?\(\){}\[\]|])")
|
|
75
|
-
|
|
76
|
-
whitespace_replacement = re.compile(r
|
|
77
|
-
|
|
78
|
-
while
|
|
79
|
-
directive_index =
|
|
81
|
+
date_format = regex_chars.sub(r"\\\1", date_format)
|
|
82
|
+
whitespace_replacement = re.compile(r"\s+")
|
|
83
|
+
date_format = whitespace_replacement.sub(r"\\s+", date_format)
|
|
84
|
+
while "%" in date_format:
|
|
85
|
+
directive_index = date_format.index("%") + 1
|
|
80
86
|
index_increment = 1
|
|
81
|
-
|
|
82
|
-
if
|
|
87
|
+
|
|
88
|
+
if date_format[directive_index] == "-":
|
|
83
89
|
index_increment = 2
|
|
84
|
-
|
|
85
|
-
if
|
|
90
|
+
|
|
91
|
+
if (
|
|
92
|
+
date_format[directive_index : directive_index + index_increment]
|
|
93
|
+
not in self
|
|
94
|
+
):
|
|
86
95
|
return None
|
|
87
96
|
|
|
88
|
-
processed_format = "%s%s%s" % (
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
97
|
+
processed_format = "%s%s%s" % (
|
|
98
|
+
processed_format,
|
|
99
|
+
date_format[: directive_index - 1],
|
|
100
|
+
self[date_format[directive_index : directive_index + index_increment]],
|
|
101
|
+
)
|
|
102
|
+
date_format = date_format[directive_index + index_increment :]
|
|
103
|
+
return f"^{processed_format}{date_format}$"
|
|
93
104
|
|
|
94
|
-
def compile(self,
|
|
105
|
+
def compile(self, date_format):
|
|
95
106
|
"""Return a compiled re object for the format string."""
|
|
96
|
-
return re.compile(self.pattern(
|
|
107
|
+
return re.compile(self.pattern(date_format), re.IGNORECASE)
|
|
108
|
+
|
|
97
109
|
|
|
98
110
|
def get_nepali_time_re_object():
|
|
99
111
|
global __nepali_time_re__CACHE
|
|
100
|
-
if __nepali_time_re__CACHE
|
|
112
|
+
if __nepali_time_re__CACHE is None:
|
|
101
113
|
__nepali_time_re__CACHE = NepaliTimeRE()
|
|
102
114
|
return __nepali_time_re__CACHE
|
|
103
115
|
|
|
104
116
|
|
|
105
117
|
def extract(datetime_str, format):
|
|
106
|
-
|
|
118
|
+
"""
|
|
107
119
|
Extracts year, month, day, hour, minute, etc from the given format.
|
|
108
|
-
|
|
120
|
+
|
|
109
121
|
eg.
|
|
110
122
|
USAGE: extract("2078-01-12", format="%Y-%m-%d")
|
|
111
123
|
INPUT:
|
|
112
|
-
datetime_str="2078-01-12"
|
|
124
|
+
datetime_str="2078-01-12"
|
|
113
125
|
format="%Y-%m-%d"
|
|
114
126
|
|
|
115
127
|
OUTPUT:
|
|
@@ -118,19 +130,151 @@ def extract(datetime_str, format):
|
|
|
118
130
|
"m": 1,
|
|
119
131
|
"d": 12,
|
|
120
132
|
}
|
|
121
|
-
|
|
133
|
+
"""
|
|
122
134
|
|
|
123
135
|
# converting datetime_str to english if any exists
|
|
124
136
|
datetime_str = nepali_to_english_text(datetime_str)
|
|
125
137
|
|
|
126
|
-
re_compiled_format = get_nepali_time_re_object().compile(format
|
|
138
|
+
re_compiled_format = get_nepali_time_re_object().compile(format)
|
|
127
139
|
match = re_compiled_format.match(datetime_str)
|
|
128
|
-
if match
|
|
140
|
+
if match is None:
|
|
129
141
|
return {}
|
|
130
142
|
return match.groupdict()
|
|
131
143
|
|
|
144
|
+
|
|
145
|
+
def __convert_12_hour_to_24_hour(hour: int, am_pm: str) -> int:
|
|
146
|
+
"""Converts hours from 12-hour format to 24-hour format.
|
|
147
|
+
|
|
148
|
+
:param hour: The hour value to convert.
|
|
149
|
+
:param am_pm: Either "am" or "pm"; signifies whether the hour is in am or pm.
|
|
150
|
+
|
|
151
|
+
:returns: The value of `hour` converted to 24-hour format.
|
|
152
|
+
"""
|
|
153
|
+
am_pm = am_pm.lower()
|
|
154
|
+
if am_pm == "am" and hour == 12:
|
|
155
|
+
return 0
|
|
156
|
+
elif am_pm == "pm" and hour != 12:
|
|
157
|
+
return hour + 12
|
|
158
|
+
return hour
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def __calculate_year(data: dict) -> int | None:
|
|
162
|
+
"""Calculates the year value from given data.
|
|
163
|
+
|
|
164
|
+
:param data: The dictionary of the format:
|
|
165
|
+
{
|
|
166
|
+
"Y": 2078,
|
|
167
|
+
"b": "Mangsir",
|
|
168
|
+
"d": 12,
|
|
169
|
+
...
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
:returns: The year value of given date data.
|
|
173
|
+
"""
|
|
174
|
+
if "y" in data:
|
|
175
|
+
return int(data["y"]) + 2000
|
|
176
|
+
elif "Y" in data:
|
|
177
|
+
return int(data["Y"])
|
|
178
|
+
return None
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def __calculate_month(data: dict) -> nepalimonth:
|
|
182
|
+
"""Calculates the month value from given data.
|
|
183
|
+
|
|
184
|
+
:param data: The dictionary of the format:
|
|
185
|
+
{
|
|
186
|
+
"Y": 2078,
|
|
187
|
+
"b": "Mangsir",
|
|
188
|
+
"d": 12,
|
|
189
|
+
...
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
:returns: The month value of given date data.
|
|
193
|
+
"""
|
|
194
|
+
if "m" in data:
|
|
195
|
+
return nepalimonth(int(data["m"]))
|
|
196
|
+
elif "b" in data:
|
|
197
|
+
return nepalimonth(data["b"])
|
|
198
|
+
elif "B" in data:
|
|
199
|
+
return nepalimonth(data["B"])
|
|
200
|
+
return nepalimonth(1)
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def __calculate_day(data: dict) -> int:
|
|
204
|
+
"""Calculates the day value from given data.
|
|
205
|
+
|
|
206
|
+
:param data: The dictionary of the format:
|
|
207
|
+
{
|
|
208
|
+
"Y": 2078,
|
|
209
|
+
"b": "Mangsir",
|
|
210
|
+
"d": 12,
|
|
211
|
+
...
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
:returns: The day value of given date data.
|
|
215
|
+
"""
|
|
216
|
+
if "d" in data:
|
|
217
|
+
return int(data["d"])
|
|
218
|
+
return 1
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def __calculate_hour_minute_seconds(data: dict) -> tuple[int, int, int, int]:
|
|
222
|
+
"""Calculates hour, minutes, seconds and microseconds from given data.
|
|
223
|
+
|
|
224
|
+
:param data: The dictionary of the format:
|
|
225
|
+
{
|
|
226
|
+
"Y": 2078,
|
|
227
|
+
"b": "Mangsir",
|
|
228
|
+
"d": 12,
|
|
229
|
+
"H": 12,
|
|
230
|
+
"M": 12,
|
|
231
|
+
"S": 12,
|
|
232
|
+
"f": 12,
|
|
233
|
+
...
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
:returns: A tuple of hour, minute, seconds and microseconds.
|
|
237
|
+
"""
|
|
238
|
+
hour = minute = second = fraction = 0
|
|
239
|
+
if "H" in data:
|
|
240
|
+
hour = int(data["H"])
|
|
241
|
+
elif "I" in data:
|
|
242
|
+
am_pm = data.get("p", "").lower() or "am"
|
|
243
|
+
hour = __convert_12_hour_to_24_hour(hour=int(data["I"]), am_pm=am_pm)
|
|
244
|
+
|
|
245
|
+
if "M" in data:
|
|
246
|
+
minute = int(data["M"])
|
|
247
|
+
|
|
248
|
+
if "S" in data:
|
|
249
|
+
second = int(data["S"])
|
|
250
|
+
|
|
251
|
+
if "f" in data:
|
|
252
|
+
s = data["f"]
|
|
253
|
+
# Pad to always return microseconds.
|
|
254
|
+
s += "0" * (6 - len(s))
|
|
255
|
+
fraction = int(s)
|
|
256
|
+
|
|
257
|
+
return hour, minute, second, fraction
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
def __calculate_weekday(data: dict) -> nepaliweek | None:
|
|
261
|
+
"""Calculates the weekday of the date given in data.
|
|
262
|
+
|
|
263
|
+
:param data: The data that describes the date.
|
|
264
|
+
|
|
265
|
+
:returns: The weekday value; 0 for Sunday, 1 for Monday, etc.
|
|
266
|
+
"""
|
|
267
|
+
if "a" in data:
|
|
268
|
+
return nepaliweek(data["a"])
|
|
269
|
+
elif "A" in data:
|
|
270
|
+
return nepaliweek(data["A"])
|
|
271
|
+
elif "w" in data:
|
|
272
|
+
return nepaliweek((int(data["w"]) - 1) % 7)
|
|
273
|
+
return None
|
|
274
|
+
|
|
275
|
+
|
|
132
276
|
def transform(data: dict):
|
|
133
|
-
|
|
277
|
+
"""
|
|
134
278
|
transforms different format data to uniform data
|
|
135
279
|
|
|
136
280
|
eg.
|
|
@@ -149,114 +293,60 @@ def transform(data: dict):
|
|
|
149
293
|
"day": 12,
|
|
150
294
|
...
|
|
151
295
|
}
|
|
152
|
-
|
|
296
|
+
"""
|
|
297
|
+
|
|
298
|
+
year = __calculate_year(data)
|
|
299
|
+
month = __calculate_month(data)
|
|
300
|
+
day = __calculate_day(data)
|
|
301
|
+
hour, minute, second, fraction = __calculate_hour_minute_seconds(data)
|
|
302
|
+
weekday = __calculate_weekday(data)
|
|
153
303
|
|
|
154
|
-
year = None
|
|
155
|
-
month = day = 1
|
|
156
|
-
hour = minute = second = fraction = 0
|
|
157
|
-
weekday = None
|
|
158
|
-
|
|
159
|
-
for date_key in data.keys():
|
|
160
|
-
if date_key == 'y':
|
|
161
|
-
year = int(data['y'])
|
|
162
|
-
year += 2000
|
|
163
|
-
elif date_key == 'Y':
|
|
164
|
-
year = int(data['Y'])
|
|
165
|
-
elif date_key == 'm':
|
|
166
|
-
month = int(data['m'])
|
|
167
|
-
elif date_key == 'B':
|
|
168
|
-
# TODO: change indexing process
|
|
169
|
-
month = EnglishChar.months.index(data['B'].lower().capitalize()) + 1
|
|
170
|
-
elif date_key == 'b':
|
|
171
|
-
# TODO: change indexing process
|
|
172
|
-
month = EnglishChar.months.index(data['b'].lower().capitalize()) + 1
|
|
173
|
-
elif date_key == 'd':
|
|
174
|
-
day = int(data['d'])
|
|
175
|
-
elif date_key == 'H':
|
|
176
|
-
hour = int(data['H'])
|
|
177
|
-
elif date_key == 'I':
|
|
178
|
-
hour = int(data['I'])
|
|
179
|
-
ampm = data.get('p', '').lower()
|
|
180
|
-
# If there was no AM/PM indicator, we'll treat this like AM
|
|
181
|
-
if ampm in ('', 'am'):
|
|
182
|
-
# We're in AM so the hour is correct unless we're
|
|
183
|
-
# looking at 12 midnight.
|
|
184
|
-
# 12 midnight == 12 AM == hour 0
|
|
185
|
-
if hour == 12:
|
|
186
|
-
hour = 0
|
|
187
|
-
elif ampm == 'pm':
|
|
188
|
-
# We're in PM so we need to add 12 to the hour unless
|
|
189
|
-
# we're looking at 12 noon.
|
|
190
|
-
# 12 noon == 12 PM == hour 12
|
|
191
|
-
if hour != 12:
|
|
192
|
-
hour += 12
|
|
193
|
-
elif date_key == 'M':
|
|
194
|
-
minute = int(data['M'])
|
|
195
|
-
elif date_key == 'S':
|
|
196
|
-
second = int(data['S'])
|
|
197
|
-
elif date_key == 'f':
|
|
198
|
-
s = data['f']
|
|
199
|
-
# Pad to always return microseconds.
|
|
200
|
-
s += "0" * (6 - len(s))
|
|
201
|
-
fraction = int(s)
|
|
202
|
-
elif date_key == 'A':
|
|
203
|
-
# TODO: change indexing process
|
|
204
|
-
weekday = EnglishChar.days.index(data['A'].lower().capitalize()) + 1
|
|
205
|
-
elif date_key == 'a':
|
|
206
|
-
# TODO: change indexing process
|
|
207
|
-
weekday = EnglishChar.days_half.index(data['a'].lower().capitalize()) + 1
|
|
208
|
-
elif date_key == 'w':
|
|
209
|
-
weekday = int(data['w'])
|
|
210
|
-
if weekday == 0:
|
|
211
|
-
weekday = 6
|
|
212
|
-
else:
|
|
213
|
-
weekday -= 1
|
|
214
304
|
return {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
305
|
+
"year": year,
|
|
306
|
+
"month": month,
|
|
307
|
+
"day": day,
|
|
308
|
+
"hour": hour,
|
|
309
|
+
"minute": minute,
|
|
310
|
+
"second": second,
|
|
311
|
+
"microsecond": fraction,
|
|
312
|
+
"weekday": weekday,
|
|
223
313
|
}
|
|
224
314
|
|
|
225
315
|
|
|
226
316
|
def validate(datetime_str, format):
|
|
227
|
-
|
|
317
|
+
"""
|
|
228
318
|
validates datetime_str with the format
|
|
229
319
|
Perform step by step test for fast performance. The steps are:
|
|
230
320
|
-
|
|
231
321
|
-
|
|
232
322
|
returns False if validation failed
|
|
233
323
|
returns nepalidatetime object if validation success.
|
|
234
|
-
|
|
235
|
-
|
|
324
|
+
"""
|
|
325
|
+
|
|
236
326
|
# 1. validate if format is correct.
|
|
237
|
-
if get_nepali_time_re_object().pattern(format
|
|
327
|
+
if get_nepali_time_re_object().pattern(format) is None:
|
|
238
328
|
# regex pattern generation failed
|
|
239
329
|
return None
|
|
240
|
-
|
|
330
|
+
|
|
241
331
|
# 2. validate if parse result if not empty
|
|
242
332
|
parsed_result = extract(datetime_str, format)
|
|
243
|
-
if parsed_result.get(
|
|
244
|
-
# compilation failed or year included
|
|
333
|
+
if parsed_result.get("Y") is None and parsed_result.get("y") is None:
|
|
334
|
+
# compilation failed or year included
|
|
245
335
|
return None
|
|
246
336
|
|
|
247
337
|
# 3. validate if transformation
|
|
248
338
|
transformed_data = transform(parsed_result)
|
|
249
|
-
if transformed_data.get(
|
|
339
|
+
if transformed_data.get("year") is None:
|
|
250
340
|
# could not transform data, not getting year
|
|
251
341
|
return None
|
|
252
342
|
|
|
253
343
|
# 4. create the datetime object
|
|
254
344
|
return nepalidatetime(
|
|
255
|
-
year=transformed_data[
|
|
256
|
-
month=transformed_data[
|
|
257
|
-
day=transformed_data[
|
|
258
|
-
hour=transformed_data[
|
|
259
|
-
minute=transformed_data[
|
|
260
|
-
second=transformed_data[
|
|
261
|
-
microsecond=transformed_data[
|
|
262
|
-
)
|
|
345
|
+
year=transformed_data["year"],
|
|
346
|
+
month=transformed_data["month"],
|
|
347
|
+
day=transformed_data["day"],
|
|
348
|
+
hour=transformed_data["hour"],
|
|
349
|
+
minute=transformed_data["minute"],
|
|
350
|
+
second=transformed_data["second"],
|
|
351
|
+
microsecond=transformed_data["microsecond"],
|
|
352
|
+
)
|
nepali/datetime/utils.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import datetime
|
|
2
|
+
from typing import Any
|
|
3
|
+
|
|
4
|
+
from nepali.exceptions import InvalidNepaliDateTimeObjectException
|
|
5
|
+
|
|
6
|
+
from ._datetime import nepalidate, nepalidatetime
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def to_nepalidatetime(datetime_object: Any) -> nepalidatetime:
|
|
10
|
+
"""
|
|
11
|
+
Converts nepalidate, datetime.datetime, datetime.date to nepalidatetime.
|
|
12
|
+
|
|
13
|
+
:param datetime_object: Object to be converted into nepalidatetime
|
|
14
|
+
:return: nepalidatetime
|
|
15
|
+
:raises InvalidNepaliDateTimeObjectException: If the input data is not a date time objects
|
|
16
|
+
"""
|
|
17
|
+
if isinstance(datetime_object, nepalidatetime):
|
|
18
|
+
return datetime_object
|
|
19
|
+
elif isinstance(datetime_object, nepalidate):
|
|
20
|
+
return nepalidatetime.from_nepali_date(datetime_object)
|
|
21
|
+
elif isinstance(datetime_object, datetime.datetime):
|
|
22
|
+
return nepalidatetime.from_datetime(datetime_object)
|
|
23
|
+
elif isinstance(datetime_object, datetime.date):
|
|
24
|
+
return nepalidatetime.from_date(datetime_object)
|
|
25
|
+
raise InvalidNepaliDateTimeObjectException(
|
|
26
|
+
"Argument must be instance of nepalidate or nepalidatetime or datetime.datetime or datetime.date"
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def to_nepalidate(datetime_object: Any) -> nepalidate:
|
|
31
|
+
"""
|
|
32
|
+
Converts nepalidate, datetime.datetime, datetime.date to nepalidate.
|
|
33
|
+
|
|
34
|
+
:param datetime_object: Object to be converted into nepalidate
|
|
35
|
+
:return: nepalidate
|
|
36
|
+
:raises InvalidNepaliDateTimeObjectException: If the input data is not a date time objects
|
|
37
|
+
"""
|
|
38
|
+
return to_nepalidatetime(datetime_object).date()
|
nepali/exceptions.py
CHANGED
|
@@ -2,17 +2,22 @@
|
|
|
2
2
|
Exceptions for nepali
|
|
3
3
|
"""
|
|
4
4
|
|
|
5
|
+
|
|
5
6
|
class InvalidDateFormatException(Exception):
|
|
6
7
|
pass
|
|
7
8
|
|
|
9
|
+
|
|
8
10
|
class InvalidDateTimeFormatException(Exception):
|
|
9
11
|
pass
|
|
10
12
|
|
|
13
|
+
|
|
11
14
|
class InvalidNepaliDateTimeObjectException(Exception):
|
|
12
15
|
pass
|
|
13
16
|
|
|
17
|
+
|
|
14
18
|
class FormatNotMatchException(Exception):
|
|
15
|
-
|
|
19
|
+
"""
|
|
16
20
|
raised while parsing nepalidatetime format
|
|
17
|
-
|
|
18
|
-
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
pass
|