diff --git a/__main__.py b/__main__.py index 53c2c43..a23f072 100644 --- a/__main__.py +++ b/__main__.py @@ -1,5 +1,14 @@ +from doomsday.algorithm import get_weekday_for_date +from doomsday.date import is_valid_date + + def main() -> None: - print("Hello world") + user_input = input("Please enter a date in the format YYYY-MM-dd: ") + + if is_valid_date(user_input): + print(f"The weekday for {user_input} is: {get_weekday_for_date(user_input)}") + else: + print("Error: The date is not valid.") main() diff --git a/doomsday/algorithm.py b/doomsday/algorithm.py index cf81d60..ee46e88 100644 --- a/doomsday/algorithm.py +++ b/doomsday/algorithm.py @@ -1,2 +1,52 @@ +DAYS: tuple[str, str, str, str, str, str, str] = ( + 'Sunday', + 'Monday', + 'Tuesday', + 'Wednesday', + 'Thursday', + 'Friday', + 'Saturday' +) + + def get_weekday_for_date(date: str) -> str: - return "Sunday" + """Returns the weekday for the given date in the format YYYY-MM-dd.""" + year, month, day = (int(i) for i in date.split('-')) + + anchor_day_index: int = get_anchor_of_the_year(year) + + doomsday_month: list[int] = [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] + + if is_leap_year(year): + doomsday_month[0] = 4 + doomsday_month[1] = 1 + + # Calculate the difference between the day and the doomsday of the month + difference: int = day - doomsday_month[month - 1] + # Calculate the weekday index by using the anchor day index and the difference + weekday_index: int = (anchor_day_index + difference) % 7 + + return DAYS[weekday_index] + + +def is_leap_year(year: int) -> bool: + """Checks if the given year is a leap year.""" + return year % 400 == 0 or (year % 100 != 0 and year % 4 == 0) + + +def get_anchor_of_the_year(year: int) -> int: + """Calculates the anchor day index for the given year.""" + value: int = year % 100 + + if value % 2 == 1: + value += 11 + value //= 2 + if value % 2 == 1: + value += 11 + + # multiple of 7 equals or greater than century year + difference_multiple_of_7: int = (7 - value) % 7 + + numbers_to_add_by_century_year: list[int] = [2, 0, 5, 3] + + return (difference_multiple_of_7 + numbers_to_add_by_century_year[(year // 100) % 4]) % 7 diff --git a/doomsday/date.py b/doomsday/date.py index 0f8e737..3c97f78 100644 --- a/doomsday/date.py +++ b/doomsday/date.py @@ -1,2 +1,25 @@ -def is_valid_date(date: str) -> bool: - return True +from datetime import datetime + + +def is_valid_date(date_str: str) -> bool: + """Checks if the given date is valid.""" + try: + # Try to convert the string to a date object + date_obj: datetime = datetime.strptime(date_str, '%Y-%m-%d') + + # Check if the year is greater than or equal to 1583 + if date_obj.year < 1583: + print("Error: The year must be greater than or equal to 1583.") + return False + + return True + + except ValueError: + # Conversion error, the date is not in the expected format + print("Error: The date does not follow the format YYYY-MM-dd.") + return False + + except Exception as e: + # Handle other potential errors + print(f"An error occurred: {e}") + return False