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