mirror of
https://git.launchpad.net/ubuntu-dev-tools
synced 2025-03-13 08:01:09 +00:00
distro_info.py: Improve convert_date logic (to avoid try statement).
This commit is contained in:
parent
876d8fdd8e
commit
88c4a0fc7f
@ -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:
|
||||
|
@ -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:
|
||||
|
@ -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():
|
||||
|
Loading…
x
Reference in New Issue
Block a user