diff --git a/__main__.py b/__main__.py index 53c2c43..c68067c 100644 --- a/__main__.py +++ b/__main__.py @@ -1,5 +1,23 @@ +from doomsday.algorithm import get_weekday_for_date +from tests.test_algorithm import TestAlgorithm +from tests.test_date import TestDate + + def main() -> None: - print("Hello world") + dates = ["2000-01-01", + "2001-01-29", + "2017-12-15", + "2023-11-15", + "2111-12-12"] # Saturday, Monday, Friday, Wednesday, Saturday + for date in dates: + print("{0} is a {1}.".format(date, get_weekday_for_date(date))) + + test_date = TestDate() + test_date.test_valid_dates() + test_date.test_invalid_dates() + + test_algorithm = TestAlgorithm() + test_algorithm.test_algorithm() main() diff --git a/doomsday/algorithm.py b/doomsday/algorithm.py index cf81d60..d64b3a8 100644 --- a/doomsday/algorithm.py +++ b/doomsday/algorithm.py @@ -1,2 +1,49 @@ +from doomsday.date import is_valid_date + + def get_weekday_for_date(date: str) -> str: - return "Sunday" + if not is_valid_date(date): + return "Invalid date" + + year, month, day = map(int, date.split('-')) + day_of_week = calculate_doomsday(year, month, day) + + week_days = ["Sunday", "Monday", + "Tuesday", "Wednesday", + "Thursday", "Friday", "Saturday"] + return week_days[day_of_week] + + +def get_year_anchor(year: int) -> int: + # Define the anchor day for the century + anchor_day = 2 + + # Calculate the century anchor day + century = year // 100 + century_anchor = (5 * (century % 4) + anchor_day) % 7 + + # Calculate the year anchor day + year_within_century = year % 100 + year_anchor = (century_anchor + (year_within_century // 12) + + (year_within_century % 12) + + ((year_within_century % 12) // 4) + ) % 7 + + return year_anchor + + +def calculate_doomsday(year: int, month: int, day: int) -> int: + year_anchor = get_year_anchor(year) + + # Doomsday for each month + doomsday_month = [3, 0, 0, 4, 9, 6, 11, 8, 5, 10, 7, 12] + + # Adjustments for January and February in leap years + if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0): + doomsday_month[0] = 4 + doomsday_month[1] = 1 + + # Calculate the day of the week for the given date + day_of_week = (day - doomsday_month[month - 1] + year_anchor) % 7 + + return day_of_week diff --git a/doomsday/date.py b/doomsday/date.py index 0f8e737..fc5f9ea 100644 --- a/doomsday/date.py +++ b/doomsday/date.py @@ -1,2 +1,22 @@ def is_valid_date(date: str) -> bool: - return True + try: + year, month, day = map(int, date.split('-')) + + if year < 1583: + return False + + if 1 <= month <= 12 and 1 <= day <= 31: + if month in [4, 6, 9, 11] and day > 30: + return False + elif month == 2: # February + if day > 29: + return False + elif day == 29 and not (year % 4 == 0 and + (year % 100 != 0 or year % 400 == 0)): + return False # February 29 is only valid in leap years + + return True + except ValueError: + pass + + return False