@@ 46,6 46,21 @@ def daily_hours(data):
return output
+def weekly_hours(data):
+ """
+ calculate how many hours worked each week
+ return a dictionary with ISO week numbers as keys and hours as values
+ """
+ output = {}
+ for day in data:
+ week = day["date"].isocalendar()[1]
+ if week in output:
+ output[week] += day["hours"]
+ else:
+ output[week] = day["hours"]
+ return output
+
+
def total_hours(data):
""" figures total hours worked and returns total hours as a float """
total = 0.0
@@ 29,6 29,15 @@ def test_daily_hours():
) == {datetime.date(2021, 3, 31): 16.380000000000003}
+def test_weekly_hours():
+ assert tc.weekly_hours(
+ [
+ {"date": datetime.date(2021, 3, 31), "job": "jobname", "hours": 9.38},
+ {"date": datetime.date(2021, 3, 31), "job": "jobname", "hours": 7},
+ ]
+ ) == {13: 16.380000000000003}
+
+
def test_total_hours():
assert (
tc.total_hours(