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