Skip to content

Commit

Permalink
fixed the one week offset issue in weekly dates
Browse files Browse the repository at this point in the history
  • Loading branch information
nKataraia committed Jul 22, 2024
1 parent 4337604 commit 102b1dc
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions code/dihlibs/dhis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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(
Expand Down

0 comments on commit 102b1dc

Please sign in to comment.