Refined calendar lookup

svn: r13762
This commit is contained in:
Doug Blank 2009-12-11 15:05:29 +00:00
parent 6d245db5fc
commit 96737bf442

View File

@ -693,7 +693,7 @@ class Date(object):
elif isinstance(calendar, int): elif isinstance(calendar, int):
self.calendar = calendar self.calendar = calendar
else: else:
self.calendar = self.lookup_calendar(calendar) self.calendar = lookup_calendar(calendar)
if modifier is None: if modifier is None:
self.modifier = Date.MOD_NONE self.modifier = Date.MOD_NONE
else: else:
@ -1241,7 +1241,7 @@ class Date(object):
5770 5770
""" """
if calendar_name: if calendar_name:
cal = self.lookup_calendar(calendar_name) cal = lookup_calendar(calendar_name)
else: else:
cal = self.calendar cal = self.calendar
if cal == self.calendar: if cal == self.calendar:
@ -1625,14 +1625,7 @@ class Date(object):
""" """
Lookup calendar name in the list of known calendars, even if translated. Lookup calendar name in the list of known calendars, even if translated.
""" """
calendar_lower = [n.lower() for n in Date.calendar_names] return lookup_calendar(calendar)
ui_lower = [n.lower() for n in Date.ui_calendar_names]
if calendar.lower() in calendar_lower:
return calendar_lower.index(calendar.lower())
elif calendar.lower() in ui_lower:
return ui_lower.index(calendar.lower())
else:
raise AttributeError("invalid calendar: '%s'" % calendar)
def lookup_quality(self, quality): def lookup_quality(self, quality):
""" """
@ -1669,7 +1662,7 @@ class Date(object):
>>> Date("Jan 1 1591").to_calendar("julian") >>> Date("Jan 1 1591").to_calendar("julian")
1590-12-22 (Julian) 1590-12-22 (Julian)
""" """
cal = self.lookup_calendar(calendar_name) cal = lookup_calendar(calendar_name)
retval = Date(self) retval = Date(self)
retval.convert_calendar(cal) retval.convert_calendar(cal)
return retval return retval
@ -1808,3 +1801,18 @@ def __build_date_string(day, mon, year, bce, mmap):
else: else:
retval = "%d %s %d%s" % (day, mmap[mon], year, bce) retval = "%d %s %d%s" % (day, mmap[mon], year, bce)
return retval return retval
def lookup_calendar(calendar):
"""
Find the ID associated with the calendar name.
>>> lookup_calendar("hebrew")
2
"""
for pos, calendar_name in enumerate(Date.calendar_names):
if calendar.lower() == calendar_name.lower():
return pos
for pos, calendar_name in enumerate(Date.ui_calendar_names):
if calendar.lower() == calendar_name.lower():
return pos
raise AttributeError("invalid calendar: '%s'" % calendar)