Skip to content

Instantly share code, notes, and snippets.

@krzysztofantczak
Last active February 15, 2024 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krzysztofantczak/b4400de80639a4ad51ab9a42382152e2 to your computer and use it in GitHub Desktop.
Save krzysztofantczak/b4400de80639a4ad51ab9a42382152e2 to your computer and use it in GitHub Desktop.
from datetime import datetime, timedelta
def decode_time_string(time_string):
# Split the string into its components
components = time_string.split('-')
# Check if the string has the correct number of components
if len(components) != 4:
raise ValueError("Input string does not have the correct format")
week_number, day_name, hour, minute = components
# Get the current date
current_date = datetime.now().date()
# Find the first occurrence of the given day name in the current month
current_month = current_date.month
current_year = current_date.year
first_day_of_month = datetime(current_year, current_month, 1)
while first_day_of_month.strftime('%A') != day_name:
first_day_of_month += timedelta(days=1)
# Calculate the actual date
week_offset = (int(week_number[1:]) - 1) * 7
target_date = first_day_of_month + timedelta(days=week_offset)
# Parse the hour and minute from the string
hour = int(hour)
minute = int(minute)
# Construct the final datetime object
target_time = datetime(target_date.year, target_date.month, target_date.day, hour, minute)
return target_time.strftime("%d-%m-%Y %H:%M")
# Test the function
time_string = "W3-Wednesday-08-00"
try:
decoded_time = decode_time_string(time_string)
print("Decoded time:", decoded_time)
except ValueError as e:
print("Error:", e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment