config: Assume abbreviated dates are in the past.

This commit is contained in:
Brett Smith 2017-06-22 15:54:24 -04:00
parent 419f52abe3
commit 8ab2373ba1
3 changed files with 21 additions and 7 deletions

View file

@ -65,7 +65,16 @@ class Configuration:
if (len(numbers) > 3) or (len(seen_seps) > 1):
raise ValueError("can't parse date from {!r}".format(s))
replacements = dict(zip(['day', 'month', 'year'], reversed(numbers)))
return self.TODAY.replace(**replacements)
retval = self.TODAY.replace(**replacements)
if retval > self.TODAY:
if 'year' in replacements:
pass
elif 'month' in replacements:
retval = retval.replace(year=retval.year - 1)
else:
retval -= datetime.timedelta(days=replacements['day'])
retval = retval.replace(**replacements)
return retval
def _build_argparser(self):
prog_parser = argparse.ArgumentParser()

View file

@ -5,7 +5,7 @@ from setuptools import setup
setup(
name='oxrlib',
description="Library to query the Open Exchange Rates (OXR) API",
version='1.5',
version='1.6',
author='Brett Smith',
author_email='brettcsmith@brettcsmith.org',
license='GNU AGPLv3+',

View file

@ -89,16 +89,21 @@ def test_historical_argparsing_failure(arglist, any_date):
assert not vars(config.args), "bad arglist succeeded"
@pytest.mark.parametrize('date_s,expect_year,expect_month,expect_day', [
('5', 1965, 4, 5),
('05', 1965, 4, 5),
('5', 1965, 7, 5),
('05', 1965, 7, 5),
('14', 1965, 7, 14),
('15', 1965, 7, 15),
('16', 1965, 6, 16),
('3-6', 1965, 3, 6),
('5.10', 1965, 5, 10),
('06-09', 1965, 6, 9),
('11.10', 1964, 11, 10),
('07-14', 1965, 7, 14),
('07/15', 1965, 7, 15),
('7.16', 1964, 7, 16),
('917/12/12', 917, 12, 12),
('2017-11-1', 2017, 11, 1),
])
def test_good_date_parsing(date_s, expect_year, expect_month, expect_day):
oxrlib.config.Configuration.TODAY = datetime.date(1965, 4, 3)
oxrlib.config.Configuration.TODAY = datetime.date(1965, 7, 15)
config = config_from(os.devnull, ['historical', date_s])
actual_date = config.args.date
assert actual_date.year == expect_year