SHARE
TWEET

Untitled

a guest Nov 14th, 2017 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import arrow
  2.  
  3.  
  4. def diff_month(d1, d2):
  5.     return (d1.year - d2.year) * 12 + d1.month - d2.month
  6.  
  7.  
  8. def calc_deadline(start_date, period):
  9.     date = arrow.get(start_date)
  10.     now = arrow.get()
  11.     remainder = diff_month(now, date) % period
  12.     deadline = now.shift(months=period - 1 - remainder).datetime
  13.     return deadline
  14.  
  15.  
  16. if __name__ == '__main__':
  17.     import datetime
  18.  
  19.  
  20.     def return_date(func):
  21.         def wrapper(*args):
  22.             return func(*args).date()
  23.  
  24.         return wrapper
  25.  
  26.  
  27.     calc_deadline = return_date(calc_deadline)
  28.  
  29.     # now is 2017-11-10
  30.     assert calc_deadline(
  31.         datetime.date(2017, 01, 01), 2) == datetime.date(2017, 12, 10)
  32.     assert calc_deadline(
  33.         datetime.date(2017, 01, 01), 1) == datetime.date(2017, 11, 10)
  34.     assert calc_deadline(
  35.         datetime.date(2017, 01, 01), 3) == datetime.date(2017, 12, 10)
  36.  
  37.  
  38.     def calc_deadline():
  39.         now = arrow.get('2017-01-31')
  40.         print now.shift(months=1).shift(months=1)
  41.  
  42.  
  43.     calc_deadline()
RAW Paste Data
Top