summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSkip Montanaro <skip@pobox.com>2002-03-15 04:08:38 (GMT)
committerSkip Montanaro <skip@pobox.com>2002-03-15 04:08:38 (GMT)
commit4c8349592d508df26b9a8023f7a78d6874eedb08 (patch)
tree37dcf32acb639ddaaa09b44493ee4b4ebd98c0a4
parent693c6c44c4db6049bc53da51fbd1a084a33a75b2 (diff)
downloadcpython-4c8349592d508df26b9a8023f7a78d6874eedb08.zip
cpython-4c8349592d508df26b9a8023f7a78d6874eedb08.tar.gz
cpython-4c8349592d508df26b9a8023f7a78d6874eedb08.tar.bz2
make _localized_name instances work more like the tuples they replaced. In
particular, negative indexes work and they are limited by the actual length of the names they represent (weekday and month names). This closes bug #503202.
-rw-r--r--Lib/calendar.py17
1 files changed, 12 insertions, 5 deletions
diff --git a/Lib/calendar.py b/Lib/calendar.py
index 0fc60be..477f595 100644
--- a/Lib/calendar.py
+++ b/Lib/calendar.py
@@ -25,18 +25,25 @@ February = 2
mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
class _localized_name:
- def __init__(self, format):
+ def __init__(self, format, len):
self.format = format
+ self.len = len
def __getitem__(self, item):
+ if item > self.len-1 or item < -self.len:
+ raise IndexError
+ if item < 0:
+ item += self.len
return strftime(self.format, (item,)*9).capitalize()
+ def __len__(self):
+ return self.len
# Full and abbreviated names of weekdays
-day_name = _localized_name('%A')
-day_abbr = _localized_name('%a')
+day_name = _localized_name('%A', 7)
+day_abbr = _localized_name('%a', 7)
# Full and abbreviated names of months (1-based arrays!!!)
-month_name = _localized_name('%B')
-month_abbr = _localized_name('%b')
+month_name = _localized_name('%B', 12)
+month_abbr = _localized_name('%b', 12)
# Constants for weekdays
(MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY) = range(7)