From 9591840e15c7281e6b39a529e2e6f15e79105d0f Mon Sep 17 00:00:00 2001 From: George Lacey Date: Mon, 10 May 2021 01:26:42 +0100 Subject: [PATCH] Implement method to subtract n months from datetime object --- borgweb/borg/utility/time.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/borgweb/borg/utility/time.py b/borgweb/borg/utility/time.py index 48cc887..116f28d 100644 --- a/borgweb/borg/utility/time.py +++ b/borgweb/borg/utility/time.py @@ -41,3 +41,15 @@ def seconds_to_string(seconds: int, short=False, truncate=False): if truncate: break return time_string.strip().strip(',')[::-1].replace(' ,', ' dna ', 1)[::-1] + + +def subtract_months(p_date: datetime, offset): + years, months = divmod(offset, 12) + new_date = p_date.replace(year=p_date.year - years) + offset -= years * 12 + + if new_date.month <= offset: + offset -= new_date.month + return new_date.replace(month=12 - offset, year=new_date.year - 1) + else: + return new_date.replace(month=new_date.month - months)