From 7a3ee2daeb60bfb67b6351a76b42a224d43a60a0 Mon Sep 17 00:00:00 2001 From: Timo Date: Wed, 15 Nov 2023 12:27:30 +0100 Subject: [PATCH 1/5] date input --- __main__.py | 10 +++++++++- doomsday/date.py | 26 ++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/__main__.py b/__main__.py index 53c2c43..f1cfa89 100644 --- a/__main__.py +++ b/__main__.py @@ -1,5 +1,13 @@ +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("The date is valid.") + else: + print("Error: The date is not valid.") main() diff --git a/doomsday/date.py b/doomsday/date.py index 0f8e737..c41e1b2 100644 --- a/doomsday/date.py +++ b/doomsday/date.py @@ -1,2 +1,24 @@ -def is_valid_date(date: str) -> bool: - return True +from datetime import datetime + + +def is_valid_date(date_str) -> bool: + try: + # Try to convert the string to a date object + date_obj = 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 From 479a5a054b7c7750f1b65d02bedef290ea482edb Mon Sep 17 00:00:00 2001 From: Timo Date: Wed, 15 Nov 2023 15:00:30 +0100 Subject: [PATCH 2/5] weekday for date function --- doomsday/algorithm.py | 57 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/doomsday/algorithm.py b/doomsday/algorithm.py index cf81d60..4dc0724 100644 --- a/doomsday/algorithm.py +++ b/doomsday/algorithm.py @@ -1,2 +1,57 @@ +days: list[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 = get_anchor_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 + + difference = day - doomsday_month[month - 1] + weekday_index = (anchor_day_index + difference) % 7 + + return days[weekday_index] + + +def is_leap_year(year): + """Checks if the given year is a leap year.""" + return year % 400 == 0 or (year % 100 != 0 and year % 4 == 0) + + +def get_anchor_year(year): + """Calculates the anchor day index for the given year.""" + century_year: int = year % 100 + + if century_year % 2 == 1: + century_year += 11 + century_year //= 2 + if century_year % 2 == 1: + century_year += 11 + + difference_multiple_of_7: int = century_year % 7 + # multiple of 7 equals or greater than century year + if difference_multiple_of_7 != 0: + difference_multiple_of_7 = 7 - difference_multiple_of_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 + + +print(get_weekday_for_date('2021-02-01')) \ No newline at end of file From 582a07ffcd692188335157938af87adcb295349a Mon Sep 17 00:00:00 2001 From: Timo Date: Wed, 15 Nov 2023 15:36:01 +0100 Subject: [PATCH 3/5] delete print --- doomsday/algorithm.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/doomsday/algorithm.py b/doomsday/algorithm.py index 4dc0724..75253ce 100644 --- a/doomsday/algorithm.py +++ b/doomsday/algorithm.py @@ -52,6 +52,3 @@ def get_anchor_year(year): 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 - - -print(get_weekday_for_date('2021-02-01')) \ No newline at end of file From eab3d0baf44659fdc9f39bfbb6e7a619c9d241df Mon Sep 17 00:00:00 2001 From: Timo Date: Wed, 15 Nov 2023 16:52:52 +0100 Subject: [PATCH 4/5] fix main fix conventions --- __main__.py | 3 ++- doomsday/algorithm.py | 34 ++++++++++++++++------------------ doomsday/date.py | 3 ++- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/__main__.py b/__main__.py index f1cfa89..a23f072 100644 --- a/__main__.py +++ b/__main__.py @@ -1,3 +1,4 @@ +from doomsday.algorithm import get_weekday_for_date from doomsday.date import is_valid_date @@ -5,7 +6,7 @@ def main() -> None: user_input = input("Please enter a date in the format YYYY-MM-dd: ") if is_valid_date(user_input): - print("The date is valid.") + print(f"The weekday for {user_input} is: {get_weekday_for_date(user_input)}") else: print("Error: The date is not valid.") diff --git a/doomsday/algorithm.py b/doomsday/algorithm.py index 75253ce..008a5fd 100644 --- a/doomsday/algorithm.py +++ b/doomsday/algorithm.py @@ -1,4 +1,4 @@ -days: list[str] = [ +DAYS: tuple[str, str, str, str, str, str, str] = ( 'Sunday', 'Monday', 'Tuesday', @@ -6,16 +6,14 @@ 'Thursday', 'Friday', 'Saturday' -] +) def get_weekday_for_date(date: str) -> str: - """ - Returns the weekday for the given date in the format YYYY-MM-dd. - """ + """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 = get_anchor_year(year) + anchor_day_index = get_anchor_of_the_year(year) doomsday_month: list[int] = [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] @@ -23,31 +21,31 @@ def get_weekday_for_date(date: str) -> str: doomsday_month[0] = 4 doomsday_month[1] = 1 + # Calculate the difference between the day and the doomsday of the month difference = day - doomsday_month[month - 1] + # Calculate the weekday index by using the anchor day index and the difference weekday_index = (anchor_day_index + difference) % 7 - return days[weekday_index] + return DAYS[weekday_index] -def is_leap_year(year): +def is_leap_year(year: int): """Checks if the given year is a leap year.""" return year % 400 == 0 or (year % 100 != 0 and year % 4 == 0) -def get_anchor_year(year): +def get_anchor_of_the_year(year: int): """Calculates the anchor day index for the given year.""" - century_year: int = year % 100 + value: int = year % 100 - if century_year % 2 == 1: - century_year += 11 - century_year //= 2 - if century_year % 2 == 1: - century_year += 11 + if value % 2 == 1: + value += 11 + value //= 2 + if value % 2 == 1: + value += 11 - difference_multiple_of_7: int = century_year % 7 # multiple of 7 equals or greater than century year - if difference_multiple_of_7 != 0: - difference_multiple_of_7 = 7 - difference_multiple_of_7 + difference_multiple_of_7: int = (7 - value) % 7 numbers_to_add_by_century_year: list[int] = [2, 0, 5, 3] diff --git a/doomsday/date.py b/doomsday/date.py index c41e1b2..4241cf9 100644 --- a/doomsday/date.py +++ b/doomsday/date.py @@ -1,7 +1,8 @@ from datetime import datetime -def is_valid_date(date_str) -> bool: +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.strptime(date_str, '%Y-%m-%d') From 16be4e3efb6a8777fc51035a0ed4c3402dfb6b70 Mon Sep 17 00:00:00 2001 From: Timo Date: Wed, 15 Nov 2023 16:56:11 +0100 Subject: [PATCH 5/5] fix type --- doomsday/algorithm.py | 10 +++++----- doomsday/date.py | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/doomsday/algorithm.py b/doomsday/algorithm.py index 008a5fd..ee46e88 100644 --- a/doomsday/algorithm.py +++ b/doomsday/algorithm.py @@ -13,7 +13,7 @@ def get_weekday_for_date(date: str) -> str: """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 = get_anchor_of_the_year(year) + 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] @@ -22,19 +22,19 @@ def get_weekday_for_date(date: str) -> str: doomsday_month[1] = 1 # Calculate the difference between the day and the doomsday of the month - difference = day - doomsday_month[month - 1] + difference: int = day - doomsday_month[month - 1] # Calculate the weekday index by using the anchor day index and the difference - weekday_index = (anchor_day_index + difference) % 7 + weekday_index: int = (anchor_day_index + difference) % 7 return DAYS[weekday_index] -def is_leap_year(year: int): +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): +def get_anchor_of_the_year(year: int) -> int: """Calculates the anchor day index for the given year.""" value: int = year % 100 diff --git a/doomsday/date.py b/doomsday/date.py index 4241cf9..3c97f78 100644 --- a/doomsday/date.py +++ b/doomsday/date.py @@ -5,7 +5,7 @@ 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.strptime(date_str, '%Y-%m-%d') + 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: