From bc191dd718b160f981a5e0097230b508b879028e Mon Sep 17 00:00:00 2001 From: Nathan Deroche Date: Wed, 15 Nov 2023 15:12:30 +0100 Subject: [PATCH 1/5] start doomsday project python --- .idea/.gitignore | 8 +++ .../inspectionProfiles/profiles_settings.xml | 6 +++ .idea/misc.xml | 7 +++ .idea/modules.xml | 8 +++ .idea/tp-python-doomsday-rule.iml | 8 +++ .idea/vcs.xml | 6 +++ __main__.py | 20 ++++++-- doomsday/algorithm.py | 49 ++++++++++++++++++- doomsday/date.py | 22 ++++++++- 9 files changed, 128 insertions(+), 6 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/tp-python-doomsday-rule.iml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..1c2fda5 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..838dac8 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..dde95a7 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/tp-python-doomsday-rule.iml b/.idea/tp-python-doomsday-rule.iml new file mode 100644 index 0000000..d9e6024 --- /dev/null +++ b/.idea/tp-python-doomsday-rule.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..c8397c9 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/__main__.py b/__main__.py index 53c2c43..94f1eb5 100644 --- a/__main__.py +++ b/__main__.py @@ -1,5 +1,19 @@ -def main() -> None: - print("Hello world") +from doomsday.date import is_valid_date -main() +def main(): + while True: + user_input = input("Please enter a date using the format YYYY-MM-dd : ") + + # Check date input + if not is_valid_date(user_input): + print("Incorrect date format. Please use this format : YYYY-MM-dd.") + continue + + # Si toutes les vérifications passent, la date est valide + print("Date valide : ", user_input) + break + + +if __name__ == "__main__": + main() diff --git a/doomsday/algorithm.py b/doomsday/algorithm.py index cf81d60..488ab04 100644 --- a/doomsday/algorithm.py +++ b/doomsday/algorithm.py @@ -1,2 +1,47 @@ -def get_weekday_for_date(date: str) -> str: - return "Sunday" +def get_weekday_for_date(date_str): + WEEKDAYS = ( + 'Sunday', + 'Monday', + 'Tuesday', + 'Wednesday', + 'Thursday', + 'Friday', + 'Saturday' + ) + + from datetime import datetime + try: + date_obj = datetime.strptime(date_str, '%Y-%m-%d') + + lastTwoNumbersCentury = date_obj.year % 100 + + if lastTwoNumbersCentury % 2 != 0: + lastTwoNumbersCentury += 11 + + lastTwoNumbersCentury //= 2 + + if lastTwoNumbersCentury % 2 != 0: + lastTwoNumbersCentury += 11 + + i = 0 + while i < 8: + if (lastTwoNumbersCentury + i) % 7 == 0: + closest_multiple_of_seven = lastTwoNumbersCentury + i + break + + numbers_by_century = [2, 0, 5, 3] + + number_to_add_century = numbers_by_century[(date_obj.year // 100) % 4] + + anchor_day = WEEKDAYS[closest_multiple_of_seven + number_to_add_century] + + print(anchor_day) + + + except ValueError: + # If the conversion fails, print an error message + print("Incorrect date format. Please use the format YYYY-MM-dd.") + return None + + +get_weekday_for_date('2021-10-10') diff --git a/doomsday/date.py b/doomsday/date.py index 0f8e737..fbf02b3 100644 --- a/doomsday/date.py +++ b/doomsday/date.py @@ -1,2 +1,22 @@ -def is_valid_date(date: str) -> bool: +def is_valid_date(date_str): + from datetime import datetime + + try: + # Attempt to convert the input string to a datetime 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("The year must be greater than or equal to 1583.") + return False + + # Additional checks for the existence of the date can be added here if needed + + return True + + except ValueError: + # If the conversion fails, print an error message + print("Incorrect date format. Please use the format YYYY-MM-dd.") + return False + return True From 5f6b02a004283a045bb0391365fb0c9f4d099cb9 Mon Sep 17 00:00:00 2001 From: Nathan Deroche Date: Wed, 15 Nov 2023 15:14:25 +0100 Subject: [PATCH 2/5] fix --- __main__.py | 1 - doomsday/date.py | 4 ---- 2 files changed, 5 deletions(-) diff --git a/__main__.py b/__main__.py index 94f1eb5..e6aac05 100644 --- a/__main__.py +++ b/__main__.py @@ -10,7 +10,6 @@ def main(): print("Incorrect date format. Please use this format : YYYY-MM-dd.") continue - # Si toutes les vérifications passent, la date est valide print("Date valide : ", user_input) break diff --git a/doomsday/date.py b/doomsday/date.py index fbf02b3..f04d633 100644 --- a/doomsday/date.py +++ b/doomsday/date.py @@ -2,7 +2,6 @@ def is_valid_date(date_str): from datetime import datetime try: - # Attempt to convert the input string to a datetime object date_obj = datetime.strptime(date_str, '%Y-%m-%d') # Check if the year is greater than or equal to 1583 @@ -10,12 +9,9 @@ def is_valid_date(date_str): print("The year must be greater than or equal to 1583.") return False - # Additional checks for the existence of the date can be added here if needed - return True except ValueError: - # If the conversion fails, print an error message print("Incorrect date format. Please use the format YYYY-MM-dd.") return False From 9b3191d5da30a69cf8777b2e6a6c8aef4dc832b6 Mon Sep 17 00:00:00 2001 From: Nathan Deroche Date: Fri, 17 Nov 2023 11:18:13 +0100 Subject: [PATCH 3/5] date input --- __main__.py | 9 +++-- doomsday/algorithm.py | 86 +++++++++++++++++++++++++------------------ doomsday/date.py | 16 ++++++-- 3 files changed, 70 insertions(+), 41 deletions(-) diff --git a/__main__.py b/__main__.py index e6aac05..3ec4f9a 100644 --- a/__main__.py +++ b/__main__.py @@ -1,18 +1,21 @@ +from doomsday.algorithm import get_weekday_for_date from doomsday.date import is_valid_date def main(): while True: - user_input = input("Please enter a date using the format YYYY-MM-dd : ") + user_input = input( + "Please enter a date using the format YYYY-MM-dd : ") # Check date input if not is_valid_date(user_input): - print("Incorrect date format. Please use this format : YYYY-MM-dd.") continue - print("Date valide : ", user_input) break + print( + f"{user_input} was a {get_weekday_for_date(user_input)}") + if __name__ == "__main__": main() diff --git a/doomsday/algorithm.py b/doomsday/algorithm.py index 488ab04..ff108f4 100644 --- a/doomsday/algorithm.py +++ b/doomsday/algorithm.py @@ -1,47 +1,63 @@ -def get_weekday_for_date(date_str): - WEEKDAYS = ( - 'Sunday', - 'Monday', - 'Tuesday', - 'Wednesday', - 'Thursday', - 'Friday', - 'Saturday' - ) - +WEEKDAYS = ( + 'Sunday', + 'Monday', + 'Tuesday', + 'Wednesday', + 'Thursday', + 'Friday', + 'Saturday' +) +# List of memorable dates that always land on doomsday for each month +# (except January and February) +doomsday_month: list[int] = [3, 28, 14, 4, 9, 6, 11, 8, 5, 10, 7, 12] + + +def get_weekday_for_date(date_str: str) -> str: + """ + Returns the day of the week for the given date in the format YYYY-MM-dd. + """ from datetime import datetime - try: - date_obj = datetime.strptime(date_str, '%Y-%m-%d') - - lastTwoNumbersCentury = date_obj.year % 100 - - if lastTwoNumbersCentury % 2 != 0: - lastTwoNumbersCentury += 11 - lastTwoNumbersCentury //= 2 + # Convert date to datetime + date = datetime.strptime(date_str, '%Y-%m-%d') + # Get the doomsday of the year + doomsday_of_year = get_doomsday_year(date.year) - if lastTwoNumbersCentury % 2 != 0: - lastTwoNumbersCentury += 11 + # Check if the year is a leap year + # If it is, change the doomsday for January and February + if date.year % 400 == 0 or (date.year % 100 != 0 and date.year % 4 == 0): + doomsday_month[0] = 4 + doomsday_month[1] = 29 - i = 0 - while i < 8: - if (lastTwoNumbersCentury + i) % 7 == 0: - closest_multiple_of_seven = lastTwoNumbersCentury + i - break + # Get the difference between the doomsday of the month and the given date + difference_doomsday_and_date = date.day - doomsday_month[date.month - 1] - numbers_by_century = [2, 0, 5, 3] + # Get the weekday of the date as an integer between 0 and 6 + weekday = (doomsday_of_year + difference_doomsday_and_date) % 7 - number_to_add_century = numbers_by_century[(date_obj.year // 100) % 4] + # Return the weekday as a string + return WEEKDAYS[weekday] - anchor_day = WEEKDAYS[closest_multiple_of_seven + number_to_add_century] - print(anchor_day) +def get_doomsday_year(year: int) -> int: + """Gets the doomsday of the year as an integer between 0 and 6""" + last_two_numbers_of_year: int = year % 100 + if last_two_numbers_of_year % 2 != 0: + last_two_numbers_of_year += 11 + last_two_numbers_of_year //= 2 + if last_two_numbers_of_year % 2 != 0: + last_two_numbers_of_year += 11 - except ValueError: - # If the conversion fails, print an error message - print("Incorrect date format. Please use the format YYYY-MM-dd.") - return None + # Get the difference between the last two numbers of the year and the + # closest superior or equal multiple of 7 + difference_multiple_of_7 = last_two_numbers_of_year % 7 + if difference_multiple_of_7 != 0: + difference_multiple_of_7 = 7 - difference_multiple_of_7 + # Number to add to the difference to get the doomsday of the year + numbers_to_add_by_century: list[int] = [2, 0, 5, 3] -get_weekday_for_date('2021-10-10') + # Return the doomsday of the year as an integer between 0 and 6 + return (difference_multiple_of_7 + numbers_to_add_by_century[ + (year // 100) % 4]) % 7 diff --git a/doomsday/date.py b/doomsday/date.py index f04d633..2d3ccc6 100644 --- a/doomsday/date.py +++ b/doomsday/date.py @@ -1,18 +1,28 @@ -def is_valid_date(date_str): +def is_valid_date(date_str: str) -> bool: from datetime import datetime try: - 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 it is not, return False and print an error message if date_obj.year < 1583: print("The year must be greater than or equal to 1583.") + return False return True + # If the date is not in the correct format (YYYY-MM-dd) or if the date + # doesn't exist (for example : 2021-18-45), then return False and print + # an error message except ValueError: print("Incorrect date format. Please use the format YYYY-MM-dd.") + return False - return True + except Exception as e: + # Handle any other exception + print(e) + + return False From 4ac87bfb39996df7d516e011e34adc63efcf9831 Mon Sep 17 00:00:00 2001 From: Nathan Deroche Date: Fri, 17 Nov 2023 11:43:56 +0100 Subject: [PATCH 4/5] Small fixes --- __main__.py | 6 ++---- doomsday/algorithm.py | 18 +++++++++--------- doomsday/date.py | 4 +++- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/__main__.py b/__main__.py index 3ec4f9a..2723d1c 100644 --- a/__main__.py +++ b/__main__.py @@ -8,10 +8,8 @@ def main(): "Please enter a date using the format YYYY-MM-dd : ") # Check date input - if not is_valid_date(user_input): - continue - - break + if is_valid_date(user_input): + break print( f"{user_input} was a {get_weekday_for_date(user_input)}") diff --git a/doomsday/algorithm.py b/doomsday/algorithm.py index ff108f4..1ba4d3e 100644 --- a/doomsday/algorithm.py +++ b/doomsday/algorithm.py @@ -1,3 +1,5 @@ +from datetime import datetime + WEEKDAYS = ( 'Sunday', 'Monday', @@ -16,8 +18,6 @@ def get_weekday_for_date(date_str: str) -> str: """ Returns the day of the week for the given date in the format YYYY-MM-dd. """ - from datetime import datetime - # Convert date to datetime date = datetime.strptime(date_str, '%Y-%m-%d') # Get the doomsday of the year @@ -41,17 +41,17 @@ def get_weekday_for_date(date_str: str) -> str: def get_doomsday_year(year: int) -> int: """Gets the doomsday of the year as an integer between 0 and 6""" - last_two_numbers_of_year: int = year % 100 + last_two_digits_of_year: int = year % 100 - if last_two_numbers_of_year % 2 != 0: - last_two_numbers_of_year += 11 - last_two_numbers_of_year //= 2 - if last_two_numbers_of_year % 2 != 0: - last_two_numbers_of_year += 11 + if last_two_digits_of_year % 2 != 0: + last_two_digits_of_year += 11 + last_two_digits_of_year //= 2 + if last_two_digits_of_year % 2 != 0: + last_two_digits_of_year += 11 # Get the difference between the last two numbers of the year and the # closest superior or equal multiple of 7 - difference_multiple_of_7 = last_two_numbers_of_year % 7 + difference_multiple_of_7 = last_two_digits_of_year % 7 if difference_multiple_of_7 != 0: difference_multiple_of_7 = 7 - difference_multiple_of_7 diff --git a/doomsday/date.py b/doomsday/date.py index 2d3ccc6..e29521b 100644 --- a/doomsday/date.py +++ b/doomsday/date.py @@ -17,7 +17,9 @@ def is_valid_date(date_str: str) -> bool: # doesn't exist (for example : 2021-18-45), then return False and print # an error message except ValueError: - print("Incorrect date format. Please use the format YYYY-MM-dd.") + print( + f"Incorrect date {date_str}." + " Please use the format YYYY-MM-dd or check if the date exists.") return False From c1deec02d27f4549b0d2cec25d55b1fc5c6079fc Mon Sep 17 00:00:00 2001 From: Nathan Deroche <145701392+NanaNana64@users.noreply.github.com> Date: Mon, 20 Nov 2023 01:45:46 +0100 Subject: [PATCH 5/5] Delete .idea directory --- .idea/.gitignore | 8 -------- .idea/inspectionProfiles/profiles_settings.xml | 6 ------ .idea/misc.xml | 7 ------- .idea/modules.xml | 8 -------- .idea/tp-python-doomsday-rule.iml | 8 -------- .idea/vcs.xml | 6 ------ 6 files changed, 43 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/inspectionProfiles/profiles_settings.xml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/tp-python-doomsday-rule.iml delete mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 1c2fda5..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml deleted file mode 100644 index 105ce2d..0000000 --- a/.idea/inspectionProfiles/profiles_settings.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 838dac8..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index dde95a7..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/tp-python-doomsday-rule.iml b/.idea/tp-python-doomsday-rule.iml deleted file mode 100644 index d9e6024..0000000 --- a/.idea/tp-python-doomsday-rule.iml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index c8397c9..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file