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.
Files changed (51) hide show
  1. nepali/__init__.py +1 -1
  2. nepali/char.py +102 -81
  3. nepali/constants.py +66 -0
  4. nepali/date_converter.py +334 -0
  5. nepali/datetime/__init__.py +13 -14
  6. nepali/datetime/_datetime.py +391 -402
  7. nepali/datetime/_formatter.py +325 -0
  8. nepali/datetime/_humanize.py +117 -132
  9. nepali/datetime/_nepalimonth.py +118 -0
  10. nepali/datetime/_nepaliweek.py +125 -0
  11. nepali/datetime/parser/__init__.py +4 -4
  12. nepali/datetime/parser/_parser.py +59 -50
  13. nepali/datetime/parser/validators.py +249 -159
  14. nepali/datetime/utils.py +38 -0
  15. nepali/exceptions.py +8 -3
  16. nepali/locations/__init__.py +3 -0
  17. nepali/locations/_data.py +4271 -0
  18. nepali/locations/_locations.py +38 -0
  19. nepali/locations/models.py +104 -0
  20. nepali/locations/utils.py +54 -0
  21. nepali/number/__init__.py +19 -0
  22. nepali/number/_nepalinumber.py +563 -0
  23. nepali/number/_number.py +53 -0
  24. nepali/number/utils.py +72 -0
  25. nepali/phone_number.py +183 -0
  26. nepali/templatetags/__init__.py +0 -0
  27. nepali/templatetags/nepalidatetime.py +194 -24
  28. nepali/templatetags/nepalinumber.py +97 -7
  29. nepali/tests/test_date_converter.py +148 -0
  30. nepali/tests/test_datetime.py +275 -29
  31. nepali/tests/test_humanize.py +78 -7
  32. nepali/tests/test_locations.py +154 -0
  33. nepali/tests/test_nepalimonth.py +152 -0
  34. nepali/tests/test_nepaliweek.py +154 -0
  35. nepali/tests/test_number.py +3152 -0
  36. nepali/tests/test_parser.py +82 -69
  37. nepali/tests/test_phone_number.py +254 -0
  38. nepali/tests/test_timezone.py +192 -0
  39. nepali/timezone.py +50 -7
  40. nepali/utils.py +9 -68
  41. nepali-1.2.0.dist-info/METADATA +476 -0
  42. nepali-1.2.0.dist-info/RECORD +46 -0
  43. {nepali-0.5.5.dist-info → nepali-1.2.0.dist-info}/WHEEL +1 -1
  44. {nepali-0.5.5.dist-info → nepali-1.2.0.dist-info/licenses}/LICENSE +1 -1
  45. {nepali-0.5.5.dist-info → nepali-1.2.0.dist-info}/top_level.txt +0 -0
  46. nepali/datetime/_converter.py +0 -394
  47. nepali/datetime/_formarter.py +0 -314
  48. nepali/datetime.py +0 -1169
  49. nepali/number.py +0 -51
  50. nepali-0.5.5.dist-info/METADATA +0 -220
  51. nepali-0.5.5.dist-info/RECORD +0 -27
