Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion __main__.py
Original file line number Diff line number Diff line change
@@ -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()
52 changes: 51 additions & 1 deletion doomsday/algorithm.py
Original file line number Diff line number Diff line change
@@ -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('-'))
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bien !


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
27 changes: 25 additions & 2 deletions doomsday/date.py
Original file line number Diff line number Diff line change
@@ -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