annotate Lib/datetime.py @ 73798:6cde416ef03b

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