distro_info.py: Improve convert_date logic (to avoid try statement).

This commit is contained in:
Benjamin Drung 2011-01-22 12:03:30 +01:00
parent 876d8fdd8e
commit 88c4a0fc7f
3 changed files with 9 additions and 6 deletions

View File

@ -69,7 +69,7 @@ def main():
try:
date = convert_date(options.date)
except ValueError:
parser.error("Option --date needs to be an date in ISO 8601 "
parser.error("Option --date needs to be a date in ISO 8601 "
"format.")
if options.all:

View File

@ -66,7 +66,7 @@ def main():
try:
date = convert_date(options.date)
except ValueError:
parser.error("Option --date needs to be an date in ISO 8601 "
parser.error("Option --date needs to be a date in ISO 8601 "
"format.")
if options.all:

View File

@ -24,15 +24,18 @@ def convert_date(string):
if not string:
date = None
else:
try:
(year, month, day) = [int(x) for x in string.split("-")]
parts = [int(x) for x in string.split("-")]
if len(parts) == 3:
(year, month, day) = parts
date = datetime.date(year, month, day)
except ValueError:
(year, month) = [int(x) for x in string.split("-")]
elif len(parts) == 2:
(year, month) = parts
if month == 12:
date = datetime.date(year, month, 31)
else:
date = datetime.date(year, month + 1, 1) - datetime.timedelta(1)
else:
raise ValueError("Date not in ISO 8601 format.")
return date
def _get_data_dir():