Mercurial > cpython-lru_cache-weakref
annotate Lib/datetime.py @ 86127:a3d09c19bb93
merge with 3.3
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Sun, 06 Oct 2013 19:24:00 +0200 |
| parents | 6ec5d188c4db |
| children |
| rev | line source |
|---|---|
|
83300
70f9f6752d28
#17571: remove broken links in datetime.py docstring.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
79136
diff
changeset
|
1 """Concrete date/time and related types. |
|
63184
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
2 |
|
83300
70f9f6752d28
#17571: remove broken links in datetime.py docstring.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
79136
diff
changeset
|
3 See http://www.iana.org/time-zones/repository/tz-link.html for |
|
70f9f6752d28
#17571: remove broken links in datetime.py docstring.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
79136
diff
changeset
|
4 time zone and DST data sources. |
|
63184
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
5 """ |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
6 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
7 import time as _time |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
8 import math as _math |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
9 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
10 def _cmp(x, y): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
11 return 0 if x == y else 1 if x > y else -1 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
12 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
13 MINYEAR = 1 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
14 MAXYEAR = 9999 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
15 _MAXORDINAL = 3652059 # date.max.toordinal() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
16 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
17 # Utility functions, adapted from Python's Demo/classes/Dates.py, which |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
18 # also assumes the current Gregorian calendar indefinitely extended in |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
19 # both directions. Difference: Dates.py calls January 1 of year 0 day |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
20 # number 1. The code here calls January 1 of year 1 day number 1. This is |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
21 # to match the definition of the "proleptic Gregorian" calendar in Dershowitz |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
22 # and Reingold's "Calendrical Calculations", where it's the base calendar |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
23 # for all computations. See the book for algorithms for converting between |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
24 # proleptic Gregorian ordinals and many other calendar systems. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
25 |
| 85459 | 26 # -1 is a placeholder for indexing purposes. |
|
85458
a6df3f045e8b
make lists uniformly integers for the benefit of pypy
Benjamin Peterson <benjamin@python.org>
parents:
85419
diff
changeset
|
27 _DAYS_IN_MONTH = [-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] |
|
63184
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
28 |
| 85459 | 29 _DAYS_BEFORE_MONTH = [-1] # -1 is a placeholder for indexing purposes. |
|
63184
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
30 dbm = 0 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
31 for dim in _DAYS_IN_MONTH[1:]: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
32 _DAYS_BEFORE_MONTH.append(dbm) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
33 dbm += dim |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
34 del dbm, dim |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
35 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
36 def _is_leap(year): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
37 "year -> 1 if leap year, else 0." |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
38 return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
39 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
40 def _days_before_year(year): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
41 "year -> number of days before January 1st of year." |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
42 y = year - 1 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
43 return y*365 + y//4 - y//100 + y//400 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
44 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
45 def _days_in_month(year, month): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
46 "year, month -> number of days in that month in that year." |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
47 assert 1 <= month <= 12, month |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
48 if month == 2 and _is_leap(year): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
49 return 29 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
50 return _DAYS_IN_MONTH[month] |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
51 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
52 def _days_before_month(year, month): |
|
85221
e07f104133d5
#18705: fix a number of typos. Patch by Févry Thibault.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
83300
diff
changeset
|
53 "year, month -> number of days in year preceding first day of month." |
|
63184
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
54 assert 1 <= month <= 12, 'month must be in 1..12' |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
55 return _DAYS_BEFORE_MONTH[month] + (month > 2 and _is_leap(year)) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
56 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
57 def _ymd2ord(year, month, day): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
58 "year, month, day -> ordinal, considering 01-Jan-0001 as day 1." |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
59 assert 1 <= month <= 12, 'month must be in 1..12' |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
60 dim = _days_in_month(year, month) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
61 assert 1 <= day <= dim, ('day must be in 1..%d' % dim) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
62 return (_days_before_year(year) + |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
63 _days_before_month(year, month) + |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
64 day) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
65 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
66 _DI400Y = _days_before_year(401) # number of days in 400 years |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
67 _DI100Y = _days_before_year(101) # " " " " 100 " |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
68 _DI4Y = _days_before_year(5) # " " " " 4 " |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
69 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
70 # A 4-year cycle has an extra leap day over what we'd get from pasting |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
71 # together 4 single years. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
72 assert _DI4Y == 4 * 365 + 1 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
73 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
74 # Similarly, a 400-year cycle has an extra leap day over what we'd get from |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
75 # pasting together 4 100-year cycles. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
76 assert _DI400Y == 4 * _DI100Y + 1 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
77 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
78 # OTOH, a 100-year cycle has one fewer leap day than we'd get from |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
79 # pasting together 25 4-year cycles. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
80 assert _DI100Y == 25 * _DI4Y - 1 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
81 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
82 def _ord2ymd(n): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
83 "ordinal -> (year, month, day), considering 01-Jan-0001 as day 1." |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
84 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
85 # n is a 1-based index, starting at 1-Jan-1. The pattern of leap years |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
86 # repeats exactly every 400 years. The basic strategy is to find the |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
87 # closest 400-year boundary at or before n, then work with the offset |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
88 # from that boundary to n. Life is much clearer if we subtract 1 from |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
89 # n first -- then the values of n at 400-year boundaries are exactly |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
90 # those divisible by _DI400Y: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
91 # |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
92 # D M Y n n-1 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
93 # -- --- ---- ---------- ---------------- |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
94 # 31 Dec -400 -_DI400Y -_DI400Y -1 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
95 # 1 Jan -399 -_DI400Y +1 -_DI400Y 400-year boundary |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
96 # ... |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
97 # 30 Dec 000 -1 -2 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
98 # 31 Dec 000 0 -1 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
99 # 1 Jan 001 1 0 400-year boundary |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
100 # 2 Jan 001 2 1 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
101 # 3 Jan 001 3 2 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
102 # ... |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
103 # 31 Dec 400 _DI400Y _DI400Y -1 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
104 # 1 Jan 401 _DI400Y +1 _DI400Y 400-year boundary |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
105 n -= 1 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
106 n400, n = divmod(n, _DI400Y) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
107 year = n400 * 400 + 1 # ..., -399, 1, 401, ... |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
108 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
109 # Now n is the (non-negative) offset, in days, from January 1 of year, to |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
110 # the desired date. Now compute how many 100-year cycles precede n. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
111 # Note that it's possible for n100 to equal 4! In that case 4 full |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
112 # 100-year cycles precede the desired day, which implies the desired |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
113 # day is December 31 at the end of a 400-year cycle. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
114 n100, n = divmod(n, _DI100Y) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
115 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
116 # Now compute how many 4-year cycles precede it. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
117 n4, n = divmod(n, _DI4Y) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
118 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
119 # And now how many single years. Again n1 can be 4, and again meaning |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
120 # that the desired day is December 31 at the end of the 4-year cycle. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
121 n1, n = divmod(n, 365) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
122 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
123 year += n100 * 100 + n4 * 4 + n1 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
124 if n1 == 4 or n100 == 4: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
125 assert n == 0 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
126 return year-1, 12, 31 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
127 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
128 # Now the year is correct, and n is the offset from January 1. We find |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
129 # the month via an estimate that's either exact or one too large. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
130 leapyear = n1 == 3 and (n4 != 24 or n100 == 3) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
131 assert leapyear == _is_leap(year) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
132 month = (n + 50) >> 5 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
133 preceding = _DAYS_BEFORE_MONTH[month] + (month > 2 and leapyear) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
134 if preceding > n: # estimate is too large |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
135 month -= 1 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
136 preceding -= _DAYS_IN_MONTH[month] + (month == 2 and leapyear) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
137 n -= preceding |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
138 assert 0 <= n < _days_in_month(year, month) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
139 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
140 # Now the year and month are correct, and n is the offset from the |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
141 # start of that month: we're done! |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
142 return year, month, n+1 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
143 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
144 # Month and day names. For localized versions, see the calendar module. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
145 _MONTHNAMES = [None, "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
146 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
147 _DAYNAMES = [None, "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
148 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
149 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
150 def _build_struct_time(y, m, d, hh, mm, ss, dstflag): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
151 wday = (_ymd2ord(y, m, d) + 6) % 7 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
152 dnum = _days_before_month(y, m) + d |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
153 return _time.struct_time((y, m, d, hh, mm, ss, wday, dnum, dstflag)) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
154 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
155 def _format_time(hh, mm, ss, us): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
156 # Skip trailing microseconds when us==0. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
157 result = "%02d:%02d:%02d" % (hh, mm, ss) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
158 if us: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
159 result += ".%06d" % us |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
160 return result |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
161 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
162 # Correctly substitute for %z and %Z escapes in strftime formats. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
163 def _wrap_strftime(object, format, timetuple): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
164 # Don't call utcoffset() or tzname() unless actually needed. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
165 freplace = None # the string to use for %f |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
166 zreplace = None # the string to use for %z |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
167 Zreplace = None # the string to use for %Z |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
168 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
169 # Scan format for %z and %Z escapes, replacing as needed. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
170 newformat = [] |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
171 push = newformat.append |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
172 i, n = 0, len(format) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
173 while i < n: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
174 ch = format[i] |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
175 i += 1 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
176 if ch == '%': |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
177 if i < n: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
178 ch = format[i] |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
179 i += 1 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
180 if ch == 'f': |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
181 if freplace is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
182 freplace = '%06d' % getattr(object, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
183 'microsecond', 0) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
184 newformat.append(freplace) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
185 elif ch == 'z': |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
186 if zreplace is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
187 zreplace = "" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
188 if hasattr(object, "utcoffset"): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
189 offset = object.utcoffset() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
190 if offset is not None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
191 sign = '+' |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
192 if offset.days < 0: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
193 offset = -offset |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
194 sign = '-' |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
195 h, m = divmod(offset, timedelta(hours=1)) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
196 assert not m % timedelta(minutes=1), "whole minute" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
197 m //= timedelta(minutes=1) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
198 zreplace = '%c%02d%02d' % (sign, h, m) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
199 assert '%' not in zreplace |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
200 newformat.append(zreplace) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
201 elif ch == 'Z': |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
202 if Zreplace is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
203 Zreplace = "" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
204 if hasattr(object, "tzname"): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
205 s = object.tzname() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
206 if s is not None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
207 # strftime is going to have at this: escape % |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
208 Zreplace = s.replace('%', '%%') |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
209 newformat.append(Zreplace) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
210 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
211 push('%') |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
212 push(ch) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
213 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
214 push('%') |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
215 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
216 push(ch) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
217 newformat = "".join(newformat) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
218 return _time.strftime(newformat, timetuple) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
219 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
220 def _call_tzinfo_method(tzinfo, methname, tzinfoarg): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
221 if tzinfo is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
222 return None |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
223 return getattr(tzinfo, methname)(tzinfoarg) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
224 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
225 # Just raise TypeError if the arg isn't None or a string. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
226 def _check_tzname(name): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
227 if name is not None and not isinstance(name, str): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
228 raise TypeError("tzinfo.tzname() must return None or string, " |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
229 "not '%s'" % type(name)) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
230 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
231 # name is the offset-producing method, "utcoffset" or "dst". |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
232 # offset is what it returned. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
233 # If offset isn't None or timedelta, raises TypeError. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
234 # If offset is None, returns None. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
235 # Else offset is checked for being in range, and a whole # of minutes. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
236 # If it is, its integer value is returned. Else ValueError is raised. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
237 def _check_utc_offset(name, offset): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
238 assert name in ("utcoffset", "dst") |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
239 if offset is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
240 return |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
241 if not isinstance(offset, timedelta): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
242 raise TypeError("tzinfo.%s() must return None " |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
243 "or timedelta, not '%s'" % (name, type(offset))) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
244 if offset % timedelta(minutes=1) or offset.microseconds: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
245 raise ValueError("tzinfo.%s() must return a whole number " |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
246 "of minutes, got %s" % (name, offset)) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
247 if not -timedelta(1) < offset < timedelta(1): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
248 raise ValueError("%s()=%s, must be must be strictly between" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
249 " -timedelta(hours=24) and timedelta(hours=24)" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
250 % (name, offset)) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
251 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
252 def _check_date_fields(year, month, day): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
253 if not isinstance(year, int): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
254 raise TypeError('int expected') |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
255 if not MINYEAR <= year <= MAXYEAR: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
256 raise ValueError('year must be in %d..%d' % (MINYEAR, MAXYEAR), year) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
257 if not 1 <= month <= 12: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
258 raise ValueError('month must be in 1..12', month) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
259 dim = _days_in_month(year, month) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
260 if not 1 <= day <= dim: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
261 raise ValueError('day must be in 1..%d' % dim, day) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
262 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
263 def _check_time_fields(hour, minute, second, microsecond): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
264 if not isinstance(hour, int): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
265 raise TypeError('int expected') |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
266 if not 0 <= hour <= 23: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
267 raise ValueError('hour must be in 0..23', hour) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
268 if not 0 <= minute <= 59: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
269 raise ValueError('minute must be in 0..59', minute) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
270 if not 0 <= second <= 59: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
271 raise ValueError('second must be in 0..59', second) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
272 if not 0 <= microsecond <= 999999: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
273 raise ValueError('microsecond must be in 0..999999', microsecond) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
274 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
275 def _check_tzinfo_arg(tz): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
276 if tz is not None and not isinstance(tz, tzinfo): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
277 raise TypeError("tzinfo argument must be None or of a tzinfo subclass") |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
278 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
279 def _cmperror(x, y): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
280 raise TypeError("can't compare '%s' to '%s'" % ( |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
281 type(x).__name__, type(y).__name__)) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
282 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
283 class timedelta: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
284 """Represent the difference between two datetime objects. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
285 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
286 Supported operators: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
287 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
288 - add, subtract timedelta |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
289 - unary plus, minus, abs |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
290 - compare to timedelta |
|
85418
4d62a62ba44d
Issue #18783: Removed existing mentions of Python long type in docstrings,
Serhiy Storchaka <storchaka@gmail.com>
parents:
85221
diff
changeset
|
291 - multiply, divide by int |
|
63184
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
292 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
293 In addition, datetime supports subtraction of two datetime objects |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
294 returning a timedelta, and addition or subtraction of a datetime |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
295 and a timedelta giving a datetime. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
296 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
297 Representation: (days, seconds, microseconds). Why? Because I |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
298 felt like it. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
299 """ |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
300 __slots__ = '_days', '_seconds', '_microseconds' |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
301 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
302 def __new__(cls, days=0, seconds=0, microseconds=0, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
303 milliseconds=0, minutes=0, hours=0, weeks=0): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
304 # Doing this efficiently and accurately in C is going to be difficult |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
305 # and error-prone, due to ubiquitous overflow possibilities, and that |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
306 # C double doesn't have enough bits of precision to represent |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
307 # microseconds over 10K years faithfully. The code here tries to make |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
308 # explicit where go-fast assumptions can be relied on, in order to |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
309 # guide the C implementation; it's way more convoluted than speed- |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
310 # ignoring auto-overflow-to-long idiomatic Python could be. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
311 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
312 # XXX Check that all inputs are ints or floats. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
313 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
314 # Final values, all integer. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
315 # s and us fit in 32-bit signed ints; d isn't bounded. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
316 d = s = us = 0 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
317 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
318 # Normalize everything to days, seconds, microseconds. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
319 days += weeks*7 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
320 seconds += minutes*60 + hours*3600 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
321 microseconds += milliseconds*1000 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
322 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
323 # Get rid of all fractions, and normalize s and us. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
324 # Take a deep breath <wink>. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
325 if isinstance(days, float): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
326 dayfrac, days = _math.modf(days) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
327 daysecondsfrac, daysecondswhole = _math.modf(dayfrac * (24.*3600.)) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
328 assert daysecondswhole == int(daysecondswhole) # can't overflow |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
329 s = int(daysecondswhole) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
330 assert days == int(days) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
331 d = int(days) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
332 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
333 daysecondsfrac = 0.0 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
334 d = days |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
335 assert isinstance(daysecondsfrac, float) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
336 assert abs(daysecondsfrac) <= 1.0 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
337 assert isinstance(d, int) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
338 assert abs(s) <= 24 * 3600 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
339 # days isn't referenced again before redefinition |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
340 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
341 if isinstance(seconds, float): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
342 secondsfrac, seconds = _math.modf(seconds) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
343 assert seconds == int(seconds) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
344 seconds = int(seconds) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
345 secondsfrac += daysecondsfrac |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
346 assert abs(secondsfrac) <= 2.0 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
347 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
348 secondsfrac = daysecondsfrac |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
349 # daysecondsfrac isn't referenced again |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
350 assert isinstance(secondsfrac, float) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
351 assert abs(secondsfrac) <= 2.0 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
352 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
353 assert isinstance(seconds, int) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
354 days, seconds = divmod(seconds, 24*3600) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
355 d += days |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
356 s += int(seconds) # can't overflow |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
357 assert isinstance(s, int) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
358 assert abs(s) <= 2 * 24 * 3600 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
359 # seconds isn't referenced again before redefinition |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
360 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
361 usdouble = secondsfrac * 1e6 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
362 assert abs(usdouble) < 2.1e6 # exact value not critical |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
363 # secondsfrac isn't referenced again |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
364 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
365 if isinstance(microseconds, float): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
366 microseconds += usdouble |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
367 microseconds = round(microseconds, 0) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
368 seconds, microseconds = divmod(microseconds, 1e6) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
369 assert microseconds == int(microseconds) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
370 assert seconds == int(seconds) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
371 days, seconds = divmod(seconds, 24.*3600.) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
372 assert days == int(days) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
373 assert seconds == int(seconds) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
374 d += int(days) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
375 s += int(seconds) # can't overflow |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
376 assert isinstance(s, int) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
377 assert abs(s) <= 3 * 24 * 3600 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
378 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
379 seconds, microseconds = divmod(microseconds, 1000000) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
380 days, seconds = divmod(seconds, 24*3600) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
381 d += days |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
382 s += int(seconds) # can't overflow |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
383 assert isinstance(s, int) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
384 assert abs(s) <= 3 * 24 * 3600 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
385 microseconds = float(microseconds) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
386 microseconds += usdouble |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
387 microseconds = round(microseconds, 0) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
388 assert abs(s) <= 3 * 24 * 3600 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
389 assert abs(microseconds) < 3.1e6 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
390 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
391 # Just a little bit of carrying possible for microseconds and seconds. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
392 assert isinstance(microseconds, float) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
393 assert int(microseconds) == microseconds |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
394 us = int(microseconds) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
395 seconds, us = divmod(us, 1000000) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
396 s += seconds # cant't overflow |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
397 assert isinstance(s, int) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
398 days, s = divmod(s, 24*3600) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
399 d += days |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
400 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
401 assert isinstance(d, int) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
402 assert isinstance(s, int) and 0 <= s < 24*3600 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
403 assert isinstance(us, int) and 0 <= us < 1000000 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
404 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
405 self = object.__new__(cls) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
406 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
407 self._days = d |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
408 self._seconds = s |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
409 self._microseconds = us |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
410 if abs(d) > 999999999: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
411 raise OverflowError("timedelta # of days is too large: %d" % d) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
412 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
413 return self |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
414 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
415 def __repr__(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
416 if self._microseconds: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
417 return "%s(%d, %d, %d)" % ('datetime.' + self.__class__.__name__, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
418 self._days, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
419 self._seconds, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
420 self._microseconds) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
421 if self._seconds: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
422 return "%s(%d, %d)" % ('datetime.' + self.__class__.__name__, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
423 self._days, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
424 self._seconds) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
425 return "%s(%d)" % ('datetime.' + self.__class__.__name__, self._days) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
426 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
427 def __str__(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
428 mm, ss = divmod(self._seconds, 60) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
429 hh, mm = divmod(mm, 60) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
430 s = "%d:%02d:%02d" % (hh, mm, ss) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
431 if self._days: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
432 def plural(n): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
433 return n, abs(n) != 1 and "s" or "" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
434 s = ("%d day%s, " % plural(self._days)) + s |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
435 if self._microseconds: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
436 s = s + ".%06d" % self._microseconds |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
437 return s |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
438 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
439 def total_seconds(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
440 """Total seconds in the duration.""" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
441 return ((self.days * 86400 + self.seconds)*10**6 + |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
442 self.microseconds) / 10**6 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
443 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
444 # Read-only field accessors |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
445 @property |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
446 def days(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
447 """days""" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
448 return self._days |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
449 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
450 @property |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
451 def seconds(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
452 """seconds""" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
453 return self._seconds |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
454 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
455 @property |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
456 def microseconds(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
457 """microseconds""" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
458 return self._microseconds |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
459 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
460 def __add__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
461 if isinstance(other, timedelta): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
462 # for CPython compatibility, we cannot use |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
463 # our __class__ here, but need a real timedelta |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
464 return timedelta(self._days + other._days, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
465 self._seconds + other._seconds, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
466 self._microseconds + other._microseconds) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
467 return NotImplemented |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
468 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
469 __radd__ = __add__ |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
470 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
471 def __sub__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
472 if isinstance(other, timedelta): |
|
69163
76180cc853b6
Issue #11576: Fixed timedelta subtraction glitch on big timedelta values
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
67450
diff
changeset
|
473 # for CPython compatibility, we cannot use |
|
76180cc853b6
Issue #11576: Fixed timedelta subtraction glitch on big timedelta values
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
67450
diff
changeset
|
474 # our __class__ here, but need a real timedelta |
|
76180cc853b6
Issue #11576: Fixed timedelta subtraction glitch on big timedelta values
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
67450
diff
changeset
|
475 return timedelta(self._days - other._days, |
|
76180cc853b6
Issue #11576: Fixed timedelta subtraction glitch on big timedelta values
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
67450
diff
changeset
|
476 self._seconds - other._seconds, |
|
76180cc853b6
Issue #11576: Fixed timedelta subtraction glitch on big timedelta values
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
67450
diff
changeset
|
477 self._microseconds - other._microseconds) |
|
63184
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
478 return NotImplemented |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
479 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
480 def __rsub__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
481 if isinstance(other, timedelta): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
482 return -self + other |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
483 return NotImplemented |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
484 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
485 def __neg__(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
486 # for CPython compatibility, we cannot use |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
487 # our __class__ here, but need a real timedelta |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
488 return timedelta(-self._days, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
489 -self._seconds, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
490 -self._microseconds) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
491 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
492 def __pos__(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
493 return self |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
494 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
495 def __abs__(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
496 if self._days < 0: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
497 return -self |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
498 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
499 return self |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
500 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
501 def __mul__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
502 if isinstance(other, int): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
503 # for CPython compatibility, we cannot use |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
504 # our __class__ here, but need a real timedelta |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
505 return timedelta(self._days * other, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
506 self._seconds * other, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
507 self._microseconds * other) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
508 if isinstance(other, float): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
509 a, b = other.as_integer_ratio() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
510 return self * a / b |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
511 return NotImplemented |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
512 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
513 __rmul__ = __mul__ |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
514 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
515 def _to_microseconds(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
516 return ((self._days * (24*3600) + self._seconds) * 1000000 + |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
517 self._microseconds) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
518 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
519 def __floordiv__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
520 if not isinstance(other, (int, timedelta)): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
521 return NotImplemented |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
522 usec = self._to_microseconds() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
523 if isinstance(other, timedelta): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
524 return usec // other._to_microseconds() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
525 if isinstance(other, int): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
526 return timedelta(0, 0, usec // other) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
527 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
528 def __truediv__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
529 if not isinstance(other, (int, float, timedelta)): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
530 return NotImplemented |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
531 usec = self._to_microseconds() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
532 if isinstance(other, timedelta): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
533 return usec / other._to_microseconds() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
534 if isinstance(other, int): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
535 return timedelta(0, 0, usec / other) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
536 if isinstance(other, float): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
537 a, b = other.as_integer_ratio() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
538 return timedelta(0, 0, b * usec / a) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
539 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
540 def __mod__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
541 if isinstance(other, timedelta): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
542 r = self._to_microseconds() % other._to_microseconds() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
543 return timedelta(0, 0, r) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
544 return NotImplemented |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
545 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
546 def __divmod__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
547 if isinstance(other, timedelta): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
548 q, r = divmod(self._to_microseconds(), |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
549 other._to_microseconds()) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
550 return q, timedelta(0, 0, r) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
551 return NotImplemented |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
552 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
553 # Comparisons of timedelta objects with other. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
554 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
555 def __eq__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
556 if isinstance(other, timedelta): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
557 return self._cmp(other) == 0 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
558 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
559 return False |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
560 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
561 def __ne__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
562 if isinstance(other, timedelta): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
563 return self._cmp(other) != 0 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
564 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
565 return True |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
566 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
567 def __le__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
568 if isinstance(other, timedelta): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
569 return self._cmp(other) <= 0 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
570 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
571 _cmperror(self, other) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
572 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
573 def __lt__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
574 if isinstance(other, timedelta): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
575 return self._cmp(other) < 0 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
576 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
577 _cmperror(self, other) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
578 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
579 def __ge__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
580 if isinstance(other, timedelta): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
581 return self._cmp(other) >= 0 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
582 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
583 _cmperror(self, other) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
584 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
585 def __gt__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
586 if isinstance(other, timedelta): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
587 return self._cmp(other) > 0 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
588 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
589 _cmperror(self, other) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
590 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
591 def _cmp(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
592 assert isinstance(other, timedelta) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
593 return _cmp(self._getstate(), other._getstate()) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
594 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
595 def __hash__(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
596 return hash(self._getstate()) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
597 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
598 def __bool__(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
599 return (self._days != 0 or |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
600 self._seconds != 0 or |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
601 self._microseconds != 0) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
602 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
603 # Pickle support. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
604 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
605 def _getstate(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
606 return (self._days, self._seconds, self._microseconds) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
607 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
608 def __reduce__(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
609 return (self.__class__, self._getstate()) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
610 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
611 timedelta.min = timedelta(-999999999) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
612 timedelta.max = timedelta(days=999999999, hours=23, minutes=59, seconds=59, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
613 microseconds=999999) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
614 timedelta.resolution = timedelta(microseconds=1) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
615 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
616 class date: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
617 """Concrete date type. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
618 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
619 Constructors: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
620 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
621 __new__() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
622 fromtimestamp() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
623 today() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
624 fromordinal() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
625 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
626 Operators: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
627 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
628 __repr__, __str__ |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
629 __cmp__, __hash__ |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
630 __add__, __radd__, __sub__ (add/radd only with timedelta arg) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
631 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
632 Methods: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
633 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
634 timetuple() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
635 toordinal() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
636 weekday() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
637 isoweekday(), isocalendar(), isoformat() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
638 ctime() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
639 strftime() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
640 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
641 Properties (readonly): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
642 year, month, day |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
643 """ |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
644 __slots__ = '_year', '_month', '_day' |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
645 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
646 def __new__(cls, year, month=None, day=None): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
647 """Constructor. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
648 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
649 Arguments: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
650 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
651 year, month, day (required, base 1) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
652 """ |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
653 if (isinstance(year, bytes) and len(year) == 4 and |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
654 1 <= year[2] <= 12 and month is None): # Month is sane |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
655 # Pickle support |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
656 self = object.__new__(cls) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
657 self.__setstate(year) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
658 return self |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
659 _check_date_fields(year, month, day) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
660 self = object.__new__(cls) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
661 self._year = year |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
662 self._month = month |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
663 self._day = day |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
664 return self |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
665 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
666 # Additional constructors |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
667 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
668 @classmethod |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
669 def fromtimestamp(cls, t): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
670 "Construct a date from a POSIX timestamp (like time.time())." |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
671 y, m, d, hh, mm, ss, weekday, jday, dst = _time.localtime(t) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
672 return cls(y, m, d) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
673 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
674 @classmethod |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
675 def today(cls): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
676 "Construct a date from time.time()." |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
677 t = _time.time() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
678 return cls.fromtimestamp(t) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
679 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
680 @classmethod |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
681 def fromordinal(cls, n): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
682 """Contruct a date from a proleptic Gregorian ordinal. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
683 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
684 January 1 of year 1 is day 1. Only the year, month and day are |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
685 non-zero in the result. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
686 """ |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
687 y, m, d = _ord2ymd(n) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
688 return cls(y, m, d) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
689 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
690 # Conversions to string |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
691 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
692 def __repr__(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
693 """Convert to formal string, for repr(). |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
694 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
695 >>> dt = datetime(2010, 1, 1) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
696 >>> repr(dt) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
697 'datetime.datetime(2010, 1, 1, 0, 0)' |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
698 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
699 >>> dt = datetime(2010, 1, 1, tzinfo=timezone.utc) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
700 >>> repr(dt) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
701 'datetime.datetime(2010, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)' |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
702 """ |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
703 return "%s(%d, %d, %d)" % ('datetime.' + self.__class__.__name__, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
704 self._year, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
705 self._month, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
706 self._day) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
707 # XXX These shouldn't depend on time.localtime(), because that |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
708 # clips the usable dates to [1970 .. 2038). At least ctime() is |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
709 # easily done without using strftime() -- that's better too because |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
710 # strftime("%c", ...) is locale specific. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
711 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
712 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
713 def ctime(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
714 "Return ctime() style string." |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
715 weekday = self.toordinal() % 7 or 7 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
716 return "%s %s %2d 00:00:00 %04d" % ( |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
717 _DAYNAMES[weekday], |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
718 _MONTHNAMES[self._month], |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
719 self._day, self._year) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
720 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
721 def strftime(self, fmt): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
722 "Format using strftime()." |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
723 return _wrap_strftime(self, fmt, self.timetuple()) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
724 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
725 def __format__(self, fmt): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
726 if len(fmt) != 0: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
727 return self.strftime(fmt) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
728 return str(self) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
729 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
730 def isoformat(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
731 """Return the date formatted according to ISO. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
732 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
733 This is 'YYYY-MM-DD'. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
734 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
735 References: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
736 - http://www.w3.org/TR/NOTE-datetime |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
737 - http://www.cl.cam.ac.uk/~mgk25/iso-time.html |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
738 """ |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
739 return "%04d-%02d-%02d" % (self._year, self._month, self._day) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
740 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
741 __str__ = isoformat |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
742 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
743 # Read-only field accessors |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
744 @property |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
745 def year(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
746 """year (1-9999)""" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
747 return self._year |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
748 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
749 @property |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
750 def month(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
751 """month (1-12)""" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
752 return self._month |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
753 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
754 @property |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
755 def day(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
756 """day (1-31)""" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
757 return self._day |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
758 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
759 # Standard conversions, __cmp__, __hash__ (and helpers) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
760 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
761 def timetuple(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
762 "Return local time tuple compatible with time.localtime()." |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
763 return _build_struct_time(self._year, self._month, self._day, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
764 0, 0, 0, -1) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
765 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
766 def toordinal(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
767 """Return proleptic Gregorian ordinal for the year, month and day. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
768 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
769 January 1 of year 1 is day 1. Only the year, month and day values |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
770 contribute to the result. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
771 """ |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
772 return _ymd2ord(self._year, self._month, self._day) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
773 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
774 def replace(self, year=None, month=None, day=None): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
775 """Return a new date with new values for the specified fields.""" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
776 if year is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
777 year = self._year |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
778 if month is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
779 month = self._month |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
780 if day is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
781 day = self._day |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
782 _check_date_fields(year, month, day) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
783 return date(year, month, day) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
784 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
785 # Comparisons of date objects with other. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
786 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
787 def __eq__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
788 if isinstance(other, date): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
789 return self._cmp(other) == 0 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
790 return NotImplemented |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
791 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
792 def __ne__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
793 if isinstance(other, date): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
794 return self._cmp(other) != 0 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
795 return NotImplemented |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
796 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
797 def __le__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
798 if isinstance(other, date): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
799 return self._cmp(other) <= 0 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
800 return NotImplemented |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
801 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
802 def __lt__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
803 if isinstance(other, date): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
804 return self._cmp(other) < 0 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
805 return NotImplemented |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
806 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
807 def __ge__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
808 if isinstance(other, date): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
809 return self._cmp(other) >= 0 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
810 return NotImplemented |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
811 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
812 def __gt__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
813 if isinstance(other, date): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
814 return self._cmp(other) > 0 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
815 return NotImplemented |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
816 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
817 def _cmp(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
818 assert isinstance(other, date) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
819 y, m, d = self._year, self._month, self._day |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
820 y2, m2, d2 = other._year, other._month, other._day |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
821 return _cmp((y, m, d), (y2, m2, d2)) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
822 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
823 def __hash__(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
824 "Hash." |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
825 return hash(self._getstate()) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
826 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
827 # Computations |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
828 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
829 def __add__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
830 "Add a date to a timedelta." |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
831 if isinstance(other, timedelta): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
832 o = self.toordinal() + other.days |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
833 if 0 < o <= _MAXORDINAL: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
834 return date.fromordinal(o) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
835 raise OverflowError("result out of range") |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
836 return NotImplemented |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
837 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
838 __radd__ = __add__ |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
839 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
840 def __sub__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
841 """Subtract two dates, or a date and a timedelta.""" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
842 if isinstance(other, timedelta): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
843 return self + timedelta(-other.days) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
844 if isinstance(other, date): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
845 days1 = self.toordinal() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
846 days2 = other.toordinal() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
847 return timedelta(days1 - days2) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
848 return NotImplemented |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
849 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
850 def weekday(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
851 "Return day of the week, where Monday == 0 ... Sunday == 6." |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
852 return (self.toordinal() + 6) % 7 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
853 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
854 # Day-of-the-week and week-of-the-year, according to ISO |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
855 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
856 def isoweekday(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
857 "Return day of the week, where Monday == 1 ... Sunday == 7." |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
858 # 1-Jan-0001 is a Monday |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
859 return self.toordinal() % 7 or 7 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
860 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
861 def isocalendar(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
862 """Return a 3-tuple containing ISO year, week number, and weekday. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
863 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
864 The first ISO week of the year is the (Mon-Sun) week |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
865 containing the year's first Thursday; everything else derives |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
866 from that. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
867 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
868 The first week is 1; Monday is 1 ... Sunday is 7. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
869 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
870 ISO calendar algorithm taken from |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
871 http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
872 """ |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
873 year = self._year |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
874 week1monday = _isoweek1monday(year) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
875 today = _ymd2ord(self._year, self._month, self._day) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
876 # Internally, week and day have origin 0 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
877 week, day = divmod(today - week1monday, 7) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
878 if week < 0: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
879 year -= 1 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
880 week1monday = _isoweek1monday(year) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
881 week, day = divmod(today - week1monday, 7) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
882 elif week >= 52: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
883 if today >= _isoweek1monday(year+1): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
884 year += 1 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
885 week = 0 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
886 return year, week+1, day+1 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
887 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
888 # Pickle support. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
889 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
890 def _getstate(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
891 yhi, ylo = divmod(self._year, 256) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
892 return bytes([yhi, ylo, self._month, self._day]), |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
893 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
894 def __setstate(self, string): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
895 if len(string) != 4 or not (1 <= string[2] <= 12): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
896 raise TypeError("not enough arguments") |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
897 yhi, ylo, self._month, self._day = string |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
898 self._year = yhi * 256 + ylo |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
899 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
900 def __reduce__(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
901 return (self.__class__, self._getstate()) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
902 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
903 _date_class = date # so functions w/ args named "date" can get at the class |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
904 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
905 date.min = date(1, 1, 1) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
906 date.max = date(9999, 12, 31) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
907 date.resolution = timedelta(days=1) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
908 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
909 class tzinfo: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
910 """Abstract base class for time zone info classes. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
911 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
912 Subclasses must override the name(), utcoffset() and dst() methods. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
913 """ |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
914 __slots__ = () |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
915 def tzname(self, dt): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
916 "datetime -> string name of time zone." |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
917 raise NotImplementedError("tzinfo subclass must override tzname()") |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
918 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
919 def utcoffset(self, dt): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
920 "datetime -> minutes east of UTC (negative for west of UTC)" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
921 raise NotImplementedError("tzinfo subclass must override utcoffset()") |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
922 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
923 def dst(self, dt): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
924 """datetime -> DST offset in minutes east of UTC. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
925 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
926 Return 0 if DST not in effect. utcoffset() must include the DST |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
927 offset. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
928 """ |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
929 raise NotImplementedError("tzinfo subclass must override dst()") |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
930 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
931 def fromutc(self, dt): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
932 "datetime in UTC -> datetime in local time." |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
933 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
934 if not isinstance(dt, datetime): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
935 raise TypeError("fromutc() requires a datetime argument") |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
936 if dt.tzinfo is not self: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
937 raise ValueError("dt.tzinfo is not self") |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
938 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
939 dtoff = dt.utcoffset() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
940 if dtoff is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
941 raise ValueError("fromutc() requires a non-None utcoffset() " |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
942 "result") |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
943 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
944 # See the long comment block at the end of this file for an |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
945 # explanation of this algorithm. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
946 dtdst = dt.dst() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
947 if dtdst is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
948 raise ValueError("fromutc() requires a non-None dst() result") |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
949 delta = dtoff - dtdst |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
950 if delta: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
951 dt += delta |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
952 dtdst = dt.dst() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
953 if dtdst is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
954 raise ValueError("fromutc(): dt.dst gave inconsistent " |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
955 "results; cannot convert") |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
956 return dt + dtdst |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
957 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
958 # Pickle support. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
959 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
960 def __reduce__(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
961 getinitargs = getattr(self, "__getinitargs__", None) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
962 if getinitargs: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
963 args = getinitargs() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
964 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
965 args = () |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
966 getstate = getattr(self, "__getstate__", None) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
967 if getstate: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
968 state = getstate() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
969 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
970 state = getattr(self, "__dict__", None) or None |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
971 if state is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
972 return (self.__class__, args) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
973 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
974 return (self.__class__, args, state) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
975 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
976 _tzinfo_class = tzinfo |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
977 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
978 class time: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
979 """Time with time zone. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
980 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
981 Constructors: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
982 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
983 __new__() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
984 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
985 Operators: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
986 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
987 __repr__, __str__ |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
988 __cmp__, __hash__ |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
989 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
990 Methods: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
991 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
992 strftime() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
993 isoformat() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
994 utcoffset() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
995 tzname() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
996 dst() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
997 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
998 Properties (readonly): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
999 hour, minute, second, microsecond, tzinfo |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1000 """ |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1001 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1002 def __new__(cls, hour=0, minute=0, second=0, microsecond=0, tzinfo=None): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1003 """Constructor. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1004 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1005 Arguments: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1006 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1007 hour, minute (required) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1008 second, microsecond (default to zero) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1009 tzinfo (default to None) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1010 """ |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1011 self = object.__new__(cls) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1012 if isinstance(hour, bytes) and len(hour) == 6: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1013 # Pickle support |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1014 self.__setstate(hour, minute or None) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1015 return self |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1016 _check_tzinfo_arg(tzinfo) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1017 _check_time_fields(hour, minute, second, microsecond) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1018 self._hour = hour |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1019 self._minute = minute |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1020 self._second = second |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1021 self._microsecond = microsecond |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1022 self._tzinfo = tzinfo |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1023 return self |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1024 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1025 # Read-only field accessors |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1026 @property |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1027 def hour(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1028 """hour (0-23)""" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1029 return self._hour |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1030 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1031 @property |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1032 def minute(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1033 """minute (0-59)""" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1034 return self._minute |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1035 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1036 @property |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1037 def second(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1038 """second (0-59)""" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1039 return self._second |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1040 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1041 @property |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1042 def microsecond(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1043 """microsecond (0-999999)""" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1044 return self._microsecond |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1045 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1046 @property |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1047 def tzinfo(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1048 """timezone info object""" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1049 return self._tzinfo |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1050 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1051 # Standard conversions, __hash__ (and helpers) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1052 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1053 # Comparisons of time objects with other. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1054 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1055 def __eq__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1056 if isinstance(other, time): |
|
77458
8272699973cb
Issue #15006: Allow equality comparison between naive and aware time
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77451
diff
changeset
|
1057 return self._cmp(other, allow_mixed=True) == 0 |
|
63184
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1058 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1059 return False |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1060 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1061 def __ne__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1062 if isinstance(other, time): |
|
77458
8272699973cb
Issue #15006: Allow equality comparison between naive and aware time
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77451
diff
changeset
|
1063 return self._cmp(other, allow_mixed=True) != 0 |
|
63184
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1064 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1065 return True |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1066 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1067 def __le__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1068 if isinstance(other, time): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1069 return self._cmp(other) <= 0 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1070 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1071 _cmperror(self, other) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1072 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1073 def __lt__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1074 if isinstance(other, time): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1075 return self._cmp(other) < 0 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1076 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1077 _cmperror(self, other) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1078 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1079 def __ge__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1080 if isinstance(other, time): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1081 return self._cmp(other) >= 0 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1082 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1083 _cmperror(self, other) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1084 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1085 def __gt__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1086 if isinstance(other, time): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1087 return self._cmp(other) > 0 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1088 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1089 _cmperror(self, other) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1090 |
|
77458
8272699973cb
Issue #15006: Allow equality comparison between naive and aware time
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77451
diff
changeset
|
1091 def _cmp(self, other, allow_mixed=False): |
|
63184
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1092 assert isinstance(other, time) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1093 mytz = self._tzinfo |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1094 ottz = other._tzinfo |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1095 myoff = otoff = None |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1096 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1097 if mytz is ottz: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1098 base_compare = True |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1099 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1100 myoff = self.utcoffset() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1101 otoff = other.utcoffset() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1102 base_compare = myoff == otoff |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1103 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1104 if base_compare: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1105 return _cmp((self._hour, self._minute, self._second, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1106 self._microsecond), |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1107 (other._hour, other._minute, other._second, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1108 other._microsecond)) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1109 if myoff is None or otoff is None: |
|
77458
8272699973cb
Issue #15006: Allow equality comparison between naive and aware time
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77451
diff
changeset
|
1110 if allow_mixed: |
|
8272699973cb
Issue #15006: Allow equality comparison between naive and aware time
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77451
diff
changeset
|
1111 return 2 # arbitrary non-zero value |
|
8272699973cb
Issue #15006: Allow equality comparison between naive and aware time
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77451
diff
changeset
|
1112 else: |
|
8272699973cb
Issue #15006: Allow equality comparison between naive and aware time
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77451
diff
changeset
|
1113 raise TypeError("cannot compare naive and aware times") |
|
63184
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1114 myhhmm = self._hour * 60 + self._minute - myoff//timedelta(minutes=1) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1115 othhmm = other._hour * 60 + other._minute - otoff//timedelta(minutes=1) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1116 return _cmp((myhhmm, self._second, self._microsecond), |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1117 (othhmm, other._second, other._microsecond)) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1118 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1119 def __hash__(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1120 """Hash.""" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1121 tzoff = self.utcoffset() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1122 if not tzoff: # zero or None |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1123 return hash(self._getstate()[0]) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1124 h, m = divmod(timedelta(hours=self.hour, minutes=self.minute) - tzoff, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1125 timedelta(hours=1)) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1126 assert not m % timedelta(minutes=1), "whole minute" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1127 m //= timedelta(minutes=1) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1128 if 0 <= h < 24: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1129 return hash(time(h, m, self.second, self.microsecond)) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1130 return hash((h, m, self.second, self.microsecond)) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1131 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1132 # Conversion to string |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1133 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1134 def _tzstr(self, sep=":"): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1135 """Return formatted timezone offset (+xx:xx) or None.""" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1136 off = self.utcoffset() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1137 if off is not None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1138 if off.days < 0: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1139 sign = "-" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1140 off = -off |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1141 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1142 sign = "+" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1143 hh, mm = divmod(off, timedelta(hours=1)) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1144 assert not mm % timedelta(minutes=1), "whole minute" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1145 mm //= timedelta(minutes=1) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1146 assert 0 <= hh < 24 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1147 off = "%s%02d%s%02d" % (sign, hh, sep, mm) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1148 return off |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1149 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1150 def __repr__(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1151 """Convert to formal string, for repr().""" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1152 if self._microsecond != 0: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1153 s = ", %d, %d" % (self._second, self._microsecond) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1154 elif self._second != 0: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1155 s = ", %d" % self._second |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1156 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1157 s = "" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1158 s= "%s(%d, %d%s)" % ('datetime.' + self.__class__.__name__, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1159 self._hour, self._minute, s) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1160 if self._tzinfo is not None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1161 assert s[-1:] == ")" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1162 s = s[:-1] + ", tzinfo=%r" % self._tzinfo + ")" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1163 return s |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1164 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1165 def isoformat(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1166 """Return the time formatted according to ISO. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1167 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1168 This is 'HH:MM:SS.mmmmmm+zz:zz', or 'HH:MM:SS+zz:zz' if |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1169 self.microsecond == 0. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1170 """ |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1171 s = _format_time(self._hour, self._minute, self._second, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1172 self._microsecond) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1173 tz = self._tzstr() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1174 if tz: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1175 s += tz |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1176 return s |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1177 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1178 __str__ = isoformat |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1179 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1180 def strftime(self, fmt): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1181 """Format using strftime(). The date part of the timestamp passed |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1182 to underlying strftime should not be used. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1183 """ |
|
67450
31465968ae26
Issue #1777412: extended year range of strftime down to 1000.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
65319
diff
changeset
|
1184 # The year must be >= 1000 else Python's strftime implementation |
|
63184
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1185 # can raise a bogus exception. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1186 timetuple = (1900, 1, 1, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1187 self._hour, self._minute, self._second, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1188 0, 1, -1) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1189 return _wrap_strftime(self, fmt, timetuple) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1190 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1191 def __format__(self, fmt): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1192 if len(fmt) != 0: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1193 return self.strftime(fmt) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1194 return str(self) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1195 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1196 # Timezone functions |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1197 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1198 def utcoffset(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1199 """Return the timezone offset in minutes east of UTC (negative west of |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1200 UTC).""" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1201 if self._tzinfo is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1202 return None |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1203 offset = self._tzinfo.utcoffset(None) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1204 _check_utc_offset("utcoffset", offset) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1205 return offset |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1206 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1207 def tzname(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1208 """Return the timezone name. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1209 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1210 Note that the name is 100% informational -- there's no requirement that |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1211 it mean anything in particular. For example, "GMT", "UTC", "-500", |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1212 "-5:00", "EDT", "US/Eastern", "America/New York" are all valid replies. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1213 """ |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1214 if self._tzinfo is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1215 return None |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1216 name = self._tzinfo.tzname(None) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1217 _check_tzname(name) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1218 return name |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1219 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1220 def dst(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1221 """Return 0 if DST is not in effect, or the DST offset (in minutes |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1222 eastward) if DST is in effect. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1223 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1224 This is purely informational; the DST offset has already been added to |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1225 the UTC offset returned by utcoffset() if applicable, so there's no |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1226 need to consult dst() unless you're interested in displaying the DST |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1227 info. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1228 """ |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1229 if self._tzinfo is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1230 return None |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1231 offset = self._tzinfo.dst(None) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1232 _check_utc_offset("dst", offset) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1233 return offset |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1234 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1235 def replace(self, hour=None, minute=None, second=None, microsecond=None, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1236 tzinfo=True): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1237 """Return a new time with new values for the specified fields.""" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1238 if hour is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1239 hour = self.hour |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1240 if minute is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1241 minute = self.minute |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1242 if second is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1243 second = self.second |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1244 if microsecond is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1245 microsecond = self.microsecond |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1246 if tzinfo is True: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1247 tzinfo = self.tzinfo |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1248 _check_time_fields(hour, minute, second, microsecond) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1249 _check_tzinfo_arg(tzinfo) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1250 return time(hour, minute, second, microsecond, tzinfo) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1251 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1252 def __bool__(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1253 if self.second or self.microsecond: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1254 return True |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1255 offset = self.utcoffset() or timedelta(0) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1256 return timedelta(hours=self.hour, minutes=self.minute) != offset |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1257 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1258 # Pickle support. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1259 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1260 def _getstate(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1261 us2, us3 = divmod(self._microsecond, 256) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1262 us1, us2 = divmod(us2, 256) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1263 basestate = bytes([self._hour, self._minute, self._second, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1264 us1, us2, us3]) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1265 if self._tzinfo is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1266 return (basestate,) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1267 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1268 return (basestate, self._tzinfo) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1269 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1270 def __setstate(self, string, tzinfo): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1271 if len(string) != 6 or string[0] >= 24: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1272 raise TypeError("an integer is required") |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1273 (self._hour, self._minute, self._second, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1274 us1, us2, us3) = string |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1275 self._microsecond = (((us1 << 8) | us2) << 8) | us3 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1276 if tzinfo is None or isinstance(tzinfo, _tzinfo_class): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1277 self._tzinfo = tzinfo |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1278 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1279 raise TypeError("bad tzinfo state arg %r" % tzinfo) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1280 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1281 def __reduce__(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1282 return (time, self._getstate()) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1283 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1284 _time_class = time # so functions w/ args named "time" can get at the class |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1285 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1286 time.min = time(0, 0, 0) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1287 time.max = time(23, 59, 59, 999999) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1288 time.resolution = timedelta(microseconds=1) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1289 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1290 class datetime(date): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1291 """datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]]) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1292 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1293 The year, month and day arguments are required. tzinfo may be None, or an |
|
85418
4d62a62ba44d
Issue #18783: Removed existing mentions of Python long type in docstrings,
Serhiy Storchaka <storchaka@gmail.com>
parents:
85221
diff
changeset
|
1294 instance of a tzinfo subclass. The remaining arguments may be ints. |
|
63184
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1295 """ |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1296 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1297 __slots__ = date.__slots__ + ( |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1298 '_hour', '_minute', '_second', |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1299 '_microsecond', '_tzinfo') |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1300 def __new__(cls, year, month=None, day=None, hour=0, minute=0, second=0, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1301 microsecond=0, tzinfo=None): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1302 if isinstance(year, bytes) and len(year) == 10: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1303 # Pickle support |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1304 self = date.__new__(cls, year[:4]) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1305 self.__setstate(year, month) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1306 return self |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1307 _check_tzinfo_arg(tzinfo) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1308 _check_time_fields(hour, minute, second, microsecond) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1309 self = date.__new__(cls, year, month, day) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1310 self._hour = hour |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1311 self._minute = minute |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1312 self._second = second |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1313 self._microsecond = microsecond |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1314 self._tzinfo = tzinfo |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1315 return self |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1316 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1317 # Read-only field accessors |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1318 @property |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1319 def hour(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1320 """hour (0-23)""" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1321 return self._hour |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1322 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1323 @property |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1324 def minute(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1325 """minute (0-59)""" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1326 return self._minute |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1327 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1328 @property |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1329 def second(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1330 """second (0-59)""" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1331 return self._second |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1332 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1333 @property |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1334 def microsecond(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1335 """microsecond (0-999999)""" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1336 return self._microsecond |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1337 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1338 @property |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1339 def tzinfo(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1340 """timezone info object""" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1341 return self._tzinfo |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1342 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1343 @classmethod |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1344 def fromtimestamp(cls, t, tz=None): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1345 """Construct a datetime from a POSIX timestamp (like time.time()). |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1346 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1347 A timezone info object may be passed in as well. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1348 """ |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1349 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1350 _check_tzinfo_arg(tz) |
|
63223
f6b92808b9b3
Make python version of fromtimestamp behave more like C.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63187
diff
changeset
|
1351 |
|
f6b92808b9b3
Make python version of fromtimestamp behave more like C.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63187
diff
changeset
|
1352 converter = _time.localtime if tz is None else _time.gmtime |
|
f6b92808b9b3
Make python version of fromtimestamp behave more like C.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63187
diff
changeset
|
1353 |
|
f6b92808b9b3
Make python version of fromtimestamp behave more like C.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63187
diff
changeset
|
1354 t, frac = divmod(t, 1.0) |
|
75590
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
72986
diff
changeset
|
1355 us = int(frac * 1e6) |
|
63223
f6b92808b9b3
Make python version of fromtimestamp behave more like C.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63187
diff
changeset
|
1356 |
|
f6b92808b9b3
Make python version of fromtimestamp behave more like C.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63187
diff
changeset
|
1357 # If timestamp is less than one microsecond smaller than a |
|
f6b92808b9b3
Make python version of fromtimestamp behave more like C.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63187
diff
changeset
|
1358 # full second, us can be rounded up to 1000000. In this case, |
|
f6b92808b9b3
Make python version of fromtimestamp behave more like C.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63187
diff
changeset
|
1359 # roll over to seconds, otherwise, ValueError is raised |
|
f6b92808b9b3
Make python version of fromtimestamp behave more like C.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63187
diff
changeset
|
1360 # by the constructor. |
|
f6b92808b9b3
Make python version of fromtimestamp behave more like C.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63187
diff
changeset
|
1361 if us == 1000000: |
|
f6b92808b9b3
Make python version of fromtimestamp behave more like C.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63187
diff
changeset
|
1362 t += 1 |
|
f6b92808b9b3
Make python version of fromtimestamp behave more like C.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63187
diff
changeset
|
1363 us = 0 |
|
63184
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1364 y, m, d, hh, mm, ss, weekday, jday, dst = converter(t) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1365 ss = min(ss, 59) # clamp out leap seconds if the platform has them |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1366 result = cls(y, m, d, hh, mm, ss, us, tz) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1367 if tz is not None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1368 result = tz.fromutc(result) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1369 return result |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1370 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1371 @classmethod |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1372 def utcfromtimestamp(cls, t): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1373 "Construct a UTC datetime from a POSIX timestamp (like time.time())." |
|
64883
2b9327777832
Fixed microsecond rounding in python version of utcfromtimestamp
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63223
diff
changeset
|
1374 t, frac = divmod(t, 1.0) |
|
75590
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
72986
diff
changeset
|
1375 us = int(frac * 1e6) |
|
64883
2b9327777832
Fixed microsecond rounding in python version of utcfromtimestamp
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63223
diff
changeset
|
1376 |
|
2b9327777832
Fixed microsecond rounding in python version of utcfromtimestamp
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63223
diff
changeset
|
1377 # If timestamp is less than one microsecond smaller than a |
|
2b9327777832
Fixed microsecond rounding in python version of utcfromtimestamp
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63223
diff
changeset
|
1378 # full second, us can be rounded up to 1000000. In this case, |
|
2b9327777832
Fixed microsecond rounding in python version of utcfromtimestamp
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63223
diff
changeset
|
1379 # roll over to seconds, otherwise, ValueError is raised |
|
2b9327777832
Fixed microsecond rounding in python version of utcfromtimestamp
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63223
diff
changeset
|
1380 # by the constructor. |
|
2b9327777832
Fixed microsecond rounding in python version of utcfromtimestamp
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63223
diff
changeset
|
1381 if us == 1000000: |
|
2b9327777832
Fixed microsecond rounding in python version of utcfromtimestamp
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63223
diff
changeset
|
1382 t += 1 |
|
2b9327777832
Fixed microsecond rounding in python version of utcfromtimestamp
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63223
diff
changeset
|
1383 us = 0 |
|
63184
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1384 y, m, d, hh, mm, ss, weekday, jday, dst = _time.gmtime(t) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1385 ss = min(ss, 59) # clamp out leap seconds if the platform has them |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1386 return cls(y, m, d, hh, mm, ss, us) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1387 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1388 # XXX This is supposed to do better than we *can* do by using time.time(), |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1389 # XXX if the platform supports a more accurate way. The C implementation |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1390 # XXX uses gettimeofday on platforms that have it, but that isn't |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1391 # XXX available from Python. So now() may return different results |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1392 # XXX across the implementations. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1393 @classmethod |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1394 def now(cls, tz=None): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1395 "Construct a datetime from time.time() and optional time zone info." |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1396 t = _time.time() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1397 return cls.fromtimestamp(t, tz) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1398 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1399 @classmethod |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1400 def utcnow(cls): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1401 "Construct a UTC datetime from time.time()." |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1402 t = _time.time() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1403 return cls.utcfromtimestamp(t) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1404 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1405 @classmethod |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1406 def combine(cls, date, time): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1407 "Construct a datetime from a given date and a given time." |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1408 if not isinstance(date, _date_class): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1409 raise TypeError("date argument must be a date instance") |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1410 if not isinstance(time, _time_class): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1411 raise TypeError("time argument must be a time instance") |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1412 return cls(date.year, date.month, date.day, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1413 time.hour, time.minute, time.second, time.microsecond, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1414 time.tzinfo) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1415 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1416 def timetuple(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1417 "Return local time tuple compatible with time.localtime()." |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1418 dst = self.dst() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1419 if dst is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1420 dst = -1 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1421 elif dst: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1422 dst = 1 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1423 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1424 dst = 0 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1425 return _build_struct_time(self.year, self.month, self.day, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1426 self.hour, self.minute, self.second, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1427 dst) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1428 |
|
77384
6671c5039e15
Issue #2736: Added datetime.timestamp() method.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
75590
diff
changeset
|
1429 def timestamp(self): |
|
6671c5039e15
Issue #2736: Added datetime.timestamp() method.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
75590
diff
changeset
|
1430 "Return POSIX timestamp as float" |
|
6671c5039e15
Issue #2736: Added datetime.timestamp() method.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
75590
diff
changeset
|
1431 if self._tzinfo is None: |
|
6671c5039e15
Issue #2736: Added datetime.timestamp() method.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
75590
diff
changeset
|
1432 return _time.mktime((self.year, self.month, self.day, |
|
6671c5039e15
Issue #2736: Added datetime.timestamp() method.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
75590
diff
changeset
|
1433 self.hour, self.minute, self.second, |
|
6671c5039e15
Issue #2736: Added datetime.timestamp() method.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
75590
diff
changeset
|
1434 -1, -1, -1)) + self.microsecond / 1e6 |
|
6671c5039e15
Issue #2736: Added datetime.timestamp() method.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
75590
diff
changeset
|
1435 else: |
|
6671c5039e15
Issue #2736: Added datetime.timestamp() method.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
75590
diff
changeset
|
1436 return (self - _EPOCH).total_seconds() |
|
6671c5039e15
Issue #2736: Added datetime.timestamp() method.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
75590
diff
changeset
|
1437 |
|
63184
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1438 def utctimetuple(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1439 "Return UTC time tuple compatible with time.gmtime()." |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1440 offset = self.utcoffset() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1441 if offset: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1442 self -= offset |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1443 y, m, d = self.year, self.month, self.day |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1444 hh, mm, ss = self.hour, self.minute, self.second |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1445 return _build_struct_time(y, m, d, hh, mm, ss, 0) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1446 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1447 def date(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1448 "Return the date part." |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1449 return date(self._year, self._month, self._day) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1450 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1451 def time(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1452 "Return the time part, with tzinfo None." |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1453 return time(self.hour, self.minute, self.second, self.microsecond) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1454 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1455 def timetz(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1456 "Return the time part, with same tzinfo." |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1457 return time(self.hour, self.minute, self.second, self.microsecond, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1458 self._tzinfo) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1459 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1460 def replace(self, year=None, month=None, day=None, hour=None, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1461 minute=None, second=None, microsecond=None, tzinfo=True): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1462 """Return a new datetime with new values for the specified fields.""" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1463 if year is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1464 year = self.year |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1465 if month is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1466 month = self.month |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1467 if day is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1468 day = self.day |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1469 if hour is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1470 hour = self.hour |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1471 if minute is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1472 minute = self.minute |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1473 if second is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1474 second = self.second |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1475 if microsecond is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1476 microsecond = self.microsecond |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1477 if tzinfo is True: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1478 tzinfo = self.tzinfo |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1479 _check_date_fields(year, month, day) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1480 _check_time_fields(hour, minute, second, microsecond) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1481 _check_tzinfo_arg(tzinfo) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1482 return datetime(year, month, day, hour, minute, second, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1483 microsecond, tzinfo) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1484 |
|
77569
88a5f2730579
Issue #9527: datetime.astimezone() method will now supply a class
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77458
diff
changeset
|
1485 def astimezone(self, tz=None): |
|
88a5f2730579
Issue #9527: datetime.astimezone() method will now supply a class
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77458
diff
changeset
|
1486 if tz is None: |
|
88a5f2730579
Issue #9527: datetime.astimezone() method will now supply a class
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77458
diff
changeset
|
1487 if self.tzinfo is None: |
|
88a5f2730579
Issue #9527: datetime.astimezone() method will now supply a class
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77458
diff
changeset
|
1488 raise ValueError("astimezone() requires an aware datetime") |
|
88a5f2730579
Issue #9527: datetime.astimezone() method will now supply a class
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77458
diff
changeset
|
1489 ts = (self - _EPOCH) // timedelta(seconds=1) |
|
88a5f2730579
Issue #9527: datetime.astimezone() method will now supply a class
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77458
diff
changeset
|
1490 localtm = _time.localtime(ts) |
|
88a5f2730579
Issue #9527: datetime.astimezone() method will now supply a class
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77458
diff
changeset
|
1491 local = datetime(*localtm[:6]) |
|
88a5f2730579
Issue #9527: datetime.astimezone() method will now supply a class
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77458
diff
changeset
|
1492 try: |
|
77570
336c53c1f547
Issue #9527: datetime.astimezone() method will now supply a class
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77569
diff
changeset
|
1493 # Extract TZ data if available |
|
77569
88a5f2730579
Issue #9527: datetime.astimezone() method will now supply a class
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77458
diff
changeset
|
1494 gmtoff = localtm.tm_gmtoff |
|
88a5f2730579
Issue #9527: datetime.astimezone() method will now supply a class
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77458
diff
changeset
|
1495 zone = localtm.tm_zone |
|
88a5f2730579
Issue #9527: datetime.astimezone() method will now supply a class
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77458
diff
changeset
|
1496 except AttributeError: |
|
88a5f2730579
Issue #9527: datetime.astimezone() method will now supply a class
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77458
diff
changeset
|
1497 # Compute UTC offset and compare with the value implied |
|
88a5f2730579
Issue #9527: datetime.astimezone() method will now supply a class
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77458
diff
changeset
|
1498 # by tm_isdst. If the values match, use the zone name |
|
88a5f2730579
Issue #9527: datetime.astimezone() method will now supply a class
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77458
diff
changeset
|
1499 # implied by tm_isdst. |
|
88a5f2730579
Issue #9527: datetime.astimezone() method will now supply a class
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77458
diff
changeset
|
1500 delta = local - datetime(*_time.gmtime(ts)[:6]) |
|
88a5f2730579
Issue #9527: datetime.astimezone() method will now supply a class
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77458
diff
changeset
|
1501 dst = _time.daylight and localtm.tm_isdst > 0 |
|
77582
0f0e3ec22fce
Issue #9527: tm_gmtoff has 'correct' sign.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77570
diff
changeset
|
1502 gmtoff = -(_time.altzone if dst else _time.timezone) |
|
0f0e3ec22fce
Issue #9527: tm_gmtoff has 'correct' sign.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77570
diff
changeset
|
1503 if delta == timedelta(seconds=gmtoff): |
|
77569
88a5f2730579
Issue #9527: datetime.astimezone() method will now supply a class
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77458
diff
changeset
|
1504 tz = timezone(delta, _time.tzname[dst]) |
|
88a5f2730579
Issue #9527: datetime.astimezone() method will now supply a class
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77458
diff
changeset
|
1505 else: |
|
88a5f2730579
Issue #9527: datetime.astimezone() method will now supply a class
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77458
diff
changeset
|
1506 tz = timezone(delta) |
|
88a5f2730579
Issue #9527: datetime.astimezone() method will now supply a class
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77458
diff
changeset
|
1507 else: |
|
77582
0f0e3ec22fce
Issue #9527: tm_gmtoff has 'correct' sign.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77570
diff
changeset
|
1508 tz = timezone(timedelta(seconds=gmtoff), zone) |
|
77570
336c53c1f547
Issue #9527: datetime.astimezone() method will now supply a class
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77569
diff
changeset
|
1509 |
|
77569
88a5f2730579
Issue #9527: datetime.astimezone() method will now supply a class
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77458
diff
changeset
|
1510 elif not isinstance(tz, tzinfo): |
|
63184
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1511 raise TypeError("tz argument must be an instance of tzinfo") |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1512 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1513 mytz = self.tzinfo |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1514 if mytz is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1515 raise ValueError("astimezone() requires an aware datetime") |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1516 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1517 if tz is mytz: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1518 return self |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1519 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1520 # Convert self to UTC, and attach the new time zone object. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1521 myoffset = self.utcoffset() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1522 if myoffset is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1523 raise ValueError("astimezone() requires an aware datetime") |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1524 utc = (self - myoffset).replace(tzinfo=tz) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1525 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1526 # Convert from UTC to tz's local time. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1527 return tz.fromutc(utc) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1528 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1529 # Ways to produce a string. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1530 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1531 def ctime(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1532 "Return ctime() style string." |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1533 weekday = self.toordinal() % 7 or 7 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1534 return "%s %s %2d %02d:%02d:%02d %04d" % ( |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1535 _DAYNAMES[weekday], |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1536 _MONTHNAMES[self._month], |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1537 self._day, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1538 self._hour, self._minute, self._second, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1539 self._year) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1540 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1541 def isoformat(self, sep='T'): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1542 """Return the time formatted according to ISO. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1543 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1544 This is 'YYYY-MM-DD HH:MM:SS.mmmmmm', or 'YYYY-MM-DD HH:MM:SS' if |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1545 self.microsecond == 0. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1546 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1547 If self.tzinfo is not None, the UTC offset is also attached, giving |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1548 'YYYY-MM-DD HH:MM:SS.mmmmmm+HH:MM' or 'YYYY-MM-DD HH:MM:SS+HH:MM'. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1549 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1550 Optional argument sep specifies the separator between date and |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1551 time, default 'T'. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1552 """ |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1553 s = ("%04d-%02d-%02d%c" % (self._year, self._month, self._day, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1554 sep) + |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1555 _format_time(self._hour, self._minute, self._second, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1556 self._microsecond)) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1557 off = self.utcoffset() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1558 if off is not None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1559 if off.days < 0: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1560 sign = "-" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1561 off = -off |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1562 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1563 sign = "+" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1564 hh, mm = divmod(off, timedelta(hours=1)) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1565 assert not mm % timedelta(minutes=1), "whole minute" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1566 mm //= timedelta(minutes=1) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1567 s += "%s%02d:%02d" % (sign, hh, mm) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1568 return s |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1569 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1570 def __repr__(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1571 """Convert to formal string, for repr().""" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1572 L = [self._year, self._month, self._day, # These are never zero |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1573 self._hour, self._minute, self._second, self._microsecond] |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1574 if L[-1] == 0: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1575 del L[-1] |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1576 if L[-1] == 0: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1577 del L[-1] |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1578 s = ", ".join(map(str, L)) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1579 s = "%s(%s)" % ('datetime.' + self.__class__.__name__, s) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1580 if self._tzinfo is not None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1581 assert s[-1:] == ")" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1582 s = s[:-1] + ", tzinfo=%r" % self._tzinfo + ")" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1583 return s |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1584 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1585 def __str__(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1586 "Convert to string, for str()." |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1587 return self.isoformat(sep=' ') |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1588 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1589 @classmethod |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1590 def strptime(cls, date_string, format): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1591 'string, format -> new datetime parsed from a string (like time.strptime()).' |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1592 import _strptime |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1593 return _strptime._strptime_datetime(cls, date_string, format) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1594 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1595 def utcoffset(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1596 """Return the timezone offset in minutes east of UTC (negative west of |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1597 UTC).""" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1598 if self._tzinfo is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1599 return None |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1600 offset = self._tzinfo.utcoffset(self) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1601 _check_utc_offset("utcoffset", offset) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1602 return offset |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1603 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1604 def tzname(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1605 """Return the timezone name. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1606 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1607 Note that the name is 100% informational -- there's no requirement that |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1608 it mean anything in particular. For example, "GMT", "UTC", "-500", |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1609 "-5:00", "EDT", "US/Eastern", "America/New York" are all valid replies. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1610 """ |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1611 name = _call_tzinfo_method(self._tzinfo, "tzname", self) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1612 _check_tzname(name) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1613 return name |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1614 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1615 def dst(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1616 """Return 0 if DST is not in effect, or the DST offset (in minutes |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1617 eastward) if DST is in effect. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1618 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1619 This is purely informational; the DST offset has already been added to |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1620 the UTC offset returned by utcoffset() if applicable, so there's no |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1621 need to consult dst() unless you're interested in displaying the DST |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1622 info. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1623 """ |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1624 if self._tzinfo is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1625 return None |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1626 offset = self._tzinfo.dst(self) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1627 _check_utc_offset("dst", offset) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1628 return offset |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1629 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1630 # Comparisons of datetime objects with other. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1631 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1632 def __eq__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1633 if isinstance(other, datetime): |
|
77458
8272699973cb
Issue #15006: Allow equality comparison between naive and aware time
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77451
diff
changeset
|
1634 return self._cmp(other, allow_mixed=True) == 0 |
|
63184
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1635 elif not isinstance(other, date): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1636 return NotImplemented |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1637 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1638 return False |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1639 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1640 def __ne__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1641 if isinstance(other, datetime): |
|
77458
8272699973cb
Issue #15006: Allow equality comparison between naive and aware time
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77451
diff
changeset
|
1642 return self._cmp(other, allow_mixed=True) != 0 |
|
63184
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1643 elif not isinstance(other, date): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1644 return NotImplemented |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1645 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1646 return True |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1647 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1648 def __le__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1649 if isinstance(other, datetime): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1650 return self._cmp(other) <= 0 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1651 elif not isinstance(other, date): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1652 return NotImplemented |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1653 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1654 _cmperror(self, other) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1655 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1656 def __lt__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1657 if isinstance(other, datetime): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1658 return self._cmp(other) < 0 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1659 elif not isinstance(other, date): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1660 return NotImplemented |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1661 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1662 _cmperror(self, other) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1663 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1664 def __ge__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1665 if isinstance(other, datetime): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1666 return self._cmp(other) >= 0 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1667 elif not isinstance(other, date): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1668 return NotImplemented |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1669 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1670 _cmperror(self, other) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1671 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1672 def __gt__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1673 if isinstance(other, datetime): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1674 return self._cmp(other) > 0 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1675 elif not isinstance(other, date): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1676 return NotImplemented |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1677 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1678 _cmperror(self, other) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1679 |
|
77458
8272699973cb
Issue #15006: Allow equality comparison between naive and aware time
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77451
diff
changeset
|
1680 def _cmp(self, other, allow_mixed=False): |
|
63184
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1681 assert isinstance(other, datetime) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1682 mytz = self._tzinfo |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1683 ottz = other._tzinfo |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1684 myoff = otoff = None |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1685 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1686 if mytz is ottz: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1687 base_compare = True |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1688 else: |
|
77451
14028bdd80bc
Removed redundant code
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77384
diff
changeset
|
1689 myoff = self.utcoffset() |
|
14028bdd80bc
Removed redundant code
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77384
diff
changeset
|
1690 otoff = other.utcoffset() |
|
63184
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1691 base_compare = myoff == otoff |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1692 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1693 if base_compare: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1694 return _cmp((self._year, self._month, self._day, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1695 self._hour, self._minute, self._second, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1696 self._microsecond), |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1697 (other._year, other._month, other._day, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1698 other._hour, other._minute, other._second, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1699 other._microsecond)) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1700 if myoff is None or otoff is None: |
|
77458
8272699973cb
Issue #15006: Allow equality comparison between naive and aware time
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77451
diff
changeset
|
1701 if allow_mixed: |
|
8272699973cb
Issue #15006: Allow equality comparison between naive and aware time
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77451
diff
changeset
|
1702 return 2 # arbitrary non-zero value |
|
8272699973cb
Issue #15006: Allow equality comparison between naive and aware time
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77451
diff
changeset
|
1703 else: |
|
8272699973cb
Issue #15006: Allow equality comparison between naive and aware time
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
77451
diff
changeset
|
1704 raise TypeError("cannot compare naive and aware datetimes") |
|
63184
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1705 # XXX What follows could be done more efficiently... |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1706 diff = self - other # this will take offsets into account |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1707 if diff.days < 0: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1708 return -1 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1709 return diff and 1 or 0 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1710 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1711 def __add__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1712 "Add a datetime and a timedelta." |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1713 if not isinstance(other, timedelta): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1714 return NotImplemented |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1715 delta = timedelta(self.toordinal(), |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1716 hours=self._hour, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1717 minutes=self._minute, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1718 seconds=self._second, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1719 microseconds=self._microsecond) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1720 delta += other |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1721 hour, rem = divmod(delta.seconds, 3600) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1722 minute, second = divmod(rem, 60) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1723 if 0 < delta.days <= _MAXORDINAL: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1724 return datetime.combine(date.fromordinal(delta.days), |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1725 time(hour, minute, second, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1726 delta.microseconds, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1727 tzinfo=self._tzinfo)) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1728 raise OverflowError("result out of range") |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1729 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1730 __radd__ = __add__ |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1731 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1732 def __sub__(self, other): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1733 "Subtract two datetimes, or a datetime and a timedelta." |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1734 if not isinstance(other, datetime): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1735 if isinstance(other, timedelta): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1736 return self + -other |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1737 return NotImplemented |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1738 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1739 days1 = self.toordinal() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1740 days2 = other.toordinal() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1741 secs1 = self._second + self._minute * 60 + self._hour * 3600 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1742 secs2 = other._second + other._minute * 60 + other._hour * 3600 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1743 base = timedelta(days1 - days2, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1744 secs1 - secs2, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1745 self._microsecond - other._microsecond) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1746 if self._tzinfo is other._tzinfo: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1747 return base |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1748 myoff = self.utcoffset() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1749 otoff = other.utcoffset() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1750 if myoff == otoff: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1751 return base |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1752 if myoff is None or otoff is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1753 raise TypeError("cannot mix naive and timezone-aware time") |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1754 return base + otoff - myoff |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1755 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1756 def __hash__(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1757 tzoff = self.utcoffset() |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1758 if tzoff is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1759 return hash(self._getstate()[0]) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1760 days = _ymd2ord(self.year, self.month, self.day) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1761 seconds = self.hour * 3600 + self.minute * 60 + self.second |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1762 return hash(timedelta(days, seconds, self.microsecond) - tzoff) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1763 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1764 # Pickle support. |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1765 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1766 def _getstate(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1767 yhi, ylo = divmod(self._year, 256) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1768 us2, us3 = divmod(self._microsecond, 256) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1769 us1, us2 = divmod(us2, 256) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1770 basestate = bytes([yhi, ylo, self._month, self._day, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1771 self._hour, self._minute, self._second, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1772 us1, us2, us3]) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1773 if self._tzinfo is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1774 return (basestate,) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1775 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1776 return (basestate, self._tzinfo) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1777 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1778 def __setstate(self, string, tzinfo): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1779 (yhi, ylo, self._month, self._day, self._hour, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1780 self._minute, self._second, us1, us2, us3) = string |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1781 self._year = yhi * 256 + ylo |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1782 self._microsecond = (((us1 << 8) | us2) << 8) | us3 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1783 if tzinfo is None or isinstance(tzinfo, _tzinfo_class): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1784 self._tzinfo = tzinfo |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1785 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1786 raise TypeError("bad tzinfo state arg %r" % tzinfo) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1787 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1788 def __reduce__(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1789 return (self.__class__, self._getstate()) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1790 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1791 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1792 datetime.min = datetime(1, 1, 1) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1793 datetime.max = datetime(9999, 12, 31, 23, 59, 59, 999999) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1794 datetime.resolution = timedelta(microseconds=1) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1795 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1796 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1797 def _isoweek1monday(year): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1798 # Helper to calculate the day number of the Monday starting week 1 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1799 # XXX This could be done more efficiently |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1800 THURSDAY = 3 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1801 firstday = _ymd2ord(year, 1, 1) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1802 firstweekday = (firstday + 6) % 7 # See weekday() above |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1803 week1monday = firstday - firstweekday |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1804 if firstweekday > THURSDAY: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1805 week1monday += 7 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1806 return week1monday |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1807 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1808 class timezone(tzinfo): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1809 __slots__ = '_offset', '_name' |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1810 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1811 # Sentinel value to disallow None |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1812 _Omitted = object() |
|
65319
078e2e47e52b
Issue 9183: Intern UTC timezone.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
64883
diff
changeset
|
1813 def __new__(cls, offset, name=_Omitted): |
|
078e2e47e52b
Issue 9183: Intern UTC timezone.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
64883
diff
changeset
|
1814 if not isinstance(offset, timedelta): |
|
078e2e47e52b
Issue 9183: Intern UTC timezone.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
64883
diff
changeset
|
1815 raise TypeError("offset must be a timedelta") |
|
078e2e47e52b
Issue 9183: Intern UTC timezone.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
64883
diff
changeset
|
1816 if name is cls._Omitted: |
|
078e2e47e52b
Issue 9183: Intern UTC timezone.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
64883
diff
changeset
|
1817 if not offset: |
|
078e2e47e52b
Issue 9183: Intern UTC timezone.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
64883
diff
changeset
|
1818 return cls.utc |
|
63184
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1819 name = None |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1820 elif not isinstance(name, str): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1821 raise TypeError("name must be a string") |
|
65319
078e2e47e52b
Issue 9183: Intern UTC timezone.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
64883
diff
changeset
|
1822 if not cls._minoffset <= offset <= cls._maxoffset: |
|
078e2e47e52b
Issue 9183: Intern UTC timezone.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
64883
diff
changeset
|
1823 raise ValueError("offset must be a timedelta" |
|
078e2e47e52b
Issue 9183: Intern UTC timezone.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
64883
diff
changeset
|
1824 " strictly between -timedelta(hours=24) and" |
|
078e2e47e52b
Issue 9183: Intern UTC timezone.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
64883
diff
changeset
|
1825 " timedelta(hours=24).") |
|
078e2e47e52b
Issue 9183: Intern UTC timezone.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
64883
diff
changeset
|
1826 if (offset.microseconds != 0 or |
|
078e2e47e52b
Issue 9183: Intern UTC timezone.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
64883
diff
changeset
|
1827 offset.seconds % 60 != 0): |
|
078e2e47e52b
Issue 9183: Intern UTC timezone.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
64883
diff
changeset
|
1828 raise ValueError("offset must be a timedelta" |
|
078e2e47e52b
Issue 9183: Intern UTC timezone.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
64883
diff
changeset
|
1829 " representing a whole number of minutes") |
|
078e2e47e52b
Issue 9183: Intern UTC timezone.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
64883
diff
changeset
|
1830 return cls._create(offset, name) |
|
63184
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1831 |
|
65319
078e2e47e52b
Issue 9183: Intern UTC timezone.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
64883
diff
changeset
|
1832 @classmethod |
|
078e2e47e52b
Issue 9183: Intern UTC timezone.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
64883
diff
changeset
|
1833 def _create(cls, offset, name=None): |
|
078e2e47e52b
Issue 9183: Intern UTC timezone.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
64883
diff
changeset
|
1834 self = tzinfo.__new__(cls) |
|
078e2e47e52b
Issue 9183: Intern UTC timezone.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
64883
diff
changeset
|
1835 self._offset = offset |
|
63184
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1836 self._name = name |
|
65319
078e2e47e52b
Issue 9183: Intern UTC timezone.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
64883
diff
changeset
|
1837 return self |
|
63184
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1838 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1839 def __getinitargs__(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1840 """pickle support""" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1841 if self._name is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1842 return (self._offset,) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1843 return (self._offset, self._name) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1844 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1845 def __eq__(self, other): |
|
79119
ec77f8fb9958
Closes #15973: fix a segmentation fault when comparing timezone objects.
Georg Brandl <georg@python.org>
parents:
77582
diff
changeset
|
1846 if type(other) != timezone: |
|
ec77f8fb9958
Closes #15973: fix a segmentation fault when comparing timezone objects.
Georg Brandl <georg@python.org>
parents:
77582
diff
changeset
|
1847 return False |
|
63184
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1848 return self._offset == other._offset |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1849 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1850 def __hash__(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1851 return hash(self._offset) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1852 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1853 def __repr__(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1854 """Convert to formal string, for repr(). |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1855 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1856 >>> tz = timezone.utc |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1857 >>> repr(tz) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1858 'datetime.timezone.utc' |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1859 >>> tz = timezone(timedelta(hours=-5), 'EST') |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1860 >>> repr(tz) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1861 "datetime.timezone(datetime.timedelta(-1, 68400), 'EST')" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1862 """ |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1863 if self is self.utc: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1864 return 'datetime.timezone.utc' |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1865 if self._name is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1866 return "%s(%r)" % ('datetime.' + self.__class__.__name__, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1867 self._offset) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1868 return "%s(%r, %r)" % ('datetime.' + self.__class__.__name__, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1869 self._offset, self._name) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1870 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1871 def __str__(self): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1872 return self.tzname(None) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1873 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1874 def utcoffset(self, dt): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1875 if isinstance(dt, datetime) or dt is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1876 return self._offset |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1877 raise TypeError("utcoffset() argument must be a datetime instance" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1878 " or None") |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1879 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1880 def tzname(self, dt): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1881 if isinstance(dt, datetime) or dt is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1882 if self._name is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1883 return self._name_from_offset(self._offset) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1884 return self._name |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1885 raise TypeError("tzname() argument must be a datetime instance" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1886 " or None") |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1887 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1888 def dst(self, dt): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1889 if isinstance(dt, datetime) or dt is None: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1890 return None |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1891 raise TypeError("dst() argument must be a datetime instance" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1892 " or None") |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1893 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1894 def fromutc(self, dt): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1895 if isinstance(dt, datetime): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1896 if dt.tzinfo is not self: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1897 raise ValueError("fromutc: dt.tzinfo " |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1898 "is not self") |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1899 return dt + self._offset |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1900 raise TypeError("fromutc() argument must be a datetime instance" |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1901 " or None") |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1902 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1903 _maxoffset = timedelta(hours=23, minutes=59) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1904 _minoffset = -_maxoffset |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1905 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1906 @staticmethod |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1907 def _name_from_offset(delta): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1908 if delta < timedelta(0): |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1909 sign = '-' |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1910 delta = -delta |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1911 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1912 sign = '+' |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1913 hours, rest = divmod(delta, timedelta(hours=1)) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1914 minutes = rest // timedelta(minutes=1) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1915 return 'UTC{}{:02d}:{:02d}'.format(sign, hours, minutes) |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1916 |
|
65319
078e2e47e52b
Issue 9183: Intern UTC timezone.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
64883
diff
changeset
|
1917 timezone.utc = timezone._create(timedelta(0)) |
|
078e2e47e52b
Issue 9183: Intern UTC timezone.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
64883
diff
changeset
|
1918 timezone.min = timezone._create(timezone._minoffset) |
|
078e2e47e52b
Issue 9183: Intern UTC timezone.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
64883
diff
changeset
|
1919 timezone.max = timezone._create(timezone._maxoffset) |
|
77384
6671c5039e15
Issue #2736: Added datetime.timestamp() method.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
75590
diff
changeset
|
1920 _EPOCH = datetime(1970, 1, 1, tzinfo=timezone.utc) |
|
63184
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1921 |
|
82957
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1922 # Some time zone algebra. For a datetime x, let |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1923 # x.n = x stripped of its timezone -- its naive time. |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1924 # x.o = x.utcoffset(), and assuming that doesn't raise an exception or |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1925 # return None |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1926 # x.d = x.dst(), and assuming that doesn't raise an exception or |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1927 # return None |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1928 # x.s = x's standard offset, x.o - x.d |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1929 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1930 # Now some derived rules, where k is a duration (timedelta). |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1931 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1932 # 1. x.o = x.s + x.d |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1933 # This follows from the definition of x.s. |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1934 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1935 # 2. If x and y have the same tzinfo member, x.s = y.s. |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1936 # This is actually a requirement, an assumption we need to make about |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1937 # sane tzinfo classes. |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1938 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1939 # 3. The naive UTC time corresponding to x is x.n - x.o. |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1940 # This is again a requirement for a sane tzinfo class. |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1941 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1942 # 4. (x+k).s = x.s |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1943 # This follows from #2, and that datimetimetz+timedelta preserves tzinfo. |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1944 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1945 # 5. (x+k).n = x.n + k |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1946 # Again follows from how arithmetic is defined. |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1947 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1948 # Now we can explain tz.fromutc(x). Let's assume it's an interesting case |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1949 # (meaning that the various tzinfo methods exist, and don't blow up or return |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1950 # None when called). |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1951 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1952 # The function wants to return a datetime y with timezone tz, equivalent to x. |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1953 # x is already in UTC. |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1954 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1955 # By #3, we want |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1956 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1957 # y.n - y.o = x.n [1] |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1958 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1959 # The algorithm starts by attaching tz to x.n, and calling that y. So |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1960 # x.n = y.n at the start. Then it wants to add a duration k to y, so that [1] |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1961 # becomes true; in effect, we want to solve [2] for k: |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1962 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1963 # (y+k).n - (y+k).o = x.n [2] |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1964 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1965 # By #1, this is the same as |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1966 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1967 # (y+k).n - ((y+k).s + (y+k).d) = x.n [3] |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1968 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1969 # By #5, (y+k).n = y.n + k, which equals x.n + k because x.n=y.n at the start. |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1970 # Substituting that into [3], |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1971 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1972 # x.n + k - (y+k).s - (y+k).d = x.n; the x.n terms cancel, leaving |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1973 # k - (y+k).s - (y+k).d = 0; rearranging, |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1974 # k = (y+k).s - (y+k).d; by #4, (y+k).s == y.s, so |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1975 # k = y.s - (y+k).d |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1976 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1977 # On the RHS, (y+k).d can't be computed directly, but y.s can be, and we |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1978 # approximate k by ignoring the (y+k).d term at first. Note that k can't be |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1979 # very large, since all offset-returning methods return a duration of magnitude |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1980 # less than 24 hours. For that reason, if y is firmly in std time, (y+k).d must |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1981 # be 0, so ignoring it has no consequence then. |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1982 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1983 # In any case, the new value is |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1984 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1985 # z = y + y.s [4] |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1986 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1987 # It's helpful to step back at look at [4] from a higher level: it's simply |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1988 # mapping from UTC to tz's standard time. |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1989 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1990 # At this point, if |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1991 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1992 # z.n - z.o = x.n [5] |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1993 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1994 # we have an equivalent time, and are almost done. The insecurity here is |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1995 # at the start of daylight time. Picture US Eastern for concreteness. The wall |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1996 # time jumps from 1:59 to 3:00, and wall hours of the form 2:MM don't make good |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1997 # sense then. The docs ask that an Eastern tzinfo class consider such a time to |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1998 # be EDT (because it's "after 2"), which is a redundant spelling of 1:MM EST |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
1999 # on the day DST starts. We want to return the 1:MM EST spelling because that's |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2000 # the only spelling that makes sense on the local wall clock. |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2001 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2002 # In fact, if [5] holds at this point, we do have the standard-time spelling, |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2003 # but that takes a bit of proof. We first prove a stronger result. What's the |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2004 # difference between the LHS and RHS of [5]? Let |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2005 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2006 # diff = x.n - (z.n - z.o) [6] |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2007 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2008 # Now |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2009 # z.n = by [4] |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2010 # (y + y.s).n = by #5 |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2011 # y.n + y.s = since y.n = x.n |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2012 # x.n + y.s = since z and y are have the same tzinfo member, |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2013 # y.s = z.s by #2 |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2014 # x.n + z.s |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2015 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2016 # Plugging that back into [6] gives |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2017 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2018 # diff = |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2019 # x.n - ((x.n + z.s) - z.o) = expanding |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2020 # x.n - x.n - z.s + z.o = cancelling |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2021 # - z.s + z.o = by #2 |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2022 # z.d |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2023 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2024 # So diff = z.d. |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2025 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2026 # If [5] is true now, diff = 0, so z.d = 0 too, and we have the standard-time |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2027 # spelling we wanted in the endcase described above. We're done. Contrarily, |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2028 # if z.d = 0, then we have a UTC equivalent, and are also done. |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2029 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2030 # If [5] is not true now, diff = z.d != 0, and z.d is the offset we need to |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2031 # add to z (in effect, z is in tz's standard time, and we need to shift the |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2032 # local clock into tz's daylight time). |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2033 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2034 # Let |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2035 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2036 # z' = z + z.d = z + diff [7] |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2037 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2038 # and we can again ask whether |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2039 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2040 # z'.n - z'.o = x.n [8] |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2041 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2042 # If so, we're done. If not, the tzinfo class is insane, according to the |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2043 # assumptions we've made. This also requires a bit of proof. As before, let's |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2044 # compute the difference between the LHS and RHS of [8] (and skipping some of |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2045 # the justifications for the kinds of substitutions we've done several times |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2046 # already): |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2047 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2048 # diff' = x.n - (z'.n - z'.o) = replacing z'.n via [7] |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2049 # x.n - (z.n + diff - z'.o) = replacing diff via [6] |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2050 # x.n - (z.n + x.n - (z.n - z.o) - z'.o) = |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2051 # x.n - z.n - x.n + z.n - z.o + z'.o = cancel x.n |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2052 # - z.n + z.n - z.o + z'.o = cancel z.n |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2053 # - z.o + z'.o = #1 twice |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2054 # -z.s - z.d + z'.s + z'.d = z and z' have same tzinfo |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2055 # z'.d - z.d |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2056 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2057 # So z' is UTC-equivalent to x iff z'.d = z.d at this point. If they are equal, |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2058 # we've found the UTC-equivalent so are done. In fact, we stop with [7] and |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2059 # return z', not bothering to compute z'.d. |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2060 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2061 # How could z.d and z'd differ? z' = z + z.d [7], so merely moving z' by |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2062 # a dst() offset, and starting *from* a time already in DST (we know z.d != 0), |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2063 # would have to change the result dst() returns: we start in DST, and moving |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2064 # a little further into it takes us out of DST. |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2065 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2066 # There isn't a sane case where this can happen. The closest it gets is at |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2067 # the end of DST, where there's an hour in UTC with no spelling in a hybrid |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2068 # tzinfo class. In US Eastern, that's 5:MM UTC = 0:MM EST = 1:MM EDT. During |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2069 # that hour, on an Eastern clock 1:MM is taken as being in standard time (6:MM |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2070 # UTC) because the docs insist on that, but 0:MM is taken as being in daylight |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2071 # time (4:MM UTC). There is no local time mapping to 5:MM UTC. The local |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2072 # clock jumps from 1:59 back to 1:00 again, and repeats the 1:MM hour in |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2073 # standard time. Since that's what the local clock *does*, we want to map both |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2074 # UTC hours 5:MM and 6:MM to 1:MM Eastern. The result is ambiguous |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2075 # in local time, but so it goes -- it's the way the local clock works. |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2076 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2077 # When x = 5:MM UTC is the input to this algorithm, x.o=0, y.o=-5 and y.d=0, |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2078 # so z=0:MM. z.d=60 (minutes) then, so [5] doesn't hold and we keep going. |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2079 # z' = z + z.d = 1:MM then, and z'.d=0, and z'.d - z.d = -60 != 0 so [8] |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2080 # (correctly) concludes that z' is not UTC-equivalent to x. |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2081 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2082 # Because we know z.d said z was in daylight time (else [5] would have held and |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2083 # we would have stopped then), and we know z.d != z'.d (else [8] would have held |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2084 # and we have stopped then), and there are only 2 possible values dst() can |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2085 # return in Eastern, it follows that z'.d must be 0 (which it is in the example, |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2086 # but the reasoning doesn't depend on the example -- it depends on there being |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2087 # two possible dst() outcomes, one zero and the other non-zero). Therefore |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2088 # z' must be in standard time, and is the spelling we want in this case. |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2089 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2090 # Note again that z' is not UTC-equivalent as far as the hybrid tzinfo class is |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2091 # concerned (because it takes z' as being in standard time rather than the |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2092 # daylight time we intend here), but returning it gives the real-life "local |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2093 # clock repeats an hour" behavior when mapping the "unspellable" UTC hour into |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2094 # tz. |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2095 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2096 # When the input is 6:MM, z=1:MM and z.d=0, and we stop at once, again with |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2097 # the 1:MM standard time spelling we want. |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2098 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2099 # So how can this break? One of the assumptions must be violated. Two |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2100 # possibilities: |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2101 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2102 # 1) [2] effectively says that y.s is invariant across all y belong to a given |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2103 # time zone. This isn't true if, for political reasons or continental drift, |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2104 # a region decides to change its base offset from UTC. |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2105 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2106 # 2) There may be versions of "double daylight" time where the tail end of |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2107 # the analysis gives up a step too early. I haven't thought about that |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2108 # enough to say. |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2109 # |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2110 # In any case, it's clear that the default fromutc() is strong enough to handle |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2111 # "almost all" time zones: so long as the standard offset is invariant, it |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2112 # doesn't matter if daylight time transition points change from year to year, or |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2113 # if daylight time is skipped in some years; it doesn't matter how large or |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2114 # small dst() may get within its bounds; and it doesn't even matter if some |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2115 # perverse time zone returns a negative dst()). So a breaking case must be |
|
313bcff51900
Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents:
79136
diff
changeset
|
2116 # pretty bizarre, and a tzinfo subclass can override fromutc() if it is. |
|
63184
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
2117 |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
2118 try: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
2119 from _datetime import * |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
2120 except ImportError: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
2121 pass |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
2122 else: |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
2123 # Clean up unused names |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
2124 del (_DAYNAMES, _DAYS_BEFORE_MONTH, _DAYS_IN_MONTH, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
2125 _DI100Y, _DI400Y, _DI4Y, _MAXORDINAL, _MONTHNAMES, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
2126 _build_struct_time, _call_tzinfo_method, _check_date_fields, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
2127 _check_time_fields, _check_tzinfo_arg, _check_tzname, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
2128 _check_utc_offset, _cmp, _cmperror, _date_class, _days_before_month, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
2129 _days_before_year, _days_in_month, _format_time, _is_leap, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
2130 _isoweek1monday, _math, _ord2ymd, _time, _time_class, _tzinfo_class, |
|
afdb53323065
Issue #7989: Added pure python implementation of the datetime module.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
2131 _wrap_strftime, _ymd2ord) |
|
63187
4ed60c4bc8b6
Use _datetime docstring if _datetime is available.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63184
diff
changeset
|
2132 # XXX Since import * above excludes names that start with _, |
|
4ed60c4bc8b6
Use _datetime docstring if _datetime is available.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63184
diff
changeset
|
2133 # docstring does not get overwritten. In the future, it may be |
|
4ed60c4bc8b6
Use _datetime docstring if _datetime is available.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63184
diff
changeset
|
2134 # appropriate to maintain a single module level docstring and |
|
4ed60c4bc8b6
Use _datetime docstring if _datetime is available.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63184
diff
changeset
|
2135 # remove the following line. |
|
4ed60c4bc8b6
Use _datetime docstring if _datetime is available.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63184
diff
changeset
|
2136 from _datetime import __doc__ |
