From baab7890a13007a2baf2c5f4c6809387c65e7510 Mon Sep 17 00:00:00 2001
From: TheJoeCoder <joe@radialbog9.uk>
Date: Sat, 6 Apr 2024 12:39:40 +0100
Subject: [PATCH] Add time editing functionality

---
 .gitignore |  1 +
 main.py    | 17 +++++++++++++++++
 2 files changed, 18 insertions(+)

diff --git a/.gitignore b/.gitignore
index 1b85138..35b4c8c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -420,5 +420,6 @@ $RECYCLE.BIN/
 
 config.json
 patches.json
+patches.json.old
 input.json
 output.ics
\ No newline at end of file
diff --git a/main.py b/main.py
index 3d3f9bf..8166d5f 100644
--- a/main.py
+++ b/main.py
@@ -98,6 +98,23 @@ def apply_patch(input_lessons: list, patch: dict, week_lookup_table: list) -> li
                 # Edit the lesson
                 for key, value in patch["edit"].items():
                     lesson[key] = value
+    elif patch["type"] == "edit_times":
+        # Edit all lesson times that match the condition
+        for lesson in input_lessons:
+            if eval_lesson_condition(lesson, patch["condition"]):
+                # Get startDate and endDate
+                start_date = datetime.datetime.strptime(lesson["startDate"], "%Y-%m-%dT%H:%M:%S")
+                end_date = datetime.datetime.strptime(lesson["endDate"], "%Y-%m-%dT%H:%M:%S")
+                # Get start time and end time, replace in date
+                if "start" in patch["edit"]:
+                    start_time = datetime.time.fromisoformat(patch["edit"]["start"])
+                    start_date = start_date.replace(hour=start_time.hour, minute=start_time.minute, second=start_time.second)
+                if "end" in patch["edit"]:
+                    end_time = datetime.time.fromisoformat(patch["edit"]["end"])
+                    end_date = end_date.replace(hour=end_time.hour, minute=end_time.minute, second=end_time.second)
+                # Set new dates
+                lesson["startDate"] = start_date.strftime("%Y-%m-%dT%H:%M:%S")
+                lesson["endDate"] = end_date.strftime("%Y-%m-%dT%H:%M:%S")
     elif patch["type"] == "add":
         # Add a lesson
         # Build day map
-- 
GitLab