~mrp/time_computer

568326afec3c67b70fd5b56cc281dec3495189ab — Mark Penner 1 year, 30 days ago
```use datetime module to calculate times; refactor
```
```2 files changed, 31 insertions(+), 42 deletions(-)

M tc.py
M test_tc.py
```
`M tc.py => tc.py +11 -13`
```@@ 7,8 7,8 @@
#  mrpenner

import sys
-import datetime
-from decimal import *
+import datetime as dt
+from decimal import Decimal, ROUND_HALF_UP

settings = {
"standard_week": Decimal(40),

@@ 23,27 23,25 @@ def parse_line(line):
return the date, job name, hours, and miles in a dictionary
"""
items = line.split()
-    year = int(items[0][0:4])
-    month = int(items[0][4:6])
-    day = int(items[0][6:8])
+    day = dt.date.fromisoformat(items[0])
name = items[1]
del items[0:2]

times = []
miles = 0
-    for i, hhmm in enumerate(items):
+    for i, item in enumerate(items):
# miles can be put at the end of a line using syntax "mi 59.5" for 59.5 miles
-        if hhmm == "mi":
+        if item == "mi":
miles = float(items[i + 1])
break
-        hh_mm = hhmm.split(":")
-        time = int(hh_mm[0]) + (int(hh_mm[1]) / 60)
-        times.append(time)
-    hours = 0
+        clock = dt.datetime.combine(day, dt.time.fromisoformat(item))
+        times.append(clock)
+    accumulated_time = dt.timedelta()
for i in range(1, len(times), 2):
-        hours += times[i] - times[i - 1]
+        accumulated_time += times[i] - times[i - 1]
+    hours = accumulated_time / dt.timedelta(hours=1)
return {
-        "date": datetime.date(year, month, day),
+        "date": day,
"job": name,
"hours": hours,
"miles": miles,

```
`M test_tc.py => test_tc.py +20 -29`
```@@ 8,7 8,7 @@ mrpenner
"""

import datetime
-from decimal import *
+from decimal import Decimal

tc = __import__("tc")

@@ 32,27 32,21 @@ def test_parse_line_with_mileage():

def test_daily_hours():
-    assert (
-        tc.daily_hours(
-            [
-                {"date": datetime.date(2021, 3, 31), "job": "jobname", "hours": 9.38},
-                {"date": datetime.date(2021, 3, 31), "job": "jobname", "hours": 7},
-            ]
-        )
-        == {datetime.date(2021, 3, 31): 16.380000000000003}
-    )
+    assert tc.daily_hours(
+        [
+            {"date": datetime.date(2021, 3, 31), "job": "jobname", "hours": 9.38},
+            {"date": datetime.date(2021, 3, 31), "job": "jobname", "hours": 7},
+        ]
+    ) == {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}
-    )
+    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_reg_overtime_hours():

@@ 63,16 57,13 @@ def test_reg_overtime_hours():

def test_job_hours():
-    assert (
-        tc.job_hours(
-            [
-                {"date": datetime.date(2021, 3, 31), "job": "jobname", "hours": 9.38},
-                {"date": datetime.date(2021, 3, 31), "job": "jobname", "hours": 7},
-                {"date": datetime.date(2021, 3, 31), "job": "jobname2", "hours": 9.38},
-            ]
-        )
-        == {"jobname": 16.380000000000003, "jobname2": 9.38}
-    )
+    assert tc.job_hours(
+        [
+            {"date": datetime.date(2021, 3, 31), "job": "jobname", "hours": 9.38},
+            {"date": datetime.date(2021, 3, 31), "job": "jobname", "hours": 7},
+            {"date": datetime.date(2021, 3, 31), "job": "jobname2", "hours": 9.38},
+        ]
+    ) == {"jobname": 16.380000000000003, "jobname2": 9.38}

def test_total_hours():

```