nepali/datetime.py DELETED
@@ -1,1169 +0,0 @@
1
- #
2
- #
3
- # Author : Ajesh Sen Thapa
4
- # Website: www.ajesh.com.np
5
- #
6
- #
7
-
8
- import time
9
- import datetime as pythonDateTime
10
-
11
- from .char import NepaliChar, EnglishChar
12
- from .timezone import NepaliTimeZone, now, utc_now
13
- from .utils import to_local, to_utc
14
- from .exceptions import InvalidDateFormatException, InvalidNepaliDateTimeObjectException
15
-
16
- class AbstractNepaliDate:
17
-
18
- __enMonths = [31, 28, 31, 30, 31, 30,31, 31, 30, 31, 30, 31]
19
- enLeapMonths = [31, 29, 31, 30, 31, 30,31, 31, 30, 31, 30, 31]
20
-
21
- # List of nepali months
22
- __npMonths = [
23
- [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 ], # 2000 BS - 1944 AD
24
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ], # 2001 BS
25
- [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
26
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
27
- [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 ],
28
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
29
- [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
30
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
31
- [ 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31 ],
32
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
33
- [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
34
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
35
- [ 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30 ],
36
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
37
- [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
38
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
39
- [ 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30 ],
40
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
41
- [ 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
42
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 ],
43
- [ 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30 ],
44
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
45
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30 ],
46
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 ],
47
- [ 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30 ],
48
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
49
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
50
- [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 ],
51
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
52
- [ 31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30 ],
53
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
54
- [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 ],
55
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
56
- [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
57
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
58
- [ 30, 32, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31 ],
59
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
60
- [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
61
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
62
- [ 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30 ],
63
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
64
- [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
65
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
66
- [ 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30 ],
67
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
68
- [ 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
69
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
70
- [ 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30 ],
71
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
72
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30 ],
73
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 ],
74
- [ 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30 ],
75
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
76
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30 ],
77
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 ],
78
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
79
- [ 31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30 ],
80
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
81
- [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 ],
82
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
83
- [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
84
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
85
- [ 30, 32, 31, 32, 31, 31, 29, 30, 29, 30, 29, 31 ],
86
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
87
- [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
88
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
89
- [ 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31 ],
90
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
91
- [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
92
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
93
- [ 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30 ],
94
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ], # 2071 BS
95
- [ 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30 ], # 2072 BS
96
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ], # 2073 BS
97
- [ 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30 ],
98
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
99
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30 ],
100
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 ],
101
- [ 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30 ],
102
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
103
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30 ],
104
- [ 31, 31, 32, 32, 31, 30, 30, 30, 29, 30, 30, 30 ],
105
- [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30 ],
106
- [ 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30 ],
107
- [ 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30 ],
108
- [ 31, 32, 31, 32, 30, 31, 30, 30, 29, 30, 30, 30 ],
109
- [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30 ],
110
- [ 31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30 ],
111
- [ 30, 31, 32, 32, 30, 31, 30, 30, 29, 30, 30, 30 ],
112
- [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30 ],
113
- [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30 ], # 2090 BS
114
- [ 31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30 ],
115
- [ 30, 31, 32, 32, 31, 30, 30, 30, 29, 30, 30, 30 ],
116
- [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30 ],
117
- [ 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30 ],
118
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 30, 30, 30 ],
119
- [ 30, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
120
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30 ],
121
- [ 31, 31, 32, 31, 31, 31, 29, 30, 29, 30, 29, 31 ],
122
- [ 31, 31, 32, 31, 31, 31, 30, 29, 29, 30, 30, 30 ] # 2099 BS - 2042 AD
123
- ]
124
-
125
- def __init__(self, npYear=0, npMonth=0, npDay=0):
126
-
127
- if npYear == 0 or npMonth == 0 or npDay == 0:
128
- self.setCurrentDate()
129
- else:
130
- self.setNpDate(npYear, npMonth, npDay)
131
-
132
-
133
- def setCurrentDate(self):
134
- """
135
- Setting current date
136
- """
137
- year = int(time.strftime("%Y"))
138
- month = int(time.strftime("%m"))
139
- date = int(time.strftime("%d"))
140
- self.setEnDate(year, month, date)
141
-
142
-
143
- #En to Np date conversion
144
-
145
- def setEnDate(self,year, month, date):
146
- """
147
- * Sets specific en dates to self object *
148
- Refrence np 2000/1/1 with en date 1943/4/14
149
- """
150
- if(not self.__isEnRange(year,month,date)):
151
- raise Exception("Date out of range")
152
-
153
- self.__enYear = year
154
- self.__enMonth = month
155
- self.__enDay = date
156
-
157
- # Setting np reference to 2000/1/1 with en date 1943/4/14
158
- self.__npYear = 2000
159
- self.__npMonth = 1
160
- self.__npDay = 1
161
-
162
- difference = self.enDateDifference(1943, 4, 14)
163
-
164
- # Getting np year untill the difference remains less than 365
165
- index = 0
166
- while( difference >= self.__npYearDays(index) ):
167
- self.__npYear+=1
168
- difference = difference - self.__npYearDays(index)
169
- index+=1
170
-
171
- # Getting np month untill the difference remains less than 31
172
- i = 0
173
- while(difference >= self.__npMonths[index][i]):
174
- difference = difference - self.__npMonths[index][i]
175
- self.__npMonth+=1
176
- i+=1
177
-
178
- # Remaning days is the date
179
- self.__npDay = self.__npDay + difference
180
-
181
- self.weekDay()
182
-
183
-
184
- def toEnString(self, format='-'):
185
- return str(self.__enYear)+format+str(self.__enMonth)+format+str(self.__enDay)
186
-
187
-
188
- def enDateDifference(self, year, month, date):
189
- """
190
- returns difference of days from the self date with the date provided
191
- """
192
- difference = self.__countTotalEnDays(self.__enYear, self.__enMonth, self.__enDay) - self.__countTotalEnDays(year, month, date)
193
- if difference < 0:
194
- return -difference
195
- else:
196
- return difference
197
-
198
-
199
- def __countTotalEnDays(self, year, month, date):
200
- """ counts english date in days with 0000-01-01 """
201
-
202
- totalDays = year * 365 + date
203
-
204
- for i in range(0,month-1):
205
- totalDays = totalDays + self.__enMonths[i]
206
-
207
- totalDays = totalDays + self.__countleap(year, month)
208
- return totalDays
209
-
210
-
211
- def __countleap(self, year, month):
212
- """ counts total leap years from year/month to 0000/01 """
213
- if (month <= 2):
214
- year-=1
215
-
216
- return (year//4-year//100+year//400)
217
-
218
-
219
- def __isEnRange(self, year, month, date):
220
- """ checks if english date in within range 1944 - 2042 """
221
- if(year < 1944 or year > 2042):
222
- return False
223
-
224
- if(month < 1 or month > 12):
225
- return False
226
-
227
- if(date < 1 or date > 31):
228
- return False
229
-
230
- return True
231
-
232
-
233
- def __isLeapYear(self, year):
234
- """ independent method to check leap year """
235
- if(year%4 == 0):
236
- if(year%100 == 0):
237
- return (year%400 == 0)
238
- else:
239
- return True
240
- else:
241
- return False
242
-
243
-
244
-
245
- # Nepali to English date conversion
246
-
247
- def setNpDate(self, year, month, date):
248
- """
249
- * Sets specific np dates to self object *
250
- Refrence en 1994/1/1 with en date 2000/9/17
251
- """
252
- if(not self.__isNpRange(year,month,date)):
253
- raise Exception("Date out of range")
254
-
255
- self.__npYear = year
256
- self.__npMonth = month
257
- self.__npDay = date
258
-
259
- # Setting en reference to 1944/1/1 with np date 2000/9/17
260
- self.__enYear = 1944
261
- self.__enMonth = 1
262
- self.__enDay = 1
263
-
264
- difference = self.npDateDifference(2000, 9, 17)
265
-
266
- # Getting en year untill the difference remains less than 365
267
- while( (difference >= 366 and self.__isLeapYear(self.__enYear)) or (difference >= 365 and not(self.__isLeapYear(self.__enYear)) ) ):
268
- if( self.__isLeapYear(self.__enYear) ):
269
- difference -= 366
270
- else:
271
- difference -= 365
272
- self.__enYear += 1
273
-
274
- # Getting en month untill the difference remains less than 31
275
- if(self.__isLeapYear(self.__enYear)):
276
- monthDays = self.enLeapMonths
277
- else:
278
- monthDays = self.__enMonths
279
- i = 0
280
- while( difference >= monthDays[i]):
281
- self.__enMonth+=1
282
- difference = difference - monthDays[i]
283
- i+=1
284
-
285
- # Remaning days is the date
286
- self.__enDay = self.__enDay + difference
287
-
288
- self.weekDay()
289
-
290
-
291
- def toNpString(self, format="-"):
292
- return str(self.npYear())+format+str(self.npMonthStr())+format+str(self.npDayStr())
293
-
294
-
295
- def npDateDifference(self, year, month, date):
296
- """
297
- Getting difference from the current date with the date provided
298
- """
299
- difference = self.__countTotalNpDays(self.__npYear, self.__npMonth, self.__npDay) - self.__countTotalNpDays(year, month, date)
300
- if(difference < 0):
301
- return -difference
302
- else:
303
- return difference
304
-
305
-
306
- def __countTotalNpDays(self, year, month, date):
307
- """ counts nepali date in days with 2000-01-01 (nepali date) """
308
-
309
- total = 0
310
- if(year < 2000):
311
- return 0
312
-
313
- total = total + (date-1)
314
-
315
- yearIndex = year - 2000
316
- for i in range(0,month-1):
317
- total = total + self.__npMonths[yearIndex][i]
318
-
319
- for i in range(0,yearIndex):
320
- total = total + self.__npYearDays(i)
321
-
322
- return total
323
-
324
-
325
- def __npYearDays(self, index):
326
- """
327
- count total days of specific year ( from index)
328
- input: index (year)
329
- return total (days)
330
-
331
- eg, for 2075 => 2075-2000 = 75
332
- __npYearDays(75) => 365 days
333
-
334
- """
335
- total = 0
336
-
337
- for i in range(0,12):
338
- total += self.__npMonths[index][i]
339
-
340
- return total
341
-
342
-
343
- def __isNpRange(self, year, month, date):
344
- """ checks if nepali date is in range 2000-2098 """
345
- if(year < 2000 or year > 2098):
346
- return False
347
-
348
- if(month < 1 or month > 12):
349
- return False
350
-
351
- if(date < 1 or date > self.__npMonths[year-2000][month-1]):
352
- return False
353
-
354
- return True
355
-
356
-
357
- # Class Regular methods
358
-
359
- def weekDay(self):
360
- # Reference date 1943/4/14 Wednesday
361
- difference = self.enDateDifference(1943, 4, 14)
362
- self.__week_day = ((3 + (difference%7) ) % 7 ) + 1
363
- return self.__week_day
364
-
365
- def enYear(self):
366
- return self.__enYear
367
-
368
- def enMonth(self):
369
- return self.__enMonth
370
-
371
- def enDay(self):
372
- return self.__enDay
373
-
374
- def npYear(self):
375
- return self.__npYear
376
-
377
- def npMonth(self):
378
- return self.__npMonth
379
-
380
- def npDay(self):
381
- return self.__npDay
382
-
383
- def npMonthStr(self):
384
- npMonth = str(self.npMonth())
385
- if len(npMonth) < 2:
386
- npMonth = '0'+npMonth
387
- return npMonth
388
-
389
- def npDayStr(self):
390
- npDay = str(self.npDay())
391
- if len(npDay) < 2:
392
- npDay = '0'+npDay
393
- return npDay
394
-
395
-
396
- class NepaliDate(AbstractNepaliDate):
397
- def __str__(self):
398
- return self.toNpString()
399
-
400
- def __repr__(self):
401
- return "<NepaliDate> "+str(self)
402
-
403
- def to_datetime(self):
404
- return to_local(pythonDateTime.datetime(self.year_en, self.month_en, self.day_en))
405
-
406
- def to_date(self):
407
- return self.to_datetime().date()
408
-
409
- def to_nepali_datetime(self):
410
- return NepaliDateTime.from_nepali_date(self)
411
-
412
- def strftime(self, format):
413
- formater = NepaliDateTimeFormater(self)
414
- return formater.get_str(format)
415
-
416
- def strftime_en(self, format):
417
- formater = NepaliDateTimeFormater(self, True)
418
- return formater.get_str(format)
419
-
420
- # operator overloadings # for future
421
- # pass
422
-
423
- # static methods
424
-
425
- @staticmethod
426
- def now(*args, **kwargs):
427
- return NepaliDate.today()
428
-
429
- @staticmethod
430
- def today():
431
- return NepaliDate()
432
-
433
- @staticmethod
434
- def from_datetime(datetime_object):
435
- return NepaliDate.from_date(datetime_object.date())
436
-
437
- @staticmethod
438
- def from_date(date_object):
439
- npDate = NepaliDate()
440
- npDate.setEnDate(date_object.year, date_object.month, date_object.day)
441
- return npDate
442
-
443
- @staticmethod
444
- def from_nepalidatetime(datetime_object):
445
- return datetime_object.date()
446
-
447
- # property
448
-
449
- # nepali date properties
450
- @property
451
- def year(self):
452
- return self.npYear()
453
-
454
- @property
455
- def month(self):
456
- return self.npMonth()
457
-
458
- @property
459
- def day(self):
460
- return self.npDay()
461
-
462
- # english date propertties
463
- @property
464
- def year_en(self):
465
- return self.enYear()
466
-
467
- @property
468
- def month_en(self):
469
- return self.enMonth()
470
-
471
- @property
472
- def day_en(self):
473
- return self.enDay()
474
-
475
- @property
476
- def week_day(self):
477
- return self.weekDay()
478
-
479
-
480
- class NepaliTime(pythonDateTime.time):
481
-
482
- def __repr__(self):
483
- return "<NepaliTime> "+str(self)
484
-
485
- # static methods
486
-
487
- @staticmethod
488
- def now(*args, **kwargs):
489
- dt = now()
490
- if kwargs.get('microsecond'):
491
- return NepaliTime(dt.time().hour, dt.time().minute, dt.time().second, dt.time().microsecond)
492
- return NepaliTime(dt.time().hour, dt.time().minute, dt.time().second)
493
-
494
-
495
- class NepaliDateTime:
496
- """
497
- NepaliDateTime
498
- """
499
-
500
- def __init__(self, year, month, day, hour=0, minute=0, second=0, microsecond=0):
501
- self.__npDate = NepaliDate(year, month, day)
502
- self.__npTime = NepaliTime(hour, minute, second, microsecond)
503
-
504
- def __str__(self):
505
- return str(self.__npDate)+' '+str(self.__npTime)
506
-
507
- def __repr__(self):
508
- return "<NepaliDateTime> "+str(self)
509
-
510
-
511
- # operator overloadings
512
-
513
- def __add__(self, other):
514
- """ addition """
515
-
516
- if type(other) == pythonDateTime.timedelta:
517
- """
518
- timedelta object
519
- """
520
- return NepaliDateTime.from_datetime(self.to_datetime() + other)
521
-
522
-
523
- return None
524
-
525
- def __sub__(self, other):
526
- """ substraction """
527
-
528
- if type(other) == self.__class__:
529
- """
530
- NepaliDateTime object
531
- """
532
- return self.to_datetime() - other.to_datetime()
533
-
534
- elif type(other) == pythonDateTime.datetime:
535
- return self.to_datetime() - to_local(other)
536
-
537
- elif type(other) == pythonDateTime.timedelta:
538
- """
539
- timedelta object
540
- """
541
- return NepaliDateTime.from_datetime(self.to_datetime() - other)
542
-
543
-
544
- return None
545
-
546
- def __lt__(self, other):
547
- """ less than """
548
-
549
- if type(other) == self.__class__:
550
- """
551
- NepaliDateTime object
552
- """
553
- return self.to_datetime() < other.to_datetime()
554
-
555
- elif type(other) == pythonDateTime.datetime:
556
- """
557
- pythonDateTime object
558
- """
559
- return self.to_datetime() < to_local(other)
560
-
561
-
562
- return None
563
-
564
- def __le__(self, other):
565
- """ less than euqal """
566
-
567
- if type(other) == self.__class__:
568
- """
569
- NepaliDateTime object
570
- """
571
- return self.to_datetime() <= other.to_datetime()
572
-
573
- elif type(other) == pythonDateTime.datetime:
574
- """
575
- pythonDateTime object
576
- """
577
- return self.to_datetime() <= to_local(other)
578
-
579
-
580
- return None
581
-
582
- def __eq__(self, other):
583
- """ equal """
584
-
585
- if type(other) == self.__class__:
586
- """
587
- NepaliDateTime object
588
- """
589
- return self.to_datetime() == other.to_datetime()
590
-
591
- elif type(other) == pythonDateTime.datetime:
592
- """
593
- pythonDateTime object
594
- """
595
- return self.to_datetime() == to_local(other)
596
-
597
-
598
- return None
599
-
600
-
601
- def __ne__(self, other):
602
- """ not equal """
603
-
604
- if type(other) == self.__class__:
605
- """
606
- NepaliDateTime object
607
- """
608
- return self.to_datetime() != other.to_datetime()
609
-
610
- elif type(other) == pythonDateTime.datetime:
611
- """
612
- pythonDateTime object
613
- """
614
- return self.to_datetime() != to_local(other)
615
-
616
-
617
- return None
618
-
619
- def __gt__(self, other):
620
- """ greater than """
621
-
622
- if type(other) == self.__class__:
623
- """
624
- NepaliDateTime object
625
- """
626
- return self.to_datetime() > other.to_datetime()
627
-
628
- elif type(other) == pythonDateTime.datetime:
629
- """
630
- pythonDateTime object
631
- """
632
- return self.to_datetime() > to_local(other)
633
-
634
-
635
- return None
636
-
637
- def __ge__(self, other):
638
- """ greater than equal """
639
-
640
- if type(other) == self.__class__:
641
- """
642
- NepaliDateTime object
643
- """
644
- return self.to_datetime() >= other.to_datetime()
645
-
646
- elif type(other) == pythonDateTime.datetime:
647
- """
648
- pythonDateTime object
649
- """
650
- return self.to_datetime() >= to_local(other)
651
-
652
-
653
- return None
654
-
655
- # object transformation
656
- def to_datetime(self):
657
- return to_local(pythonDateTime.datetime.combine(self.__npDate.to_date(), self.__npTime))
658
-
659
- def date(self):
660
- return self.__npDate
661
-
662
- def time(self):
663
- return self.__npTime
664
-
665
- # string format
666
- def strftime(self, format):
667
- formater = NepaliDateTimeFormater(self)
668
- return formater.get_str(format)
669
-
670
- def strftime_en(self, format):
671
- formater = NepaliDateTimeFormater(self, True)
672
- return formater.get_str(format)
673
-
674
- # static methods
675
-
676
- @staticmethod
677
- def from_datetime(dt):
678
- dt = to_local(dt)
679
- nd = NepaliDate.from_date(dt.date())
680
- return NepaliDateTime(nd.npYear(), nd.npMonth(), nd.npDay(), dt.hour, dt.minute, dt.second)
681
-
682
- @staticmethod
683
- def from_date(date_object):
684
- nepali_date_object = NepaliDate.from_date(date_object)
685
- return NepaliDateTime.from_nepali_date(nepali_date_object)
686
-
687
- @staticmethod
688
- def from_nepali_date(nepali_date_object):
689
- return NepaliDateTime(nepali_date_object.year, nepali_date_object.month, nepali_date_object.day)
690
-
691
- @staticmethod
692
- def now():
693
- return NepaliDateTime.from_datetime(utc_now())
694
-
695
- # property
696
-
697
- @property
698
- def year(self):
699
- return self.__npDate.year
700
-
701
- @property
702
- def month(self):
703
- return self.__npDate.month
704
-
705
- @property
706
- def day(self):
707
- return self.__npDate.day
708
-
709
- @property
710
- def week_day(self):
711
- return self.__npDate.weekDay()
712
-
713
- @property
714
- def hour(self):
715
- return self.__npTime.hour
716
-
717
- @property
718
- def minute(self):
719
- return self.__npTime.minute
720
-
721
- @property
722
- def second(self):
723
- return self.__npTime.second
724
-
725
-
726
- class HumanizeDateTime:
727
- """
728
- HumanizeDate converts NepaliDateTime to nepali human readable form
729
- """
730
-
731
- __past_text = "अघि"
732
- __future_text = "पछि"
733
- __now_text = "भर्खरै"
734
- __year_text = "वर्ष"
735
- __month_text = "महिना"
736
- __day_text = "दिन"
737
- __hour_text = "घण्टा"
738
- __minute_text = "मिनेट"
739
- __second_text = "सेकेन्ड"
740
-
741
- def __init__(self, datetime_obj, *args, **kwargs):
742
- """
743
- initializes humanize class
744
- datetime_obj: python datetime object to be humanized
745
- threshold (kwargs): threshold to be humanize
746
- format (kwargs): format to display behind threshold
747
- """
748
- if type(datetime_obj) == NepaliDateTime:
749
- self.datetime_obj = datetime_obj.to_datetime()
750
- elif type(datetime_obj) == NepaliDate:
751
- self.datetime_obj = NepaliDateTime.from_nepali_date(datetime_obj).to_datetime()
752
- elif type(datetime_obj) == pythonDateTime.date:
753
- self.datetime_obj = NepaliDateTime.from_date(datetime_obj).to_datetime()
754
- elif type(datetime_obj) == pythonDateTime.datetime:
755
- self.datetime_obj = to_local(datetime_obj)
756
- else:
757
- raise InvalidNepaliDateTimeObjectException('Argument must be instance of NepaliDate or NepaliDateTime or datetime.datetime or datetime.date')
758
-
759
- self.threshold = kwargs.get('threshold')
760
- self.format = kwargs.get('format')
761
-
762
- # seconds afer from now to datetime_obj
763
- self.seconds = None
764
-
765
-
766
- def __calc_seconds(self):
767
- """ calculates total seconds from now """
768
-
769
- current_date_time = now()
770
- date = self.datetime_obj
771
- self.seconds = int((current_date_time-date).total_seconds())
772
- self.interval_tense = self.__past_text
773
- if(self.seconds < 0):
774
- self.interval_tense = self.__future_text
775
-
776
- def to_str(self):
777
- """ returns humanize string """
778
-
779
- self.__calc_seconds() # refreshing seconds
780
- seconds = self.seconds
781
- if( seconds < 0):
782
- seconds = 0 - seconds
783
-
784
- if not self.threshold == None:
785
- if( seconds >= self.threshold):
786
- return self.get_datetime().strip()
787
-
788
- return self.get_humanize().strip()
789
-
790
-
791
- def get_humanize(self):
792
- """
793
- returns humanize datetime
794
- """
795
- self.__calc_seconds() # refreshing seconds
796
-
797
- interval_value = 0
798
- interval_text = ""
799
- if( self.seconds == 0 ):
800
- # now
801
- return self.__now_text
802
-
803
- elif( self.seconds < 60):
804
- # seconds
805
- interval_value = self.seconds
806
- interval_text = self.__second_text
807
-
808
- elif( self.seconds < 3600):
809
- # minute
810
- interval_value = self.seconds//60
811
- interval_text = self.__minute_text
812
-
813
- elif( self.seconds < 86400):
814
- # hour
815
- interval_value = self.seconds//3600
816
- interval_text = self.__hour_text
817
-
818
- elif( self.seconds < 2592000):
819
- # day
820
- interval_value = self.seconds//86400
821
- interval_text = self.__day_text
822
-
823
- elif( self.seconds < 946080000):
824
- # month
825
- interval_value = self.seconds//2592000
826
- interval_text = self.__month_text
827
-
828
- else:
829
- # year
830
- interval_value = self.seconds//946080000
831
- interval_text = self.__year_text
832
-
833
- interval_value = NepaliChar.number(interval_value)
834
- return str(interval_value)+' '+str(interval_text)+' '+self.interval_tense
835
-
836
-
837
- def get_datetime(self):
838
- """
839
- returns date in nepali characters
840
- """
841
- if not self.format:
842
- self.format = '%B %d, %Y'
843
- ndt = NepaliDateTime.from_datetime(self.datetime_obj)
844
- return ndt.strftime(self.format)
845
-
846
- def __str__(self):
847
- return self.to_str()
848
-
849
- def __repr__(self):
850
- return str(self)
851
-
852
-
853
- class NepaliDateTimeFormater:
854
- """
855
- NepaliDateTimeFormater: formats nepali datetime to string ( using strftime )
856
- """
857
-
858
- # format according to python's datetime with class method
859
- format_map = {
860
- 'a': 'weekdayHalf',
861
- 'A': 'weekdayFull',
862
- 'w': 'weekdayNumber',
863
- 'd': 'day',
864
- '-d': 'day_nonzero',
865
- 'b': 'monthFull',
866
- 'B': 'monthFull',
867
- 'm': 'monthNumber',
868
- '-m': 'monthNumber_nonzero',
869
- 'y': 'yearHalf',
870
- 'Y': 'yearFull',
871
- 'H': 'hour24',
872
- '-H': 'hour24_nonzero',
873
- 'I': 'hour12',
874
- '-I': 'hour12_nonzero',
875
- 'p': 'ampm',
876
- 'M': 'minute',
877
- '-M': 'minute_nonzero',
878
- 'S': 'second',
879
- '-S': 'second_nonzero',
880
- }
881
-
882
- def __init__(self, datetime_object, english=False):
883
- if type(datetime_object) == NepaliDateTime:
884
- self.npDateTime = datetime_object
885
- elif type(datetime_object) == NepaliDate:
886
- self.npDateTime = datetime_object.to_nepali_datetime()
887
- elif type(datetime_object) == pythonDateTime.date:
888
- self.npDateTime = NepaliDateTime.from_date(datetime_object)
889
- elif type(datetime_object) == pythonDateTime.datetime:
890
- self.npDateTime = NepaliDateTime.from_datetime(datetime_object)
891
- else:
892
- raise InvalidNepaliDateTimeObjectException('Argument must be instance of NepaliDate or NepaliDateTime or datetime.datetime or datetime.date')
893
-
894
-
895
- self.english = english
896
-
897
- def __str__(self):
898
- return str(self.npDateTime)
899
-
900
- def get_str(self, format):
901
- """ generates formated string """
902
- i, n = 0, len(format)
903
- time_str = []
904
- try:
905
- while i < n:
906
- ch = format[i]
907
- i += 1
908
- if ch == '%':
909
- if i < n:
910
- ch = format[i]
911
-
912
- if ch == '%':
913
- # for % character
914
- time_str.append('%')
915
-
916
- elif ch == '-':
917
- # special mid characters eg. "-" for non zero padded values
918
- special_ch = ch
919
- if i+1 < n:
920
- i += 1
921
- ch = format[i]
922
- time_str.append(getattr(self, self.get_format_map(special_ch+ch)))
923
- else:
924
- # mapping % forwarded character
925
- time_str.append(getattr(self, self.get_format_map(ch)))
926
- i += 1
927
- else:
928
- time_str.append(ch)
929
- except InvalidDateFormatException as e:
930
- raise e
931
- except Exception:
932
- raise Exception('Unbale to convert NepaliDateTime to str')
933
- time_str = ''.join(time_str)
934
-
935
- return time_str
936
-
937
- def get_format_map(self, ch):
938
- if ch not in self.format_map:
939
- raise InvalidDateFormatException('Invalid Date format %{}'.format(ch))
940
- return self.format_map.get(ch)
941
-
942
- @property
943
- def weekdayHalf(self):
944
- """
945
- %a
946
- """
947
- if self.english:
948
- return EnglishChar.half_day(self.npDateTime.week_day)
949
- return NepaliChar.half_day(self.npDateTime.week_day)
950
-
951
- @property
952
- def weekdayFull(self):
953
- """
954
- %A
955
- """
956
- if self.english:
957
- return EnglishChar.day(self.npDateTime.week_day)
958
- return NepaliChar.day(self.npDateTime.week_day)
959
-
960
- @property
961
- def weekdayNumber(self):
962
- """
963
- %w
964
- """
965
- if self.english:
966
- return str(self.npDateTime.week_day-1)
967
- return NepaliChar.number(self.npDateTime.week_day-1)
968
-
969
- @property
970
- def day(self):
971
- """
972
- %d
973
- """
974
- day = str(self.npDateTime.day)
975
- if len(day) < 2:
976
- day = '0'+day
977
- if self.english:
978
- return str(day)
979
- return NepaliChar.number(day)
980
-
981
- @property
982
- def day_nonzero(self):
983
- """
984
- %D
985
- """
986
- day = str(self.npDateTime.day)
987
- if self.english:
988
- return str(day)
989
- return NepaliChar.number(day)
990
-
991
- @property
992
- def monthFull(self):
993
- """
994
- %B or %b
995
- """
996
- if self.english:
997
- return EnglishChar.month(self.npDateTime.month)
998
- return NepaliChar.month(self.npDateTime.month)
999
-
1000
- @property
1001
- def monthNumber(self):
1002
- """
1003
- %m
1004
- """
1005
- month = str(self.npDateTime.month)
1006
- if len(month) < 2:
1007
- month = '0'+month
1008
- if self.english:
1009
- return str(month)
1010
- return NepaliChar.number(month)
1011
-
1012
- @property
1013
- def monthNumber_nonzero(self):
1014
- """
1015
- %m
1016
- """
1017
- month = str(self.npDateTime.month)
1018
- if self.english:
1019
- return str(month)
1020
- return NepaliChar.number(month)
1021
-
1022
- @property
1023
- def yearHalf(self):
1024
- """
1025
- %y
1026
- """
1027
- if self.english:
1028
- return str(self.npDateTime.year)[2:]
1029
- return NepaliChar.number(str(self.npDateTime.year)[2:])
1030
-
1031
- @property
1032
- def yearFull(self):
1033
- """
1034
- %Y
1035
- """
1036
- if self.english:
1037
- return str(self.npDateTime.year)
1038
- return NepaliChar.number(self.npDateTime.year)
1039
-
1040
- @property
1041
- def hour24(self):
1042
- """
1043
- %H
1044
- """
1045
- hour = str(self.npDateTime.hour)
1046
- if len(hour) < 2:
1047
- hour = '0'+hour
1048
- if self.english:
1049
- return str(hour)
1050
- return NepaliChar.number(hour)
1051
-
1052
- @property
1053
- def hour24_nonzero(self):
1054
- """
1055
- %H
1056
- """
1057
- hour = self.npDateTime.hour
1058
- if self.english:
1059
- return str(hour)
1060
- return NepaliChar.number(hour)
1061
-
1062
- @property
1063
- def hour12(self):
1064
- """
1065
- %I
1066
- """
1067
- hour = self.npDateTime.hour
1068
- if hour > 12:
1069
- hour = hour - 12
1070
- if hour == 0:
1071
- hour = 12
1072
- hour = str(hour)
1073
- if len(hour) < 2:
1074
- hour = '0'+hour
1075
-
1076
- if self.english:
1077
- return str(hour)
1078
- return NepaliChar.number(hour)
1079
-
1080
- @property
1081
- def hour12_nonzero(self):
1082
- """
1083
- %I
1084
- """
1085
- hour = self.npDateTime.hour
1086
- if hour > 12:
1087
- hour = hour - 12
1088
- if hour == 0:
1089
- hour = 12
1090
- hour = str(hour)
1091
- if self.english:
1092
- return str(hour)
1093
- return NepaliChar.number(hour)
1094
-
1095
- @property
1096
- def ampm(self):
1097
- """
1098
- %p
1099
- """
1100
- if self.english:
1101
- ampm = 'AM'
1102
- if self.npDateTime.hour > 12:
1103
- ampm = 'PM'
1104
- return str(ampm)
1105
-
1106
- ampm = ''
1107
- if self.npDateTime.hour < 12:
1108
- ampm = 'शुभप्रभात'
1109
- elif self.npDateTime.hour >= 12 and self.npDateTime.hour < 18:
1110
- ampm = 'मध्यान्ह'
1111
- else:
1112
- ampm = 'अपरान्ह'
1113
- return str(ampm)
1114
-
1115
- @property
1116
- def minute(self):
1117
- """
1118
- %M
1119
- """
1120
- minute = str(self.npDateTime.minute)
1121
- if len(minute) < 2:
1122
- minute = '0'+minute
1123
- if self.english:
1124
- return str(minute)
1125
- return NepaliChar.number(minute)
1126
-
1127
- @property
1128
- def minute_nonzero(self):
1129
- """
1130
- %M
1131
- """
1132
- minute = str(self.npDateTime.minute)
1133
- if self.english:
1134
- return str(minute)
1135
- return NepaliChar.number(minute)
1136
-
1137
- @property
1138
- def second(self):
1139
- """
1140
- %S
1141
- """
1142
- second = str(self.npDateTime.second)
1143
- if len(second) < 2:
1144
- second = '0'+second
1145
- if self.english:
1146
- return str(second)
1147
- return NepaliChar.number(second)
1148
-
1149
- @property
1150
- def second_nonzero(self):
1151
- """
1152
- %S
1153
- """
1154
- second = str(self.npDateTime.second)
1155
- if self.english:
1156
- return str(second)
1157
- return NepaliChar.number(second)
1158
-
1159
-
1160
- def nepalihumanize(datetime_obj, threshold=None, format=None):
1161
- """ returns to humanize nepalidatetime """
1162
- humanize = HumanizeDateTime(datetime_obj, threshold=threshold, format=format)
1163
- humanize_str = humanize.to_str()
1164
- return humanize_str
1165
-
1166
-
1167
- nepalidate = NepaliDate
1168
- nepalitime = NepaliTime
1169
- nepalidatetime = NepaliDateTime