From dc5c0b25ba10381d0423a7e99e4afa812363f631 Mon Sep 17 00:00:00 2001 From: Ollie <69084614+olijeffers0n@users.noreply.github.com> Date: Sat, 5 Aug 2023 23:50:45 +0100 Subject: [PATCH] Update time formats --- schedule/__init__.py | 2 +- schedule/api.py | 34 ++++++++++++++++++++-------------- schedule/structures.py | 24 ++++++++++++++++-------- 3 files changed, 37 insertions(+), 23 deletions(-) diff --git a/schedule/__init__.py b/schedule/__init__.py index c32bdb2..3fe1573 100644 --- a/schedule/__init__.py +++ b/schedule/__init__.py @@ -1,4 +1,4 @@ __name__ = "MySchedule.py" -__version__ = "1.0.5" +__version__ = "1.0.6" from .api import ScheduleAPI diff --git a/schedule/api.py b/schedule/api.py index ec318a9..9c865e6 100644 --- a/schedule/api.py +++ b/schedule/api.py @@ -215,17 +215,19 @@ def get_timecard(self, week_end_date: str = None) -> List[Clock]: :return: A timecard for the given week code """ data = { - "authToken": self.auth_token, - "reqType": "ESS", - "mm": "ESSTIMECARD", - "sm": "SUMMARY", - } + "authToken": self.auth_token, + "reqType": "ESS", + "mm": "ESSTIMECARD", + "sm": "SUMMARY", + } if week_end_date is not None: - data.update({ - "weekendDate": week_end_date, - "weekend": week_end_date, - }) + data.update( + { + "weekendDate": week_end_date, + "weekend": week_end_date, + } + ) # Make a request to the timecard page tc_request = self.session.post( @@ -273,7 +275,7 @@ def get_timecard(self, week_end_date: str = None) -> List[Clock]: if date_nodes and date_nodes[0] != " ": date = date_nodes[0] data_dict[date] = {"punches": []} - + # Check if time and punch is present if time_nodes and punch_type_nodes: # Extract the time and punch type from the CDATA content @@ -310,18 +312,22 @@ def get_timecard(self, week_end_date: str = None) -> List[Clock]: clock_in_time = datetime.strptime(punch["time"], "%H:%M") # Convert total seconds to hours and minutes - time_clocked_in_hours, remaining_seconds = divmod(time_clocked_in.seconds, 3600) + time_clocked_in_hours, remaining_seconds = divmod( + time_clocked_in.seconds, 3600 + ) time_clocked_in_minutes = remaining_seconds // 60 - time_clocked_out_hours, remaining_seconds = divmod(time_clocked_out.seconds, 3600) + time_clocked_out_hours, remaining_seconds = divmod( + time_clocked_out.seconds, 3600 + ) time_clocked_out_minutes = remaining_seconds // 60 clocks.append( Clock( date, punches, - f"{time_clocked_in_hours}:{time_clocked_in_minutes}", - f"{time_clocked_out_hours}:{time_clocked_out_minutes}" + f"{time_clocked_in_hours}:{time_clocked_in_minutes if time_clocked_in_minutes > 9 else f'0{time_clocked_in_minutes}'}", + f"{time_clocked_out_hours}:{time_clocked_out_minutes if time_clocked_out_minutes > 9 else f'0{time_clocked_out_minutes}'}", ) ) diff --git a/schedule/structures.py b/schedule/structures.py index 6a2a03c..46de060 100644 --- a/schedule/structures.py +++ b/schedule/structures.py @@ -46,12 +46,14 @@ def __repr__(self) -> str: + ")" ) + class PunchType(Enum): ShiftStart = "Shift Start" BreakStart = "Meal Start" BreakEnd = "Meal End" ShiftEnd = "Shift End" + class Punch: def __init__(self, punch_type: PunchType, time: str) -> None: self.type = punch_type @@ -61,18 +63,18 @@ def __repr__(self) -> str: return f"Punch({self.type}, {self.time})" def to_dict(self): - return { - "type": self.type.value, - "time": self.time - } + return {"type": self.type.value, "time": self.time} @classmethod def from_dict(cls, data): punch_type = PunchType(data["type"]) return cls(punch_type, data["time"]) + class Clock: - def __init__(self, date: str, punches: List[Punch], clocked_in_time: str, break_time: str) -> None: + def __init__( + self, date: str, punches: List[Punch], clocked_in_time: str, break_time: str + ) -> None: self.date = date self.punches = punches self.time_clocked_in = clocked_in_time @@ -82,17 +84,23 @@ def __repr__(self) -> str: data = "Clock(" for punch in self.punches: data += punch.__repr__() + ", " - return data + f"Time Clocked in: {self.time_clocked_in}, Time Clocked out: {self.time_clocked_out}" + ")" + return ( + data + + f"Time Clocked in: {self.time_clocked_in}, Time Clocked out: {self.time_clocked_out}" + + ")" + ) def to_dict(self): return { "date": self.date, "punches": [punch.to_dict() for punch in self.punches], "time_clocked_in": self.time_clocked_in, - "time_clocked_out": self.time_clocked_out + "time_clocked_out": self.time_clocked_out, } @classmethod def from_dict(cls, data): punches = [Punch.from_dict(punch_data) for punch_data in data["punches"]] - return cls(data["date"], punches, data["time_clocked_in"], data["time_clocked_out"]) + return cls( + data["date"], punches, data["time_clocked_in"], data["time_clocked_out"] + )