From 716ff90a4e6e400357f97cf4211db07ddc9da8b6 Mon Sep 17 00:00:00 2001 From: Pablo Castellano Date: Sun, 16 Sep 2018 18:21:53 +0200 Subject: [PATCH] use relativedelta to calculate next month/year relativedelta handles correctly leap years --- icalevents/icalparser.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/icalevents/icalparser.py b/icalevents/icalparser.py index f30472e..8596b37 100644 --- a/icalevents/icalparser.py +++ b/icalevents/icalparser.py @@ -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))