Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import arrow
- def diff_month(d1, d2):
- return (d1.year - d2.year) * 12 + d1.month - d2.month
- def calc_deadline(start_date, period):
- date = arrow.get(start_date)
- now = arrow.get()
- remainder = diff_month(now, date) % period
- deadline = now.shift(months=period - 1 - remainder).datetime
- return deadline
- if __name__ == '__main__':
- import datetime
- def return_date(func):
- def wrapper(*args):
- return func(*args).date()
- return wrapper
- calc_deadline = return_date(calc_deadline)
- # now is 2017-11-10
- assert calc_deadline(
- datetime.date(2017, 01, 01), 2) == datetime.date(2017, 12, 10)
- assert calc_deadline(
- datetime.date(2017, 01, 01), 1) == datetime.date(2017, 11, 10)
- assert calc_deadline(
- datetime.date(2017, 01, 01), 3) == datetime.date(2017, 12, 10)
- def calc_deadline():
- now = arrow.get('2017-01-31')
- print now.shift(months=1).shift(months=1)
- calc_deadline()
Add Comment
Please, Sign In to add comment