From 102b1dc0ca411d80ded4f5ea77cf430d48e9bdb9 Mon Sep 17 00:00:00 2001 From: nitusima Date: Mon, 22 Jul 2024 11:07:00 +0300 Subject: [PATCH] fixed the one week offset issue in weekly dates --- code/dihlibs/dhis/__init__.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/code/dihlibs/dhis/__init__.py b/code/dihlibs/dhis/__init__.py index 879adff..9f99025 100644 --- a/code/dihlibs/dhis/__init__.py +++ b/code/dihlibs/dhis/__init__.py @@ -213,11 +213,15 @@ def get_period(self, when, period_type="monthly"): } ).get(period_type.lower()) - def get_week_date(self,date): + def get_week_date(self,date): parts = date.split("W") - week_start = 7 * int(parts[1]) - year_start = datetime.strptime(parts[0], "%Y") - return year_start + relativedelta(days=abs(week_start)) + year = int(parts[0]) + week = int(parts[1]) + year_start = datetime(year, 1, 1) + week_start = year_start + relativedelta(weeks=week-1) + while week_start.weekday() != 0: # 0 means Monday + week_start += relativedelta(days=1) + return week_start def period_to_db_date(self, date: str): formats = ["%Y-%m-%d", "%YW%W", "%Y%m", "%Y"] @@ -236,6 +240,7 @@ def set_period_cols(r): col_name = ("issued_" if "referral" in r.db_view else f"reported_") + pt period = self.get_period(date, r.period_type) + print("period is",period,' and date is ',date) db_val = self.period_to_db_date(period) return col_name, db_val, period e_map = e_map.reset_index().merge(