Skip to content

Instantly share code, notes, and snippets.

@toaco
Created November 13, 2017 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toaco/9fabda1511d68144cdef9b95a840eb04 to your computer and use it in GitHub Desktop.
Save toaco/9fabda1511d68144cdef9b95a840eb04 to your computer and use it in GitHub Desktop.
calculate deadline of current period which unit is month
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment