Skip to content

Commit

Permalink
Fix get_week
Browse files Browse the repository at this point in the history
Base on 'today' and adapt for leap-year situation
  • Loading branch information
vingerha committed Feb 3, 2024
1 parent 4fd6afa commit 55b4e6e
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/models/stat.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,15 +604,20 @@ def get_week(self, year, month=None, measure_type=None):
now_date = datetime.now(timezone.utc)
if month is None:
month = int(datetime.now().strftime("%m"))
today = date.today()
start = today - timedelta(days=today.weekday())

#adapt for day in leap-year
if datetime.now.strftime("%m%d") == "0229" and int(year) != int(datetime.now().strftime("%Y")):
today = date.today() + timedelta(days=1)
else:
today = date.today()
start = today.replace(year=year, month=month) - timedelta(days=today.replace(year=year, month=month).weekday())
end = start + timedelta(days=6)
begin = datetime.combine(
now_date.replace(year=year, month=month, day=int(start.strftime("%d"))),
start,
datetime.min.time(),
)
end = datetime.combine(
now_date.replace(year=year, month=month, day=int(start.strftime("%d"))),
end,
datetime.max.time(),
)
value = 0
Expand Down Expand Up @@ -779,6 +784,15 @@ def generate_price(self):
)
return json.dumps(result)

def get_daily(self, date, mesure_type):
begin = datetime.combine(date, datetime.min.time())
end = datetime.combine(date, datetime.max.time())
value = 0
for item in self.db.get_detail_range(self.usage_point_id, begin, end):
if self.get_mesure_type(item.date).upper() == mesure_type.upper():
value += item.value / (60 / item.interval)
return value

def delete(self):
self.db.del_stat(self.usage_point_id)

Expand Down

0 comments on commit 55b4e6e

Please sign in to comment.