annotate Demo/classes/Dates.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents bb3d1bd1e293
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Class Date supplies date objects that support date arithmetic.
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 #
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 # Date(month,day,year) returns a Date object. An instance prints as,
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 # e.g., 'Mon 16 Aug 1993'.
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 #
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 # Addition, subtraction, comparison operators, min, max, and sorting
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 # all work as expected for date objects: int+date or date+int returns
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 # the date `int' days from `date'; date+date raises an exception;
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 # date-int returns the date `int' days before `date'; date2-date1 returns
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 # an integer, the number of days from date1 to date2; int-date raises an
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 # exception; date1 < date2 is true iff date1 occurs before date2 (&
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 # similarly for other comparisons); min(date1,date2) is the earlier of
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 # the two dates and max(date1,date2) the later; and date objects can be
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 # used as dictionary keys.
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 #
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 # Date objects support one visible method, date.weekday(). This returns
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 # the day of the week the date falls on, as a string.
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 #
1966
dd7be8128410 adapted to new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1378
diff changeset
19 # Date objects also have 4 read-only data attributes:
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 # .month in 1..12
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 # .day in 1..31
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 # .year int or long int
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 # .ord the ordinal of the date relative to an arbitrary staring point
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 #
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 # The Dates module also supplies function today(), which returns the
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 # current date as a date object.
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 #
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 # Those entranced by calendar trivia will be disappointed, as no attempt
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 # has been made to accommodate the Julian (etc) system. On the other
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 # hand, at least this package knows that 2000 is a leap year but 2100
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 # isn't, and works fine for years with a hundred decimal digits <wink>.
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 # Tim Peters tim@ksr.com
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 # not speaking for Kendall Square Research Corp
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
1966
dd7be8128410 adapted to new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1378
diff changeset
36 # Adapted to Python 1.1 (where some hacks to overcome coercion are unnecessary)
dd7be8128410 adapted to new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1378
diff changeset
37 # by Guido van Rossum
dd7be8128410 adapted to new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1378
diff changeset
38
29371
8c6d4da5ccd3 Patch #748846: Let Demo/classes/Date.py mention DateTime module.
Martin v. Löwis <martin@v.loewis.de>
parents: 9097
diff changeset
39 # Note that as of Python 2.3, a datetime module is included in the stardard
8c6d4da5ccd3 Patch #748846: Let Demo/classes/Date.py mention DateTime module.
Martin v. Löwis <martin@v.loewis.de>
parents: 9097
diff changeset
40 # library.
8c6d4da5ccd3 Patch #748846: Let Demo/classes/Date.py mention DateTime module.
Martin v. Löwis <martin@v.loewis.de>
parents: 9097
diff changeset
41
1966
dd7be8128410 adapted to new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1378
diff changeset
42 # vi:set tabsize=8:
dd7be8128410 adapted to new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1378
diff changeset
43
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 _MONTH_NAMES = [ 'January', 'February', 'March', 'April', 'May',
9097
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
45 'June', 'July', 'August', 'September', 'October',
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
46 'November', 'December' ]
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
47
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 _DAY_NAMES = [ 'Friday', 'Saturday', 'Sunday', 'Monday',
9097
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
49 'Tuesday', 'Wednesday', 'Thursday' ]
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 _DAYS_IN_MONTH = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 _DAYS_BEFORE_MONTH = []
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 dbm = 0
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 for dim in _DAYS_IN_MONTH:
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 _DAYS_BEFORE_MONTH.append(dbm)
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 dbm = dbm + dim
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 del dbm, dim
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 _INT_TYPES = type(1), type(1L)
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
61
38627
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
62 def _is_leap(year): # 1 if leap year, else 0
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 if year % 4 != 0: return 0
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 if year % 400 == 0: return 1
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 return year % 100 != 0
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
66
38627
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
67 def _days_in_year(year): # number of days in year
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 return 365 + _is_leap(year)
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
69
38627
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
70 def _days_before_year(year): # number of days before year
49162
bb3d1bd1e293 #687648 from Robert Schuppenies: use classic division. From me: don't use string exception; add __main__ section
Andrew M. Kuchling <amk@amk.ca>
parents: 38627
diff changeset
71 return year*365L + (year+3)//4 - (year+99)//100 + (year+399)//400
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
72
38627
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
73 def _days_in_month(month, year): # number of days in month of year
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 if month == 2 and _is_leap(year): return 29
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 return _DAYS_IN_MONTH[month-1]
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
76
38627
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
77 def _days_before_month(month, year): # number of days in year before month
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 return _DAYS_BEFORE_MONTH[month-1] + (month > 2 and _is_leap(year))
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
79
38627
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
80 def _date2num(date): # compute ordinal of date.month,day,year
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
81 return _days_before_year(date.year) + \
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
82 _days_before_month(date.month, date.year) + \
9097
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
83 date.day
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
84
38627
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
85 _DI400Y = _days_before_year(400) # number of days in 400 years
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
86
38627
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
87 def _num2date(n): # return date with ordinal n
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 if type(n) not in _INT_TYPES:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29371
diff changeset
89 raise TypeError, 'argument must be integer: %r' % type(n)
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
90
9097
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
91 ans = Date(1,1,1) # arguments irrelevant; just getting a Date obj
1966
dd7be8128410 adapted to new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1378
diff changeset
92 del ans.ord, ans.month, ans.day, ans.year # un-initialize it
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 ans.ord = n
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
94
49162
bb3d1bd1e293 #687648 from Robert Schuppenies: use classic division. From me: don't use string exception; add __main__ section
Andrew M. Kuchling <amk@amk.ca>
parents: 38627
diff changeset
95 n400 = (n-1)//_DI400Y # # of 400-year blocks preceding
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 year, n = 400 * n400, n - _DI400Y * n400
49162
bb3d1bd1e293 #687648 from Robert Schuppenies: use classic division. From me: don't use string exception; add __main__ section
Andrew M. Kuchling <amk@amk.ca>
parents: 38627
diff changeset
97 more = n // 365
38627
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
98 dby = _days_before_year(more)
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 if dby >= n:
9097
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
100 more = more - 1
38627
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
101 dby = dby - _days_in_year(more)
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 year, n = year + more, int(n - dby)
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
103
9097
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
104 try: year = int(year) # chop to int, if it fits
1966
dd7be8128410 adapted to new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1378
diff changeset
105 except (ValueError, OverflowError): pass
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
106
49162
bb3d1bd1e293 #687648 from Robert Schuppenies: use classic division. From me: don't use string exception; add __main__ section
Andrew M. Kuchling <amk@amk.ca>
parents: 38627
diff changeset
107 month = min(n//29 + 1, 12)
38627
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
108 dbm = _days_before_month(month, year)
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 if dbm >= n:
9097
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
110 month = month - 1
38627
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
111 dbm = dbm - _days_in_month(month, year)
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
112
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 ans.month, ans.day, ans.year = month, n-dbm, year
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 return ans
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
115
38627
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
116 def _num2day(n): # return weekday name of day with ordinal n
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 return _DAY_NAMES[ int(n % 7) ]
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
118
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
119
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 class Date:
38627
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
121 def __init__(self, month, day, year):
9097
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
122 if not 1 <= month <= 12:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29371
diff changeset
123 raise ValueError, 'month must be in 1..12: %r' % (month,)
38627
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
124 dim = _days_in_month(month, year)
9097
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
125 if not 1 <= day <= dim:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29371
diff changeset
126 raise ValueError, 'day must be in 1..%r: %r' % (dim, day)
9097
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
127 self.month, self.day, self.year = month, day, year
38627
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
128 self.ord = _date2num(self)
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
129
1966
dd7be8128410 adapted to new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1378
diff changeset
130 # don't allow setting existing attributes
38627
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
131 def __setattr__(self, name, value):
9097
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
132 if self.__dict__.has_key(name):
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
133 raise AttributeError, 'read-only attribute ' + name
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
134 self.__dict__[name] = value
1966
dd7be8128410 adapted to new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1378
diff changeset
135
38627
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
136 def __cmp__(self, other):
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
137 return cmp(self.ord, other.ord)
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
138
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 # define a hash function so dates can be used as dictionary keys
38627
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
140 def __hash__(self):
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
141 return hash(self.ord)
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
142
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 # print as, e.g., Mon 16 Aug 1993
38627
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
144 def __repr__(self):
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29371
diff changeset
145 return '%.3s %2d %.3s %r' % (
9097
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
146 self.weekday(),
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
147 self.day,
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29371
diff changeset
148 _MONTH_NAMES[self.month-1],
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29371
diff changeset
149 self.year)
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
150
1966
dd7be8128410 adapted to new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1378
diff changeset
151 # Python 1.1 coerces neither int+date nor date+int
38627
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
152 def __add__(self, n):
9097
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
153 if type(n) not in _INT_TYPES:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29371
diff changeset
154 raise TypeError, 'can\'t add %r to date' % type(n)
38627
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
155 return _num2date(self.ord + n)
1966
dd7be8128410 adapted to new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1378
diff changeset
156 __radd__ = __add__ # handle int+date
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
157
1966
dd7be8128410 adapted to new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1378
diff changeset
158 # Python 1.1 coerces neither date-int nor date-date
38627
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
159 def __sub__(self, other):
9097
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
160 if type(other) in _INT_TYPES: # date-int
38627
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
161 return _num2date(self.ord - other)
9097
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
162 else:
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
163 return self.ord - other.ord # date-date
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
164
1966
dd7be8128410 adapted to new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1378
diff changeset
165 # complain about int-date
38627
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
166 def __rsub__(self, other):
9097
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
167 raise TypeError, 'Can\'t subtract date from integer'
1966
dd7be8128410 adapted to new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1378
diff changeset
168
38627
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
169 def weekday(self):
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
170 return _num2day(self.ord)
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
171
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 def today():
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 import time
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 local = time.localtime(time.time())
38627
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
175 return Date(local[1], local[2], local[0])
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
176
49162
bb3d1bd1e293 #687648 from Robert Schuppenies: use classic division. From me: don't use string exception; add __main__ section
Andrew M. Kuchling <amk@amk.ca>
parents: 38627
diff changeset
177 class DateTestError(Exception):
bb3d1bd1e293 #687648 from Robert Schuppenies: use classic division. From me: don't use string exception; add __main__ section
Andrew M. Kuchling <amk@amk.ca>
parents: 38627
diff changeset
178 pass
bb3d1bd1e293 #687648 from Robert Schuppenies: use classic division. From me: don't use string exception; add __main__ section
Andrew M. Kuchling <amk@amk.ca>
parents: 38627
diff changeset
179
38627
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
180 def test(firstyear, lastyear):
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 a = Date(9,30,1913)
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 b = Date(9,30,1914)
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29371
diff changeset
183 if repr(a) != 'Tue 30 Sep 1913':
9097
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
184 raise DateTestError, '__repr__ failure'
1966
dd7be8128410 adapted to new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1378
diff changeset
185 if (not a < b) or a == b or a > b or b != b:
9097
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
186 raise DateTestError, '__cmp__ failure'
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 if a+365 != b or 365+a != b:
9097
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
188 raise DateTestError, '__add__ failure'
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 if b-a != 365 or b-365 != a:
9097
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
190 raise DateTestError, '__sub__ failure'
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 try:
9097
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
192 x = 1 - a
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
193 raise DateTestError, 'int-date should have failed'
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 except TypeError:
9097
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
195 pass
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 try:
9097
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
197 x = a + b
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
198 raise DateTestError, 'date+date should have failed'
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 except TypeError:
9097
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
200 pass
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 if a.weekday() != 'Tuesday':
9097
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
202 raise DateTestError, 'weekday() failure'
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 if max(a,b) is not b or min(a,b) is not a:
9097
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
204 raise DateTestError, 'min/max failure'
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 d = {a-1:b, b:a+1}
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 if d[b-366] != b or d[a+(b-a)] != Date(10,1,1913):
9097
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
207 raise DateTestError, 'dictionary failure'
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
208
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 # verify date<->number conversions for first and last days for
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 # all years in firstyear .. lastyear
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
211
38627
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 31515
diff changeset
212 lord = _days_before_year(firstyear)
1378
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 y = firstyear
b03d00e24c3c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 while y <= lastyear:
9097
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
215 ford = lord + 1
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
216 lord = ford + _days_in_year(y) - 1
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
217 fd, ld = Date(1,1,y), Date(12,31,y)
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
218 if (fd.ord,ld.ord) != (ford,lord):
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
219 raise DateTestError, ('date->num failed', y)
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
220 fd, ld = _num2date(ford), _num2date(lord)
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
221 if (1,1,y,12,31,y) != \
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
222 (fd.month,fd.day,fd.year,ld.month,ld.day,ld.year):
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
223 raise DateTestError, ('num->date failed', y)
4278561bd5e8 nannified.
Guido van Rossum <guido@python.org>
parents: 1966
diff changeset
224 y = y + 1
49162
bb3d1bd1e293 #687648 from Robert Schuppenies: use classic division. From me: don't use string exception; add __main__ section
Andrew M. Kuchling <amk@amk.ca>
parents: 38627
diff changeset
225
bb3d1bd1e293 #687648 from Robert Schuppenies: use classic division. From me: don't use string exception; add __main__ section
Andrew M. Kuchling <amk@amk.ca>
parents: 38627
diff changeset
226 if __name__ == '__main__':
bb3d1bd1e293 #687648 from Robert Schuppenies: use classic division. From me: don't use string exception; add __main__ section
Andrew M. Kuchling <amk@amk.ca>
parents: 38627
diff changeset
227 test(1850, 2150)