Get one day of the week from Zeller's joint ceremony ~ Incidentally, the perpetual calendar ~

From Zeller's congruence formula, calculate what day of the week is.

calcal.py


#!/usr/bin/python3
import sys
y = int(sys.argv[1])
m = int(sys.argv[2])
d = int(sys.argv[3])
youbia = ["Day", "Month", "fire", "water", "wood", "Money", "soil"]

if (m == 1 or m == 2):
    md = m+10
    yd = y-1
else:
    md = m-2
    yd = y

a = yd // 100
b = yd % 100
youbi = (int(2.6*md - 0.2) + d + b + b//4 + a//4 - 2*a)%7

print("Year", y, "Year", m, "Month", d, "day", youbia[youbi], "It's the day of the week.",sep="")

Output example

$ calcal.py 1967 5 12
May 12, 1967 is Friday.
$ calcal.py 1994 12 25
December 25, 1994 is Sunday.
$ calcal.py 1995 12 24
December 24, 1995 is Sunday.
$ calcal.py 1998 12 24
December 24, 1998 is Thursday.
$ calcal.py 2000 5 12
May 12, 2000 is Friday.
$ calcal.py 2019 12 25
December 25, 2019 is Wednesday.
$ calcal.py 10000 5 12
May 12, 10000 AD is Friday.
$ 

Perpetual calendar

Incidentally, it is a program that outputs the perpetual calendar.

cal.py


#!/usr/bin/python3
import sys
argvs = sys.argv
argc = len( argvs )
months=[31,28,31,30,31,30,31,31,30,31,30,31]
youbia = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]

def weekofday(y,m,d):
  md, yd = (m-2, y) if m > 2 else (m+10, y-1)
  a, b = divmod(yd, 100)
  return ( (int(2.6*md - 0.2) + d + b + b//4 + a//4 - 2*a)%7)

def printcal(y,m):
  mm=months[m-1]+(1 if (m==2 and y%4==0 and y%400!=0) else 0)
  print(y,m,sep='/')
  print (*youbia,sep=' ')
  f=weekofday(y,m,1)
  print("    "*f,end='')
  for i in range(1,mm+1):
    print(" %2d " %i,end='')
    if f==6:
      print("")
    f=(f+1)%7
  if f!=0:
    print("")

  
if argc==3:
  y = int(sys.argv[1])
  m = int(sys.argv[2])
else:
  print("Usage : cal.py y m")
  exit(1)
printcal(y,m)


Output example

$ cal.py 2020 2
2020/2
Sun Mon Tue Wed Thu Fri Sat
                          1 
  2   3   4   5   6   7   8 
  9  10  11  12  13  14  15 
 16  17  18  19  20  21  22 
 23  24  25  26  27  28  29 
$

Permanent calendar (2)

cal.py version 2.0 The code that summarizes the day of the week calculation and the perpetual calendar is shown. The day of the week calculation function is that of the integer operation presented by kkdd. $ ./cal.py y m to display the calendar for the year m month. $ ./cal.py y m d displays the day of the week on the m month d day of the year y.

cal.py


#!/usr/bin/python3
import sys
argvs = sys.argv
argc = len( argvs )
months=[31,28,31,30,31,30,31,31,30,31,30,31]
youbia = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]

def dayofweek(year, month, day):
  m = (month-3)%12
  y = year if month >= 3 else year - 1
  n = day + (13*m+2)//5 + y + y//4 - (y//100) + y//400 + 2
  return (n%7)

def printcal(y,m):
  mm=months[m-1]+(1 if (m==2 and y%4==0 and y%400!=0) else 0)
  print(y,m,sep='/')
  print (*youbia,sep=' ')
  f=dayofweek(y,m,1)
  print("    "*f,end='')
  for i in range(1,mm+1):
    print(" %2d " %i,end='')
    if f==6:
      print("")
    f=(f+1)%7
  if f!=0:
    print("")

if argc==3:
  y = int(sys.argv[1])
  m = int(sys.argv[2])
  printcal(y,m)
elif argc==4:
  y = int(sys.argv[1])
  m = int(sys.argv[2])
  d = int(sys.argv[3])
  f = dayofweek(y,m,d)
  print(y, "/", m, "/", d, " is ", youbia[f],sep="")

else:
  print("Usage : cal.py y m [d]")
  exit(1)

Recommended Posts

Get one day of the week from Zeller's joint ceremony ~ Incidentally, the perpetual calendar ~
[Python] Get the day of the week (English & Japanese)
Get the last day of the specified month
Get wordpress posts from the past week
Get the contents of git diff from python
Find out the day of the week with datetime
Get the return code of the Python script from bat
Which day of the week do you buy gold?
What to do if you get `locale.Error: unsupported locale setting` when getting the day of the week from a date in Python
Aggregate the number of hits per second for one day from the web server log with Python