From 0885bf300aa4b2a13baac0a6d4e12916a68a7247 Mon Sep 17 00:00:00 2001 From: Maximilian Zettler Date: Thu, 10 Mar 2022 10:29:13 +0100 Subject: [PATCH] enhance get_hours_present --- timebot/constants.py | 10 +++++----- timebot/timebot.py | 25 ++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/timebot/constants.py b/timebot/constants.py index 4ff9570..9475c42 100644 --- a/timebot/constants.py +++ b/timebot/constants.py @@ -15,13 +15,13 @@ class DateFormats(Enum): SIMPLE_DATE = "%Y-%m-%d" SIMPLE_TIME = "%H:%M" - """ - Return escaped version of value if value is string. - - "%" will be escaped -> "%%" - """ @DynamicClassAttribute def evalue(self): + """ + Return escaped version of value if value is string. + + "%" will be escaped -> "%%" + """ if isinstance(self._value_, (str,)): return self._value_.replace("%", "%%") return self._value_ diff --git a/timebot/timebot.py b/timebot/timebot.py index 5f0c260..a40f14d 100644 --- a/timebot/timebot.py +++ b/timebot/timebot.py @@ -363,5 +363,28 @@ class TimeBot: start_date=now.replace(hour=0, minute=0, second=0, microsecond=0), end_date=now.replace(hour=0, minute=0, second=0, microsecond=0), ) - time_delta = now - datetime.datetime.strptime(punches[-1]["dateTime"], DateFormats.FULL_DATETIME.value) + punch_out_time = now + if punches and punches[0]["entryNumber"] == PunchCodes.LEAVING.value: + punch_out_time = datetime.datetime.strptime(punches.pop(0)["dateTime"], DateFormats.FULL_DATETIME.value) + + punch_in_time = None + if punches and punches[-1]["entryNumber"] == PunchCodes.COMING.value: + punch_in_time = datetime.datetime.strptime(punches.pop(-1)["dateTime"], DateFormats.FULL_DATETIME.value) + + if punch_in_time is None: + return datetime.timedelta(seconds=0) + + breaks = [] + end = now + for i in punches: + if i["entryNumber"] == PunchCodes.BREAK_END.value: + end = datetime.datetime.strptime(i["dateTime"], DateFormats.FULL_DATETIME.value) + if i["entryNumber"] == PunchCodes.BREAK_START.value: + start = datetime.datetime.strptime(i["dateTime"], DateFormats.FULL_DATETIME.value) + breaks.append(end - start) + end = now + + time_delta = punch_out_time - punch_in_time + for i in breaks: + time_delta = time_delta - i return time_delta