aboutsummaryrefslogtreecommitdiff
path: root/challenge-178/paulo-custodio/python/ch-2.py
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2024-10-22 18:11:56 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2024-10-22 18:11:56 +0100
commit45c413ded143a3d7548e837e2cc459f4fabcda41 (patch)
treeb82f0097d81f98326854e90cddbfaa9ff9613c15 /challenge-178/paulo-custodio/python/ch-2.py
parentc3edc9149c2c1d435eecf347139ca83094e1d86e (diff)
downloadperlweeklychallenge-club-45c413ded143a3d7548e837e2cc459f4fabcda41.tar.gz
perlweeklychallenge-club-45c413ded143a3d7548e837e2cc459f4fabcda41.tar.bz2
perlweeklychallenge-club-45c413ded143a3d7548e837e2cc459f4fabcda41.zip
Add Python solutions
Diffstat (limited to 'challenge-178/paulo-custodio/python/ch-2.py')
-rw-r--r--challenge-178/paulo-custodio/python/ch-2.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/challenge-178/paulo-custodio/python/ch-2.py b/challenge-178/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..e05c344ec8
--- /dev/null
+++ b/challenge-178/paulo-custodio/python/ch-2.py
@@ -0,0 +1,46 @@
+#!/usr/bin/env python3
+
+# Challenge 178
+#
+# Task 2: Business Date
+# Submitted by: Mohammad S Anwar
+#
+# You are given $timestamp (date with time) and $duration in hours.
+#
+# Write a script to find the time that occurs $duration business hours after
+# $timestamp. For the sake of this task, let us assume the working hours is 9am
+# to 6pm, Monday to Friday. Please ignore timezone too.
+#
+# For example,
+#
+# Suppose the given timestamp is 2022-08-01 10:30 and the duration is 4 hours.
+# Then the next business date would be 2022-08-01 14:30.
+#
+# Similar if the given timestamp is 2022-08-01 17:00 and the duration is 3.5 hours.
+# Then the next business date would be 2022-08-02 11:30.
+
+from datetime import datetime, timedelta
+import sys
+
+def next_business_date(date_text, time_text, hours):
+ date_str = f"{date_text} {time_text}"
+ date = datetime.strptime(date_str, "%Y-%m-%d %H:%M")
+
+ minutes = hours * 60
+ while True:
+ end_day = date.replace(hour=18, minute=0)
+ remain = (end_day - date).total_seconds() / 60
+ if minutes < remain:
+ date += timedelta(minutes=minutes)
+ return date
+ else:
+ minutes -= remain
+ date = end_day + timedelta(hours=(24 - 18) + 9)
+ while date.weekday() >= 5: # skip weekend
+ date += timedelta(days=1)
+
+if len(sys.argv) != 4:
+ raise ValueError("usage: ch-2.py yyyy-mm-dd HH:MM hours")
+
+result_date = next_business_date(sys.argv[1], sys.argv[2], float(sys.argv[3]))
+print(result_date.strftime("%Y-%m-%d %H:%M"))