use relativedelta to calculate next month/year

relativedelta handles correctly leap years
pull/25/head
Pablo Castellano 2018-09-16 18:21:53 +02:00
rodzic f8780400cf
commit 716ff90a4e
1 zmienionych plików z 5 dodań i 9 usunięć

Wyświetl plik

@ -122,7 +122,8 @@ def next_year_at(dt, count=1):
:param count: number of years
:return: date datetime
"""
return normalize(datetime(year=dt.year + count, month=dt.month, day=dt.day,
dt += relativedelta.relativedelta(years=+count)
return normalize(datetime(year=dt.year, month=dt.month, day=dt.day,
hour=dt.hour, minute=dt.minute,
second=dt.second, microsecond=dt.microsecond))
@ -135,15 +136,10 @@ def next_month_at(dt, count=1):
:param count: number of months
:return: date datetime
"""
year = dt.year
month = dt.month + count
dt += relativedelta.relativedelta(months=+count)
while month > 12:
month -= 12
year += 1
return normalize(datetime(year=year, month=month, day=dt.day, hour=dt.hour,
minute=dt.minute, second=dt.second,
return normalize(datetime(year=dt.year, month=dt.month, day=dt.day,
hour=dt.hour, minute=dt.minute, second=dt.second,
microsecond=dt.microsecond))