mirror of
https://git.launchpad.net/ubuntu-dev-tools
synced 2025-03-16 17:41:08 +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:
|
try:
|
||||||
date = convert_date(options.date)
|
date = convert_date(options.date)
|
||||||
except ValueError:
|
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.")
|
"format.")
|
||||||
|
|
||||||
if options.all:
|
if options.all:
|
||||||
|
@ -66,7 +66,7 @@ def main():
|
|||||||
try:
|
try:
|
||||||
date = convert_date(options.date)
|
date = convert_date(options.date)
|
||||||
except ValueError:
|
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.")
|
"format.")
|
||||||
|
|
||||||
if options.all:
|
if options.all:
|
||||||
|
@ -24,15 +24,18 @@ def convert_date(string):
|
|||||||
if not string:
|
if not string:
|
||||||
date = None
|
date = None
|
||||||
else:
|
else:
|
||||||
try:
|
parts = [int(x) for x in string.split("-")]
|
||||||
(year, month, day) = [int(x) for x in string.split("-")]
|
if len(parts) == 3:
|
||||||
|
(year, month, day) = parts
|
||||||
date = datetime.date(year, month, day)
|
date = datetime.date(year, month, day)
|
||||||
except ValueError:
|
elif len(parts) == 2:
|
||||||
(year, month) = [int(x) for x in string.split("-")]
|
(year, month) = parts
|
||||||
if month == 12:
|
if month == 12:
|
||||||
date = datetime.date(year, month, 31)
|
date = datetime.date(year, month, 31)
|
||||||
else:
|
else:
|
||||||
date = datetime.date(year, month + 1, 1) - datetime.timedelta(1)
|
date = datetime.date(year, month + 1, 1) - datetime.timedelta(1)
|
||||||
|
else:
|
||||||
|
raise ValueError("Date not in ISO 8601 format.")
|
||||||
return date
|
return date
|
||||||
|
|
||||||
def _get_data_dir():
|
def _get_data_dir():
|
||||||
|
Loading…
x
Reference in New Issue
Block a user