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
@@ -1,394 +0,0 @@
1
- import time
2
-
3
- class NepaliDateConverter:
4
-
5
- __enMonths = [31, 28, 31, 30, 31, 30,31, 31, 30, 31, 30, 31]
6
- enLeapMonths = [31, 29, 31, 30, 31, 30,31, 31, 30, 31, 30, 31]
7
-
8
- # List of nepali months
9
- __npMonths = [
10
- [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 ], # 2000 BS - 1944 AD
11
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ], # 2001 BS
12
- [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
13
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
14
- [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 ],
15
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
16
- [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
17
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
18
- [ 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31 ],
19
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
20
- [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
21
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
22
- [ 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30 ],
23
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
24
- [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
25
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
26
- [ 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30 ],
27
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
28
- [ 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
29
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 ],
30
- [ 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30 ],
31
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
32
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30 ],
33
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 ],
34
- [ 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30 ],
35
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
36
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
37
- [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 ],
38
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
39
- [ 31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30 ],
40
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
41
- [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 ],
42
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
43
- [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
44
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
45
- [ 30, 32, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31 ],
46
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
47
- [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
48
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
49
- [ 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30 ],
50
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
51
- [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
52
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
53
- [ 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30 ],
54
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
55
- [ 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
56
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
57
- [ 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30 ],
58
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
59
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30 ],
60
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 ],
61
- [ 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30 ],
62
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
63
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30 ],
64
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 ],
65
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
66
- [ 31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30 ],
67
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
68
- [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 ],
69
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
70
- [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
71
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
72
- [ 30, 32, 31, 32, 31, 31, 29, 30, 29, 30, 29, 31 ],
73
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
74
- [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
75
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
76
- [ 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31 ],
77
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
78
- [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
79
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
80
- [ 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30 ],
81
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ], # 2071 BS
82
- [ 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30 ], # 2072 BS
83
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ], # 2073 BS
84
- [ 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30 ],
85
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
86
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30 ],
87
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 ],
88
- [ 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30 ],
89
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
90
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30 ],
91
- [ 31, 31, 32, 32, 31, 30, 30, 30, 29, 30, 30, 30 ],
92
- [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30 ],
93
- [ 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30 ],
94
- [ 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30 ],
95
- [ 31, 32, 31, 32, 30, 31, 30, 30, 29, 30, 30, 30 ],
96
- [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30 ],
97
- [ 31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30 ],
98
- [ 30, 31, 32, 32, 30, 31, 30, 30, 29, 30, 30, 30 ],
99
- [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30 ],
100
- [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30 ], # 2090 BS
101
- [ 31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30 ],
102
- [ 30, 31, 32, 32, 31, 30, 30, 30, 29, 30, 30, 30 ],
103
- [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30 ],
104
- [ 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30 ],
105
- [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 30, 30, 30 ],
106
- [ 30, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
107
- [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30 ],
108
- [ 31, 31, 32, 31, 31, 31, 29, 30, 29, 30, 29, 31 ],
109
- [ 31, 31, 32, 31, 31, 31, 30, 29, 29, 30, 30, 30 ] # 2099 BS - 2042 AD
110
- ]
111
-
112
- def set_current_date(self):
113
- year = int(time.strftime("%Y"))
114
- month = int(time.strftime("%m"))
115
- date = int(time.strftime("%d"))
116
- self.set_english_date(year, month, date)
117
-
118
- # English to Nepali date conversion
119
-
120
- def set_english_date(self,year, month, date):
121
- """
122
- * Sets specific en dates to self object *
123
- Reference np 2000/1/1 with en date 1943/4/14
124
- """
125
- if(not self.__isEnRange(year,month,date)):
126
- raise Exception("Date out of range")
127
-
128
- self.__enYear = year
129
- self.__enMonth = month
130
- self.__enDay = date
131
-
132
- # Setting np reference to 2000/1/1 with en date 1943/4/14
133
- self.__npYear = 2000
134
- self.__npMonth = 1
135
- self.__npDay = 1
136
-
137
- difference = self.enDateDifference(1943, 4, 14)
138
-
139
- # Getting np year until the difference remains less than 365
140
- index = 0
141
- while( difference >= self.__npYearDays(index) ):
142
- self.__npYear+=1
143
- difference = difference - self.__npYearDays(index)
144
- index+=1
145
-
146
- # Getting np month until the difference remains less than 31
147
- i = 0
148
- while(difference >= self.__npMonths[index][i]):
149
- difference = difference - self.__npMonths[index][i]
150
- self.__npMonth+=1
151
- i+=1
152
-
153
- # Remaning days is the date
154
- self.__npDay = self.__npDay + difference
155
-
156
- self.__calculate_week_day()
157
-
158
-
159
- def toEnString(self, format='-'):
160
- return str(self.__enYear)+format+str(self.__enMonth)+format+str(self.__enDay)
161
-
162
-
163
- def enDateDifference(self, year, month, date):
164
- """
165
- returns difference of days from the self date with the date provided
166
- """
167
- difference = self.__countTotalEnDays(self.__enYear, self.__enMonth, self.__enDay) - self.__countTotalEnDays(year, month, date)
168
- if difference < 0:
169
- return -difference
170
- else:
171
- return difference
172
-
173
-
174
- def __countTotalEnDays(self, year, month, date):
175
- """ counts english date in days with 0000-01-01 """
176
-
177
- totalDays = year * 365 + date
178
-
179
- for i in range(0,month-1):
180
- totalDays = totalDays + self.__enMonths[i]
181
-
182
- totalDays = totalDays + self.__countleap(year, month)
183
- return totalDays
184
-
185
-
186
- def __countleap(self, year, month):
187
- """ counts total leap years from year/month to 0000/01 """
188
- if (month <= 2):
189
- year-=1
190
-
191
- return (year//4-year//100+year//400)
192
-
193
-
194
- def __isEnRange(self, year, month, date):
195
- """ checks if english date in within range 1944 - 2042 """
196
- if(year < 1944 or year > 2042):
197
- return False
198
-
199
- if(month < 1 or month > 12):
200
- return False
201
-
202
- if(date < 1 or date > 31):
203
- return False
204
-
205
- return True
206
-
207
-
208
- def __isLeapYear(self, year):
209
- """ independent method to check leap year """
210
- if(year%4 == 0):
211
- if(year%100 == 0):
212
- return (year%400 == 0)
213
- else:
214
- return True
215
- else:
216
- return False
217
-
218
-
219
-
220
- # Nepali to English date conversion
221
-
222
- def set_nepali_date(self, year, month, date):
223
- """
224
- * Sets specific np dates to self object *
225
- Reference en 1994/1/1 with en date 2000/9/17
226
- """
227
- if(not self.__isNpRange(year,month,date)):
228
- raise Exception("Date out of range")
229
-
230
- self.__npYear = year
231
- self.__npMonth = month
232
- self.__npDay = date
233
-
234
- # Setting en reference to 1944/1/1 with np date 2000/9/17
235
- self.__enYear = 1944
236
- self.__enMonth = 1
237
- self.__enDay = 1
238
-
239
- difference = self.npDateDifference(2000, 9, 17)
240
-
241
- # Getting en year until the difference remains less than 365
242
- while( (difference >= 366 and self.__isLeapYear(self.__enYear)) or (difference >= 365 and not(self.__isLeapYear(self.__enYear)) ) ):
243
- if( self.__isLeapYear(self.__enYear) ):
244
- difference -= 366
245
- else:
246
- difference -= 365
247
- self.__enYear += 1
248
-
249
- # Getting en month until the difference remains less than 31
250
- if(self.__isLeapYear(self.__enYear)):
251
- monthDays = self.enLeapMonths
252
- else:
253
- monthDays = self.__enMonths
254
- i = 0
255
- while( difference >= monthDays[i]):
256
- self.__enMonth+=1
257
- difference = difference - monthDays[i]
258
- i+=1
259
-
260
- # Remaning days is the date
261
- self.__enDay = self.__enDay + difference
262
-
263
- self.__calculate_week_day()
264
-
265
-
266
- def toNpString(self, format="-"):
267
- return str(self.npYear())+format+str(self.npMonthStr())+format+str(self.npDayStr())
268
-
269
-
270
- def npDateDifference(self, year, month, date):
271
- """
272
- Getting difference from the current date with the date provided
273
- """
274
- difference = self.__countTotalNpDays(self.__npYear, self.__npMonth, self.__npDay) - self.__countTotalNpDays(year, month, date)
275
- if(difference < 0):
276
- return -difference
277
- else:
278
- return difference
279
-
280
-
281
- def __countTotalNpDays(self, year, month, date):
282
- """ counts nepali date in days with 2000-01-01 (nepali date) """
283
-
284
- total = 0
285
- if(year < 2000):
286
- return 0
287
-
288
- total = total + (date-1)
289
-
290
- yearIndex = year - 2000
291
- for i in range(0,month-1):
292
- total = total + self.__npMonths[yearIndex][i]
293
-
294
- for i in range(0,yearIndex):
295
- total = total + self.__npYearDays(i)
296
-
297
- return total
298
-
299
-
300
- def __npYearDays(self, index):
301
- """
302
- count total days of specific year ( from index)
303
- input: index (year)
304
- return total (days)
305
-
306
- eg, for 2075 => 2075-2000 = 75
307
- __npYearDays(75) => 365 days
308
-
309
- """
310
- total = 0
311
-
312
- for i in range(0,12):
313
- total += self.__npMonths[index][i]
314
-
315
- return total
316
-
317
-
318
- def __isNpRange(self, year, month, date):
319
- """ checks if nepali date is in range 2000-2098 """
320
- if(year < 2000 or year > 2098):
321
- return False
322
-
323
- if(month < 1 or month > 12):
324
- return False
325
-
326
- if(date < 1 or date > self.__npMonths[year-2000][month-1]):
327
- return False
328
-
329
- return True
330
-
331
- def __calculate_week_day(self):
332
- # Reference date 1943/4/14 Wednesday
333
- difference = self.enDateDifference(1943, 4, 14)
334
- self.__week_day = ((3 + (difference%7) ) % 7 ) + 1
335
- return self.__week_day
336
-
337
- # Properties
338
- def weekday(self):
339
- return self.__week_day
340
-
341
- @property
342
- def year_en(self):
343
- return self.__enYear
344
-
345
- @property
346
- def month_en(self):
347
- return self.__enMonth
348
-
349
- @property
350
- def day_en(self):
351
- return self.__enDay
352
-
353
- @property
354
- def year_np(self):
355
- return self.__npYear
356
-
357
- @property
358
- def month_np(self):
359
- return self.__npMonth
360
-
361
- @property
362
- def day_np(self):
363
- return self.__npDay
364
-
365
-
366
- # Class static methods
367
-
368
- @classmethod
369
- def get_converter_object(cls):
370
- if not hasattr(cls, '__converter_Cache'):
371
- cls.__converter_Cache = cls()
372
- return cls.__converter_Cache
373
-
374
- @classmethod
375
- def english_to_nepali(cls, year, month, day):
376
- converter = cls.get_converter_object()
377
- converter.set_english_date(year, month, day)
378
- return converter.year_np, converter.month_np, converter.day_np
379
-
380
- @classmethod
381
- def nepali_to_english(cls, year, month, day):
382
- converter = cls.get_converter_object()
383
- converter.set_nepali_date(year, month, day)
384
- return converter.year_en, converter.month_en, converter.day_en
385
-
386
- @classmethod
387
- def current_english_date(cls):
388
- converter = cls.get_converter_object()
389
- converter.set_current_date()
390
- return converter.year_en, converter.month_en, converter.day_en
391
-
392
- @classmethod
393
- def current_nepali_date(cls):
394
- return cls.english_to_nepali(*cls.current_english_date())
@@ -1,314 +0,0 @@
1
- import datetime as pythonDateTime
2
- from nepali.char import EnglishChar, NepaliChar
3
-
4
- from nepali.exceptions import InvalidDateFormatException, InvalidNepaliDateTimeObjectException
5
-
6
- from ._datetime import nepalidate, nepalidatetime
7
-
8
- class NepaliDateTimeFormater:
9
- """
10
- NepaliDateTimeFormater: formats nepali datetime to string ( using strftime )
11
- """
12
-
13
- # format according to python's datetime with class method
14
- format_map = {
15
- 'a': 'weekdayHalf',
16
- 'A': 'weekdayFull',
17
- 'w': 'weekdayNumber',
18
- 'd': 'day',
19
- '-d': 'day_nonzero',
20
- 'b': 'monthFull',
21
- 'B': 'monthFull',
22
- 'm': 'monthNumber',
23
- '-m': 'monthNumber_nonzero',
24
- 'y': 'yearHalf',
25
- 'Y': 'yearFull',
26
- 'H': 'hour24',
27
- '-H': 'hour24_nonzero',
28
- 'I': 'hour12',
29
- '-I': 'hour12_nonzero',
30
- 'p': 'ampm',
31
- 'M': 'minute',
32
- '-M': 'minute_nonzero',
33
- 'S': 'second',
34
- '-S': 'second_nonzero',
35
- }
36
-
37
- def __init__(self, datetime_object, english=False):
38
- if type(datetime_object) == nepalidatetime:
39
- self.npDateTime = datetime_object
40
- elif type(datetime_object) == nepalidate:
41
- self.npDateTime = datetime_object.to_nepalidatetime()
42
- elif type(datetime_object) == pythonDateTime.date:
43
- self.npDateTime = nepalidatetime.from_date(datetime_object)
44
- elif type(datetime_object) == pythonDateTime.datetime:
45
- self.npDateTime = nepalidatetime.from_datetime(datetime_object)
46
- else:
47
- raise InvalidNepaliDateTimeObjectException('Argument must be instance of nepalidate or nepalidatetime or datetime.datetime or datetime.date')
48
-
49
-
50
- self.english = english
51
-
52
- def __str__(self):
53
- return str(self.npDateTime)
54
-
55
- def get_str(self, format):
56
- """ generates formated string """
57
- i, n = 0, len(format)
58
- time_str = []
59
- try:
60
- while i < n:
61
- ch = format[i]
62
- i += 1
63
- if ch == '%':
64
- if i < n:
65
- ch = format[i]
66
-
67
- if ch == '%':
68
- # for % character
69
- time_str.append('%')
70
-
71
- elif ch == '-':
72
- # special mid characters eg. "-" for non zero padded values
73
- special_ch = ch
74
- if i+1 < n:
75
- i += 1
76
- ch = format[i]
77
- time_str.append(getattr(self, self.get_format_map(special_ch+ch)))
78
- else:
79
- # mapping % forwarded character
80
- time_str.append(getattr(self, self.get_format_map(ch)))
81
- i += 1
82
- else:
83
- time_str.append(ch)
84
- except InvalidDateFormatException as e:
85
- raise e
86
- except Exception:
87
- raise Exception('Unable to convert NepaliDateTime to str')
88
- time_str = ''.join(time_str)
89
-
90
- return time_str
91
-
92
- def get_format_map(self, ch):
93
- if ch not in self.format_map:
94
- raise InvalidDateFormatException('Invalid Date format %{}'.format(ch))
95
- return self.format_map.get(ch)
96
-
97
- @property
98
- def weekdayHalf(self):
99
- """
100
- %a
101
- """
102
- if self.english:
103
- return EnglishChar.half_day(self.npDateTime.weekday() + 1)
104
- return NepaliChar.half_day(self.npDateTime.weekday() + 1)
105
-
106
- @property
107
- def weekdayFull(self):
108
- """
109
- %A
110
- """
111
- if self.english:
112
- return EnglishChar.day(self.npDateTime.weekday() + 1)
113
- return NepaliChar.day(self.npDateTime.weekday() + 1)
114
-
115
- @property
116
- def weekdayNumber(self):
117
- """
118
- %w
119
- """
120
- if self.english:
121
- return str(self.npDateTime.weekday())
122
- return NepaliChar.number(self.npDateTime.weekday())
123
-
124
- @property
125
- def day(self):
126
- """
127
- %d
128
- """
129
- day = str(self.npDateTime.day)
130
- if len(day) < 2:
131
- day = '0'+day
132
- if self.english:
133
- return str(day)
134
- return NepaliChar.number(day)
135
-
136
- @property
137
- def day_nonzero(self):
138
- """
139
- %D
140
- """
141
- day = str(self.npDateTime.day)
142
- if self.english:
143
- return str(day)
144
- return NepaliChar.number(day)
145
-
146
- @property
147
- def monthFull(self):
148
- """
149
- %B or %b
150
- """
151
- if self.english:
152
- return EnglishChar.month(self.npDateTime.month)
153
- return NepaliChar.month(self.npDateTime.month)
154
-
155
- @property
156
- def monthNumber(self):
157
- """
158
- %m
159
- """
160
- month = str(self.npDateTime.month)
161
- if len(month) < 2:
162
- month = '0'+month
163
- if self.english:
164
- return str(month)
165
- return NepaliChar.number(month)
166
-
167
- @property
168
- def monthNumber_nonzero(self):
169
- """
170
- %m
171
- """
172
- month = str(self.npDateTime.month)
173
- if self.english:
174
- return str(month)
175
- return NepaliChar.number(month)
176
-
177
- @property
178
- def yearHalf(self):
179
- """
180
- %y
181
- """
182
- if self.english:
183
- return str(self.npDateTime.year)[2:]
184
- return NepaliChar.number(str(self.npDateTime.year)[2:])
185
-
186
- @property
187
- def yearFull(self):
188
- """
189
- %Y
190
- """
191
- if self.english:
192
- return str(self.npDateTime.year)
193
- return NepaliChar.number(self.npDateTime.year)
194
-
195
- @property
196
- def hour24(self):
197
- """
198
- %H
199
- """
200
- hour = str(self.npDateTime.hour)
201
- if len(hour) < 2:
202
- hour = '0'+hour
203
- if self.english:
204
- return str(hour)
205
- return NepaliChar.number(hour)
206
-
207
- @property
208
- def hour24_nonzero(self):
209
- """
210
- %H
211
- """
212
- hour = self.npDateTime.hour
213
- if self.english:
214
- return str(hour)
215
- return NepaliChar.number(hour)
216
-
217
- @property
218
- def hour12(self):
219
- """
220
- %I
221
- """
222
- hour = self.npDateTime.hour
223
- if hour > 12:
224
- hour = hour - 12
225
- if hour == 0:
226
- hour = 12
227
- hour = str(hour)
228
- if len(hour) < 2:
229
- hour = '0'+hour
230
-
231
- if self.english:
232
- return str(hour)
233
- return NepaliChar.number(hour)
234
-
235
- @property
236
- def hour12_nonzero(self):
237
- """
238
- %I
239
- """
240
- hour = self.npDateTime.hour
241
- if hour > 12:
242
- hour = hour - 12
243
- if hour == 0:
244
- hour = 12
245
- hour = str(hour)
246
- if self.english:
247
- return str(hour)
248
- return NepaliChar.number(hour)
249
-
250
- @property
251
- def ampm(self):
252
- """
253
- %p
254
- """
255
- if self.english:
256
- ampm = 'AM'
257
- if self.npDateTime.hour > 12:
258
- ampm = 'PM'
259
- return str(ampm)
260
-
261
- ampm = ''
262
- if self.npDateTime.hour < 12:
263
- ampm = 'शुभप्रभात'
264
- elif self.npDateTime.hour >= 12 and self.npDateTime.hour < 18:
265
- ampm = 'मध्यान्ह'
266
- else:
267
- ampm = 'अपरान्ह'
268
- return str(ampm)
269
-
270
- @property
271
- def minute(self):
272
- """
273
- %M
274
- """
275
- minute = str(self.npDateTime.minute)
276
- if len(minute) < 2:
277
- minute = '0'+minute
278
- if self.english:
279
- return str(minute)
280
- return NepaliChar.number(minute)
281
-
282
- @property
283
- def minute_nonzero(self):
284
- """
285
- %M
286
- """
287
- minute = str(self.npDateTime.minute)
288
- if self.english:
289
- return str(minute)
290
- return NepaliChar.number(minute)
291
-
292
- @property
293
- def second(self):
294
- """
295
- %S
296
- """
297
- second = str(self.npDateTime.second)
298
- if len(second) < 2:
299
- second = '0'+second
300
- if self.english:
301
- return str(second)
302
- return NepaliChar.number(second)
303
-
304
- @property
305
- def second_nonzero(self):
306
- """
307
- %S
308
- """
309
- second = str(self.npDateTime.second)
310
- if self.english:
311
- return str(second)
312
- return NepaliChar.number(second)
313
-
314
-