Zeller's Congruence - Coded in Python
- davidcarew19
- Dec 13, 2024
- 3 min read
#!C:\Python37\python
# 1 2 3 4 5 6
# ...56789012345678901234567890123456789012345678901234567890...
# C:\Users\DCarew\py3dev\ZellersC_1arg.py
#
# Compute day-of-the-week for given date using Zeller's Congruence
# URL Wikipedia reference to Zeller's formula is:
#
# Note: date m/b YYYYMMDD format, one cmd-line arg: -dYYYYMMDD
# Note2: there is simpler, near-nocode way to do this using python library
# import code (i.e. calendar and perhaps datetime). This program is
# by way of experimenting w/ Zeller's Congruence.
#
# SLIME TRAIL (change history)
#
# DATE WHO DESCRIPTION
# ======= ======= =========================================
# 08/22/2023 See URL hoping this works 'cuz it will be hell to debug otherwise
# URL is: https://stackoverflow.com/questions
# /46002047/day-of-the-week-using-zellers-congruence-
# -python-3
# 10/25/2023 carew Modify to accept single command-line arg in format YYYYMMDD
# 12/13/2024 carew Modify for 70-char width (to fit neatly in a Wix weblog post...
# TO DO: #####################################################################
import argparse
import datetime
import sys
def valid_date(d):
"""
Use datetime library validation to check arg passed in
"""
try:
fulldt = datetime.datetime.strptime(d, "%Y%m%d")
except ValueError:
msg = "Not a valid date: '{0}'.".format(d)
raise argparse.ArgumentTypeError(msg)
return d
# main starts here
parser = argparse.ArgumentParser(prog='ZellersC_1arg',
description="Find calendar day-of-week for 'any' date yrs 1583-9999")
parser.add_argument('-d', "--yyyymmdd",
help="Zeller's Congruence Date - format YYYYMMDD ",
required=True,
type=valid_date)
args = parser.parse_args()
yyyymmdd_dt = args.yyyymmdd
# diagnostics below commented out
# print(" year is: ", yyyymmdd_dt[:4])
# print("month is: ", yyyymmdd_dt[4:6])
# print(" day is: ", yyyymmdd_dt[6:8])
year = int(yyyymmdd_dt[:4])
if year not in range(1583, 10000):
print("Year is out of allowed range 1583 - 9999. Please enter \
valid date arg as -dYYYYMMDD")
sys.exit(2)
month = int(yyyymmdd_dt[4:6])
if month not in range(1, 13):
print("Month is out of allowed range 1 - 12. Enter valid date \
as -dYYYYMMDD")
sys.exit(2)
if month == 1 or month == 2:
month += 12
year -= 1
# adjust month, year numbers for the formula from Wikipedia
day = int(yyyymmdd_dt[6:8])
if day not in range(1,32):
print("day is not good value for of month (1 - 31). \
Please enter date as digits -dYYYYMMDD ")
sys.exit(2)
kay = int(year % 100)
# kay is K century number-- see Gregorian calendar
# formula is in above wikipedia URL reference.
jay = int(year // 100) # jay is J zero-based century number. See
# Wikipedia reference.
# Below is my code to execute the wikipedia formula:
result = (day + (13 * (month+1) // 5) + kay + (kay // 4) + \
(jay // 4) - (2*jay))
result = result % 7
# print("result is: ",result) # diagnostic commented out
weekday = {0:"Saturday", 1:"Sunday", 2:"Monday", 3:"Tuesday",\
4:"Wednesday", 5:"Thursday", 6:"Friday"}
# print("The day of the week for date " + yyyymmdd_dt + " is: " + \
# weekday[int(result)] + ".")
# ...more 'sophisticated' formatting below
print(f"The day of the week for date %4d-%2.2d-%2.2d is: \
{weekday[int(result)]}." % (year,month,day))
Copy the above code and paste it into Notepad (or any editor's blank buffer). Save the code under {whatever-name-you-wish}.py
Run the code using the python launcher "py". Here is an example showing that D-Day was a Tuesday:
$>py -3 ZellersC_narrow_1argp3v3.py -d 19440606
The day of the week for date 1944-06-06 is: Tuesday.
Note: the code is formatted in a terrible, squirrelly way, because Wix text entry is not ideal for entering code-- HOWEVER, I did carefully test it by extracting the source from the above code section, and for me it ran, in spite of non-attractive formatting.
Comments