Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

# (jim) C:\Users\Jim>python dow.py

# Enter a date (yyyy-mm-dd): 2024-05-24

# The day of the week for 2024-05-24 is Friday.

def compute_day_of_week(date):

    day, month, year = date
    century = year // 100
    year_part = year % 100

    # Compute Century-Item
    if year < 1752 or (year == 1752 and (month < 9 or (month == 9 and day < 14))):
        century_item = (18 - century) % 7
    else:
        century_item = ((3 - (century % 4)) * 2) % 7
    
    # Compute Year-Item
    year_item = (year_part + (year_part // 4)) % 7
    
    # Compute Month-Item
    month_items = [0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5]
    month_item = month_items[month - 1]
    
    # Compute Day-Item
    day_item = day % 7
    
    # Total
    total = (century_item + year_item + month_item + day_item) % 7
    
    # Correction for Leap Year
    if month <= 2 and (year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)):
        total = (total - 1 + 7) % 7
    
    return total
def day_of_week_string(day_index):

    days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
    return days[day_index]
# Prompt for the date

date_input = input("Enter a date (yyyy-mm-dd): ")

year, month, day = map(int, date_input.split('-'))

date = (day, month, year)

# Compute and print the day of the week

day_index = compute_day_of_week(date) day_name = day_of_week_string(day_index) print(f"The day of the week for {date_input} is {day_name}.")



Including 1752 is problematic, in the British Empire, it was the only year with 355 days, as September 3–13 were skipped when the Empire adopted the Gregorian calendar.

These skipped days were a rolling problem across the world, when they were applied varied by country and religion of the recorder of historic accounts - they were by no means uniformly applied in the same year across the planet.


I like the story of how the portuguese sailed east, and the spanish sailed west, and by the time they met each other (Macau trading with Manila?) there was a day dislocation between the two empires. Nowadays we have timezones and an International Date Line* to solve the underlying issue, but back then it seems as if no one was surprised that calendars would be off by certain numbers of days between ports, and merely took it in stride.

Lagniappe: https://en.wikipedia.org/wiki/List_of_adoption_dates_of_the_...

* shout out to Kiribati (UTC+14)! I'd like to think the bronze age danes rocked their string skirts in much the same way the Kiribati do their "grass" [pandanus] ones. https://www.youtube.com/watch?v=ZUhs1TdHMt0


You are right. This code would not be accurate for many regions in the year they transitioned from the Julian to the Gregorian calendar




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: