-
Notifications
You must be signed in to change notification settings - Fork 18
/
jdatetime.py
1240 lines (982 loc) · 33.3 KB
/
jdatetime.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import sys
from datetime import date, timedelta, tzinfo, time as _time, datetime as dt
import operator
from persiantools import digits, utils, PY2
MINYEAR = 1
MAXYEAR = 9377
_MAXORDINAL = 3424878
MONTH_NAMES_EN = [
None,
"Farvardin",
"Ordibehesht",
"Khordad",
"Tir",
"Mordad",
"Shahrivar",
"Mehr",
"Aban",
"Azar",
"Dey",
"Bahman",
"Esfand",
]
MONTH_NAMES_FA = [
None,
"فروردین",
"اردیبهشت",
"خرداد",
"تیر",
"مرداد",
"شهریور",
"مهر",
"آبان",
"آذر",
"دی",
"بهمن",
"اسفند",
]
MONTH_NAMES_ABBR_EN = [
None,
"Far",
"Ord",
"Kho",
"Tir",
"Mor",
"Sha",
"Meh",
"Aba",
"Aza",
"Dey",
"Bah",
"Esf",
]
MONTH_NAMES_ABBR_FA = [
None,
"فرو",
"ارد",
"خرد",
"تیر",
"مرد",
"شهر",
"مهر",
"آبا",
"آذر",
"دی",
"بهم",
"اسف",
]
WEEKDAY_NAMES_EN = [
"Shanbeh",
"Yekshanbeh",
"Doshanbeh",
"Seshanbeh",
"Chaharshanbeh",
"Panjshanbeh",
"Jomeh",
]
WEEKDAY_NAMES_FA = ["شنبه", "یکشنبه", "دوشنبه", "سهشنبه", "چهارشنبه", "پنجشنبه", "جمعه"]
WEEKDAY_NAMES_ABBR_EN = ["Sha", "Yek", "Dos", "Ses", "Cha", "Pan", "Jom"]
WEEKDAY_NAMES_ABBR_FA = ["ش", "ی", "د", "س", "چ", "پ", "ج"]
_MONTH_COUNT = [
[-1, -1, -1], # for indexing purposes
[31, 31, 0], # farvardin
[31, 31, 31], # ordibehesht
[31, 31, 62], # khordad
[31, 31, 93], # tir
[31, 31, 124], # mordad
[31, 31, 155], # shahrivar
[30, 30, 186], # mehr
[30, 30, 216], # aban
[30, 30, 246], # azar
[30, 30, 276], # dey
[30, 30, 306], # bahman
[29, 30, 336], # esfand
]
class JalaliDate(object):
__slots__ = "_year", "_month", "_day", "_locale", "_hashcode"
def __init__(self, year, month=None, day=None, locale="en"):
if isinstance(year, JalaliDate) and month is None:
month = year.month
day = year.day
locale = year.locale
year = year.year
elif isinstance(year, date):
jdate = self.to_jalali(year)
year, month, day = jdate.year, jdate.month, jdate.day
elif (isinstance(year, bytes) and len(year) == 4 and 1 <= year[2] <= 12) or (
isinstance(year, str) and year.startswith("[", 0, 1)
):
if PY2:
import ast
self.__setstate__(ast.literal_eval(year))
else:
self.__setstate__(year)
year = self._year
month = self._month
day = self._day
year, month, day, locale = self._check_date_fields(year, month, day, locale)
self._year = year
self._month = month
self._day = day
self._locale = locale
self._hashcode = -1
@property
def year(self):
return self._year
@property
def month(self):
return self._month
@property
def day(self):
return self._day
@property
def locale(self):
return self._locale
@locale.setter
def locale(self, locale):
assert locale in ("en", "fa"), "locale must be 'en' or 'fa'"
self._locale = locale
@classmethod
def _check_date_fields(cls, year, month, day, locale):
year = operator.index(year)
month = operator.index(month)
day = operator.index(day)
if not MINYEAR <= year <= MAXYEAR:
raise ValueError("year must be in %d..%d" % (MINYEAR, MAXYEAR), year)
if not 1 <= month <= 12:
raise ValueError("month must be in 1..12", month)
dim = cls.days_in_month(month, year)
if not 1 <= day <= dim:
raise ValueError("day must be in 1..%d" % dim, day)
if locale not in ["en", "fa"]:
raise ValueError("locale must be 'en' or 'fa'")
return year, month, day, locale
@classmethod
def chack_date(cls, year, month, day):
try:
cls._check_date_fields(year, month, day, "en")
except (ValueError, TypeError):
return False
else:
return True
@staticmethod
def is_leap(year):
assert MINYEAR <= year <= MAXYEAR
c = 0.24219858156028368 # 683 / 2820
# return ((year + 2346) * 683) % 2820 < 683
return ((year + 2346) * c) % 1 < c
@classmethod
def days_in_month(cls, month, year):
assert 1 <= month <= 12, "month must be in 1..12"
if month == 12 and cls.is_leap(year):
return _MONTH_COUNT[month][1]
return _MONTH_COUNT[month][0]
@staticmethod
def days_before_month(month):
assert 1 <= month <= 12, "month must be in 1..12"
return _MONTH_COUNT[month][2]
@classmethod
def to_jalali(cls, year, month=None, day=None):
"""based on jdf.scr.ir"""
if month is None and isinstance(year, date):
month = year.month
day = year.day
year = year.year
g_d_m = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
jy = 0 if year <= 1600 else 979
year -= 621 if year <= 1600 else 1600
year2 = year + 1 if month > 2 else year
days = (365 * year) + int((year2 + 3) / 4) - int((year2 + 99) / 100)
days += int((year2 + 399) / 400) - 80 + day + g_d_m[month - 1]
jy += 33 * int(days / 12053)
days %= 12053
jy += 4 * int(days / 1461)
days %= 1461
jy += int((days - 1) / 365)
if days > 365:
days = (days - 1) % 365
if days < 186:
jm = 1 + int(days / 31)
jd = 1 + (days % 31)
else:
arit = days - 186
jm = 7 + int(arit / 30)
jd = 1 + (arit % 30)
return cls(jy, jm, jd)
def to_gregorian(self):
"""based on jdf.scr.ir"""
month = self.month
day = self.day
year = self.year
gy = 621 if year <= 979 else 1600
year -= 0 if year <= 979 else 979
d = (month - 1) * 31 if month < 7 else ((month - 7) * 30) + 186
days = (365 * year) + (int(year / 33) * 8) + int(((year % 33) + 3) / 4)
days += 78 + day + d
gy += 400 * int(days / 146097)
days %= 146097
if days > 36524:
days -= 1
gy += 100 * int(days / 36524)
days %= 36524
if days >= 365:
days += 1
gy += 4 * int(days / 1461)
days %= 1461
gy += int((days - 1) / 365)
if days > 365:
days = (days - 1) % 365
gd = days + 1
g_d_m = [
0,
31,
29 if (gy % 4 == 0 and gy % 100 != 0) or gy % 400 == 0 else 28,
31,
30,
31,
30,
31,
31,
30,
31,
30,
31,
]
gm = 0
for gm, g in enumerate(g_d_m):
if gd <= g:
break
gd -= g
return date(gy, gm, gd)
@classmethod
def today(cls):
return cls(date.today())
def timetuple(self):
"Return local time tuple compatible with time.localtime()."
return self.to_gregorian().timetuple()
def isoformat(self):
iso = "%04d-%02d-%02d" % (self._year, self._month, self._day)
if self._locale == "fa":
iso = digits.en_to_fa(iso)
return iso
__str__ = isoformat
def toordinal(self):
return self.to_gregorian().toordinal() - 226894
@classmethod
def fromordinal(cls, n):
return cls(date.fromordinal(n + 226894))
@classmethod
def fromisoformat(cls, date_string):
"""Construct a date from the output of JalaliDate.isoformat()."""
if not isinstance(date_string, str):
raise TypeError("fromisoformat: argument must be str")
return cls(*cls._parse_isoformat_date(digits.fa_to_en(date_string)))
@classmethod
def _parse_isoformat_date(cls, dtstr):
# It is assumed that this function will only be called with a
# string of length exactly 10, and (though this is not used) ASCII-only
year = int(dtstr[0:4])
if dtstr[4] != "-":
raise ValueError("Invalid date separator: %s" % dtstr[4])
month = int(dtstr[5:7])
if dtstr[7] != "-":
raise ValueError("Invalid date separator")
day = int(dtstr[8:10])
return [year, month, day]
def __hash__(self):
if self._hashcode == -1:
self._hashcode = hash(self.__getstate__())
return self._hashcode
def __getstate__(self):
yhi, ylo = divmod(self._year, 256)
return (bytes([yhi, ylo, self._month, self._day]),)
def __setstate__(self, string):
if len(string) != 4 or not (1 <= string[2] <= 12):
raise TypeError("not enough arguments")
yhi, ylo, self._month, self._day = string
self._year = yhi * 256 + ylo
def __reduce__(self):
return self.__class__, self.__getstate__()
def __repr__(self):
return "JalaliDate(%d, %d, %d, %s)" % (
self._year,
self._month,
self._day,
WEEKDAY_NAMES_EN[self.weekday()],
)
resolution = timedelta(1)
def replace(self, year=None, month=None, day=None, locale=None):
if year is None:
year = self._year
if month is None:
month = self._month
if day is None:
day = self._day
if locale is None:
locale = self._locale
return JalaliDate(year, month, day, locale)
@classmethod
def fromtimestamp(cls, timestamp):
return cls(date.fromtimestamp(timestamp))
def weekday(self):
return (self.toordinal() + 4) % 7
def __format__(self, fmt):
if not isinstance(fmt, str):
raise TypeError("must be str, not %s" % type(fmt).__name__)
if len(fmt) != 0:
return self.strftime(fmt)
return str(self)
def isoweekday(self):
return self.weekday() + 1
def week_of_year(self):
o = JalaliDate(self._year, 1, 1).weekday()
days = self.days_before_month(self._month) + self._day + o
week_no, r = divmod(days, 7)
if r > 0:
week_no += 1
return week_no
def isocalendar(self):
"""Return a 3-tuple containing ISO year, week number, and weekday."""
return self.year, self.week_of_year(), self.isoweekday()
def ctime(self):
return self.strftime("%c")
def strftime(self, fmt, locale=None):
if locale is None or locale not in ["fa", "en"]:
locale = self._locale
month_names = MONTH_NAMES_EN if locale == "en" else MONTH_NAMES_FA
month_names_abbr = MONTH_NAMES_ABBR_EN if locale == "en" else MONTH_NAMES_ABBR_FA
day_names = WEEKDAY_NAMES_EN if locale == "en" else WEEKDAY_NAMES_FA
day_names_abbr = WEEKDAY_NAMES_ABBR_EN if locale == "en" else WEEKDAY_NAMES_ABBR_FA
am = "AM" if locale == "en" else "ق.ظ"
format_time = {
"%a": day_names_abbr[self.weekday()],
"%A": day_names[self.weekday()],
"%w": str(self.weekday()),
"%d": "%02d" % self._day,
"%b": month_names_abbr[self._month],
"%B": month_names[self._month],
"%m": "%02d" % self._month,
"%y": "%02d" % (self._year % 100),
"%Y": "%04d" % self._year,
"%H": "00",
"%I": "00",
"%p": am,
"%M": "00",
"%S": "00",
"%f": "000000",
"%z": "",
"%Z": "",
"%j": "%03d" % (self.days_before_month(self._month) + self._day),
"%U": "%02d" % self.week_of_year(),
"%W": "%02d" % self.week_of_year(),
"%X": "00:00:00",
"%%": "%",
}
if "%c" in fmt:
fmt = utils.replace(fmt, {"%c": self.strftime("%A %d %B %Y")})
if "%x" in fmt:
fmt = utils.replace(fmt, {"%x": self.strftime("%y/%m/%d")})
result = utils.replace(fmt, format_time)
if locale == "fa":
result = digits.en_to_fa(result)
return result
def _compare(self, other):
assert isinstance(other, JalaliDate)
y, m, d = self._year, self._month, self._day
y2, m2, d2 = other.year, other.month, other.day
return 0 if (y, m, d) == (y2, m2, d2) else 1 if (y, m, d) > (y2, m2, d2) else -1
def __eq__(self, other):
if isinstance(other, JalaliDate):
return self._compare(other) == 0
elif isinstance(other, date):
return self._compare(JalaliDate(other)) == 0
raise NotImplementedError
def __ne__(self, other):
if isinstance(other, JalaliDate):
return self._compare(other) != 0
elif isinstance(other, date):
return self._compare(JalaliDate(other)) != 0
raise NotImplementedError
def __le__(self, other):
if isinstance(other, JalaliDate):
return self._compare(other) <= 0
elif isinstance(other, date):
return self._compare(JalaliDate(other)) <= 0
raise NotImplementedError
def __lt__(self, other):
if isinstance(other, JalaliDate):
return self._compare(other) < 0
elif isinstance(other, date):
return self._compare(JalaliDate(other)) < 0
raise NotImplementedError
def __ge__(self, other):
if isinstance(other, JalaliDate):
return self._compare(other) >= 0
elif isinstance(other, date):
return self._compare(JalaliDate(other)) >= 0
raise NotImplementedError
def __gt__(self, other):
if isinstance(other, JalaliDate):
return self._compare(other) > 0
elif isinstance(other, date):
return self._compare(JalaliDate(other)) > 0
raise NotImplementedError
def __add__(self, other):
"Add a date to a timedelta."
if isinstance(other, timedelta):
o = self.toordinal() + other.days
if 0 < o <= _MAXORDINAL:
return JalaliDate.fromordinal(o)
raise OverflowError("result out of range")
raise NotImplementedError
__radd__ = __add__
def __sub__(self, other):
"""Subtract two JalaliDates/dates, or a JalaliDate/date and a timedelta."""
if isinstance(other, timedelta):
return self + timedelta(-other.days)
if isinstance(other, JalaliDate):
days1 = self.toordinal()
days2 = other.toordinal()
return timedelta(days1 - days2)
if isinstance(other, date):
days1 = self.toordinal()
days2 = JalaliDate(other).toordinal()
return timedelta(days1 - days2)
raise NotImplementedError
@classmethod
def strptime(cls, data_string, fmt):
raise NotImplementedError
_tzinfo_class = tzinfo
class JalaliDateTime(JalaliDate):
__slots__ = JalaliDate.__slots__ + ("_hour", "_minute", "_second", "_microsecond", "_tzinfo")
def __init__(
self,
year,
month=None,
day=None,
hour=0,
minute=0,
second=0,
microsecond=0,
tzinfo=None,
locale="en",
):
# Pickle support
if isinstance(year, JalaliDateTime) and month is None:
month = year.month
day = year.day
hour = year.hour
minute = year.minute
second = year.second
microsecond = year.microsecond
year = year.year
elif isinstance(year, dt) and month is None:
j = JalaliDate(year.date())
month = j.month
day = j.day
hour = year.hour
minute = year.minute
second = year.second
microsecond = year.microsecond
if tzinfo is None:
tzinfo = year.tzinfo
year = j.year
elif (isinstance(year, bytes) and len(year) == 10) or (
isinstance(year, str) and year.startswith("[", 0, 1)
):
if PY2:
import ast
self.__setstate__(ast.literal_eval(year), month)
else:
self.__setstate__(year, month)
year = self._year
month = self._month
day = self._day
hour = self._hour
minute = self._minute
second = self._second
microsecond = self._microsecond
tzinfo = self._tzinfo
super(JalaliDateTime, self).__init__(year, month, day, locale)
self._check_tzinfo_arg(tzinfo)
self._check_time_fields(hour, minute, second, microsecond)
self._hour = hour
self._minute = minute
self._second = second
self._microsecond = microsecond
self._tzinfo = tzinfo
@staticmethod
def _check_time_fields(hour, minute, second, microsecond):
if not isinstance(hour, int):
raise TypeError("int expected")
if not 0 <= hour <= 23:
raise ValueError("hour must be in 0..23", hour)
if not 0 <= minute <= 59:
raise ValueError("minute must be in 0..59", minute)
if not 0 <= second <= 59:
raise ValueError("second must be in 0..59", second)
if not 0 <= microsecond <= 999999:
raise ValueError("microsecond must be in 0..999999", microsecond)
@property
def hour(self):
return self._hour
@property
def minute(self):
return self._minute
@property
def second(self):
return self._second
@property
def microsecond(self):
return self._microsecond
@property
def tzinfo(self):
return self._tzinfo
@classmethod
def fromtimestamp(cls, t, tz=None):
return cls(dt.fromtimestamp(t, tz))
@classmethod
def utcfromtimestamp(cls, t):
return cls(dt.utcfromtimestamp(t))
def date(self):
return JalaliDate(self.year, self.month, self.day).to_gregorian()
def jalali_date(self):
return JalaliDate(self.year, self.month, self.day)
def jdate(self):
return self.jalali_date()
def time(self):
return _time(self.hour, self.minute, self.second, self.microsecond)
def timetz(self):
return _time(self.hour, self.minute, self.second, self.microsecond, self.tzinfo)
def replace(
self,
year=None,
month=None,
day=None,
hour=None,
minute=None,
second=None,
microsecond=None,
tzinfo=True,
locale=None,
):
if year is None:
year = self.year
if month is None:
month = self.month
if day is None:
day = self.day
if hour is None:
hour = self.hour
if minute is None:
minute = self.minute
if second is None:
second = self.second
if microsecond is None:
microsecond = self.microsecond
if tzinfo is True:
tzinfo = self.tzinfo
if locale is None:
locale = self.locale
self._check_date_fields(year, month, day, locale)
self._check_time_fields(hour, minute, second, microsecond)
self._check_tzinfo_arg(tzinfo)
return JalaliDateTime(year, month, day, hour, minute, second, microsecond, tzinfo, locale)
@classmethod
def now(cls, tz=None):
return cls(dt.now(tz))
@classmethod
def today(cls):
return cls.now()
@classmethod
def utcnow(cls):
return cls(dt.utcnow())
@staticmethod
def _check_tzinfo_arg(tz):
if tz is not None and not isinstance(tz, tzinfo):
raise TypeError("tzinfo argument must be None or of a tzinfo subclass")
@classmethod
def combine(cls, jdate, time_v):
if not isinstance(jdate, JalaliDate):
raise TypeError("date argument must be a JalaliDate instance")
if not isinstance(time_v, _time):
raise TypeError("time argument must be a time instance")
return cls(
jdate.year,
jdate.month,
jdate.day,
time_v.hour,
time_v.minute,
time_v.second,
time_v.microsecond,
time_v.tzinfo,
)
def timestamp(self):
if sys.version_info >= (3, 3):
return self.to_gregorian().timestamp()
else:
gregorian_dt = self.to_gregorian()
return (gregorian_dt - dt(1970, 1, 1, tzinfo=gregorian_dt.tzinfo)).total_seconds()
def utctimetuple(self):
"Return UTC time tuple compatible with time.gmtime()."
return self.to_gregorian().utctimetuple()
def astimezone(self, tz=None):
return JalaliDateTime(self.to_gregorian().astimezone(tz))
def ctime(self):
month_names = MONTH_NAMES_EN if self.locale == "en" else MONTH_NAMES_FA
day_names = WEEKDAY_NAMES_EN if self.locale == "en" else WEEKDAY_NAMES_FA
c = "%s %02d %s %d %02d:%02d:%02d" % (
day_names[self.weekday()],
self.day,
month_names[self.month],
self.year,
self.hour,
self.minute,
self.second,
)
if self.locale == "fa":
c = digits.en_to_fa(c)
return c
def isoformat(self, sep="T"):
s = "%04d-%02d-%02d%c%02d:%02d:%02d" % (
self.year,
self.month,
self.day,
sep,
self.hour,
self.minute,
self.second,
)
if self.microsecond:
s += ".%06d" % self.microsecond
off = self.utcoffset()
if off is not None:
if off.days < 0:
sign = "-"
off = -off
else:
sign = "+"
hh, mm = divmod(off.total_seconds(), timedelta(hours=1).total_seconds())
assert not mm % timedelta(minutes=1).total_seconds(), "whole minute"
mm //= timedelta(minutes=1).total_seconds()
s += "%s%02d:%02d" % (sign, hh, mm)
return s
def utcoffset(self):
if self._tzinfo is None:
return None
offset = self._tzinfo.utcoffset(self.to_gregorian())
return offset
def tzname(self):
if self._tzinfo is None:
return None
name = getattr(self._tzinfo, "tzname")(self.to_gregorian())
if name is not None and not isinstance(name, str):
raise TypeError("tzinfo.tzname() must return None or string, " "not '%s'" % type(name))
return name
def dst(self):
if self._tzinfo is None:
return None
offset = self._tzinfo.dst(self.to_gregorian())
self.check_utc_offset("dst", offset)
return offset
@staticmethod
def check_utc_offset(name, offset):
assert name in ("utcoffset", "dst")
if offset is None:
return
if not isinstance(offset, timedelta):
raise TypeError(
"tzinfo.%s() must return None " "or timedelta, not '%s'" % (name, type(offset))
)
if offset % timedelta(minutes=1) or offset.microseconds:
raise ValueError(
"tzinfo.%s() must return a whole number " "of minutes, got %s" % (name, offset)
)
if not -timedelta(1) < offset < timedelta(1):
raise ValueError(
"%s()=%s, must be must be strictly between"
" -timedelta(hours=24) and timedelta(hours=24)" % (name, offset)
)
@classmethod
def to_jalali(
cls,
year,
month=None,
day=None,
hour=None,
minute=None,
second=None,
microsecond=None,
tzinfo=None,
):
if month is None and isinstance(year, dt):
month = year.month
day = year.day
hour = year.hour
minute = year.minute
second = year.second
microsecond = year.microsecond
tzinfo = year.tzinfo
year = year.year
j_date = JalaliDate.to_jalali(year, month, day)
return cls.combine(
j_date,
_time(hour=hour, minute=minute, second=second, microsecond=microsecond, tzinfo=tzinfo),
)
def to_gregorian(self):
g_date = super(JalaliDateTime, self).to_gregorian()
return dt.combine(
g_date,
_time(
hour=self._hour,
minute=self._minute,
second=self._second,
microsecond=self._microsecond,
tzinfo=self._tzinfo,
),
)
@classmethod
def strptime(cls, data_string, fmt):
raise NotImplementedError
def __repr__(self):
"""Convert to formal string, for repr()."""
l = [
self._year,
self._month,
self._day, # These are never zero
self._hour,
self._minute,
self._second,
self._microsecond,
]
if l[-1] == 0:
del l[-1]
if l[-1] == 0:
del l[-1]
s = ", ".join(map(str, l))
s = "%s(%s)" % ("JalaliDateTime", s)
if self._tzinfo is not None:
assert s[-1:] == ")"
s = s[:-1] + ", tzinfo=%r" % self._tzinfo + ")"
return s
def __str__(self):
return self.isoformat(sep=" ")
def strftime(self, fmt):
datetime = self.to_gregorian()
format_time = {
"%H": "%02d" % self._hour,
"%I": "%02d" % (self._hour if self._hour <= 12 else self._hour - 12),
"%p": "AM" if self._hour < 12 else "PM",
"%M": "%02d" % self._minute,
"%S": "%02d" % self._second,
"%f": "%06d" % self._microsecond,
"%z": datetime.strftime("%z"),
"%Z": ("" if not self._tzinfo else self._tzinfo.tzname(self)),
"%X": "%02d:%02d:%02d" % (self._hour, self._minute, self._second),
}
if "%c" in fmt:
fmt = utils.replace(fmt, {"%c": self.strftime("%A %d %B %Y %X")})
result = utils.replace(fmt, format_time)