cliutil: Add diff_year function.

This commit is contained in:
Brett Smith 2020-07-27 11:50:11 -04:00
parent 4615364538
commit a27d68beab
4 changed files with 33 additions and 26 deletions

View file

@ -255,6 +255,17 @@ def add_version_argument(parser: argparse.ArgumentParser) -> argparse.Action:
def date_arg(arg: str) -> datetime.date:
return datetime.datetime.strptime(arg, '%Y-%m-%d').date()
def diff_year(date: datetime.date, diff: int) -> datetime.date:
new_year = date.year + diff
try:
return date.replace(year=new_year)
except ValueError:
# The original date is Feb 29, which doesn't exist in the new year.
if diff < 0:
return datetime.date(new_year, 2, 28)
else:
return datetime.date(new_year, 3, 1)
def year_or_date_arg(arg: str) -> Union[int, datetime.date]:
"""Get either a date or a year (int) from an argument string

View file

@ -339,17 +339,6 @@ metadata to match. A single ticket number is a shortcut for
args.report_type = ReportType.ODS
return args
def diff_year(date: datetime.date, diff: int) -> datetime.date:
new_year = date.year + diff
try:
return date.replace(year=new_year)
except ValueError:
# The original date is Feb 29, which doesn't exist in the new year.
if diff < 0:
return datetime.date(new_year, 2, 28)
else:
return datetime.date(new_year, 3, 1)
def main(arglist: Optional[Sequence[str]]=None,
stdout: TextIO=sys.stdout,
stderr: TextIO=sys.stderr,
@ -365,9 +354,9 @@ def main(arglist: Optional[Sequence[str]]=None,
if args.start_date is None:
args.stop_date = datetime.date.today()
else:
args.stop_date = diff_year(args.start_date, 1)
args.stop_date = cliutil.diff_year(args.start_date, 1)
if args.start_date is None:
args.start_date = diff_year(args.stop_date, -1)
args.start_date = cliutil.diff_year(args.stop_date, -1)
returncode = 0
books_loader = config.books_loader()

View file

@ -747,17 +747,6 @@ metadata to match. A single ticket number is a shortcut for
args.accounts = list(LedgerODS.ACCOUNT_COLUMNS)
return args
def diff_year(date: datetime.date, diff: int) -> datetime.date:
new_year = date.year + diff
try:
return date.replace(year=new_year)
except ValueError:
# The original date is Feb 29, which doesn't exist in the new year.
if diff < 0:
return datetime.date(new_year, 2, 28)
else:
return datetime.date(new_year, 3, 1)
def main(arglist: Optional[Sequence[str]]=None,
stdout: TextIO=sys.stdout,
stderr: TextIO=sys.stderr,
@ -771,11 +760,11 @@ def main(arglist: Optional[Sequence[str]]=None,
today = datetime.date.today()
if args.start_date is None:
args.start_date = diff_year(today, -1)
args.start_date = cliutil.diff_year(today, -1)
if args.stop_date is None:
args.stop_date = today + datetime.timedelta(days=30)
elif args.stop_date is None:
args.stop_date = diff_year(args.start_date, 1)
args.stop_date = cliutil.diff_year(args.start_date, 1)
returncode = 0
books_loader = config.books_loader()

View file

@ -218,3 +218,21 @@ def test_version_argument(argparser, capsys, arg):
stdout, _ = capsys.readouterr()
lines = iter(stdout.splitlines())
assert re.match(r'^test_cliutil version \d+\.\d+\.\d+', next(lines, "<EOF>"))
@pytest.mark.parametrize('date,diff,expected', [
(datetime.date(2010, 2, 28), 0, datetime.date(2010, 2, 28)),
(datetime.date(2010, 2, 28), 1, datetime.date(2011, 2, 28)),
(datetime.date(2010, 2, 28), 2, datetime.date(2012, 2, 28)),
(datetime.date(2010, 2, 28), -1, datetime.date(2009, 2, 28)),
(datetime.date(2010, 2, 28), -2, datetime.date(2008, 2, 28)),
(datetime.date(2012, 2, 29), 2, datetime.date(2014, 3, 1)),
(datetime.date(2012, 2, 29), 4, datetime.date(2016, 2, 29)),
(datetime.date(2012, 2, 29), -2, datetime.date(2010, 2, 28)),
(datetime.date(2012, 2, 29), -4, datetime.date(2008, 2, 29)),
(datetime.date(2010, 3, 1), 1, datetime.date(2011, 3, 1)),
(datetime.date(2010, 3, 1), 2, datetime.date(2012, 3, 1)),
(datetime.date(2010, 3, 1), -1, datetime.date(2009, 3, 1)),
(datetime.date(2010, 3, 1), -2, datetime.date(2008, 3, 1)),
])
def test_diff_year(date, diff, expected):
assert cliutil.diff_year(date, diff) == expected