diff --git a/oxrlib/config.py b/oxrlib/config.py index 4c4877d..93eac18 100644 --- a/oxrlib/config.py +++ b/oxrlib/config.py @@ -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() diff --git a/setup.py b/setup.py index ab55238..c287242 100755 --- a/setup.py +++ b/setup.py @@ -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+', diff --git a/tests/test_Configuration.py b/tests/test_Configuration.py index 96f9d05..4c85edb 100644 --- a/tests/test_Configuration.py +++ b/tests/test_Configuration.py @@ -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