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
18 changes: 13 additions & 5 deletions __main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
def main() -> None:
print("Hello world")


main()
from doomsday.date import is_valid_date
from doomsday.algorithm import get_weekday_for_date

def input_date() -> None:
"""Asks for a date and check if its valid"""
time_str = str(input("Enter date in this format yyyy-mm-dd \n >>"))
if is_valid_date(time_str):
result = get_weekday_for_date(time_str)
print(f"The day of the week is: {result}")
else:
return False

input_date()
48 changes: 46 additions & 2 deletions doomsday/algorithm.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,46 @@
def get_weekday_for_date(date: str) -> str:
return "Sunday"
def get_anchor_day(year: int) -> int:

# Extract the century
century = year // 100 * 100

# Century offset
anchor = ((century // 100) % 4) * 5

# Add the offset based on the century
anchor = (anchor + 2) % 7

return anchor


def get_doomsday(year):

# Extract the last two digits of the year
year_digits = year % 100

# Divide by 12 and take the remainder
quotient = year_digits // 12
remainder = year_digits - 12*quotient

# Apply the doomsday formula
doomsday = (remainder // 4)

doomsday = (quotient+remainder+doomsday+get_anchor_day(year)) % 7

return doomsday

def get_weekday_for_date(date: str):

year, month, day = map(int, date.split("-"))

anchor_day = get_anchor_day(year)

# Get Doomsday
doomsday = get_doomsday(year)

# Calculate day_of_week_index
day_of_week_index = (day - doomsday + anchor_day) % 7

days_of_week = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

# Get the corresponding day in the array
return days_of_week[day_of_week_index]
32 changes: 30 additions & 2 deletions doomsday/date.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,30 @@
def is_valid_date(date: str) -> bool:
return True
def is_valid_date(date: str):
try:
if not date:
raise ValueError("Empty date string")

date = date.split("-", 3)
if len(date) != 3:
raise ValueError("Invalid date format")

year = int(date[0]) if date[0] and int(date[0]) >= 1583 else None
month = int(date[1]) if date[1] and 1 <= int(date[1]) <= 12 else None
if year is None:
raise ValueError("Invalid year")
if month is None:
raise ValueError("Invalid month")
if date[2] and month in [1, 3, 5, 7, 8, 10, 12]:
day = int(date[2]) if 1 <= int(date[2]) <= 31 else None
elif date[2] and month in [4, 6, 9, 11]:
day = int(date[2]) if 1 <= int(date[2]) <= 30 else None
elif date[2] and month == 2:
day = int(date[2]) if is_leap_year(year) and int(date[2]) > 0 and int(date[2]) < 30 or not is_leap_year(year) and int(date[2]) > 0 and int(date[2]) < 29 else None
if day is None:
raise ValueError("Invalid day")
return True

except ValueError as e:
print(e)

def is_leap_year(year: int) -> bool:
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)