perpetual.py

Created by tlake

Created on July 21, 2024

2.34 KB

Shows a perpetual calendar


from math import *

# print out table showing monthly calendar
def put_month( month, year ):
    # compute julian day for year and month
    days = 0
    if (month <= 2): 
        days = -1

    jd = 367*year \
        - floor(7*(year + floor((month + 9)/12))/4) \
        + floor(275*month/9) \
        - floor(3*(floor((year + days)/100) + 1)/4) \
        + 1721031

    # get days in this month
    if month in(4,6,9,11):
        days = 30                      # Apr, Jun, Sep, Nov
    elif month == 2:
        days = 28                      # Feb normal
        if year % 4 == 0:
            if not((year % 100 == 0) and \
                   (year % 400 != 0)):
                days = 29              # Feb leap year
    else:
        days = 31                      # other months

    # init with blanks
    for i in range(1,43):
        C[i] = "   "

    # day of week of first day of month
    beg = jd - 7*floor(jd/7)
    for i in range(1,days+1):
        C[i+beg] = '{:3d}'.format(i)

    # display month and year
    print("            ",end='')
    if month == 1:
        print("Jan",end='')
    elif month == 2:
        print("Feb",end='')
    elif month ==  3:
        print("Mar",end='')
    elif month == 4:
        print("Apr",end='')
    elif month == 5:
        print("May",end='')
    elif month == 6:
        print("Jun",end='')
    elif month == 7:
        print("Jul",end='')
    elif month == 8:
        print("Aug",end='')
    elif month == 9:
        print("Sep",end='')
    elif month == 10:
        print("Oct",end='')
    elif month == 11:
        print("Nov",end='')
    elif month == 12:
        print("Dec",end='')
    print('',year)

    # display days
    print("   S   M   T   W   T   F   S")
    print('',C[1],  C[2],  C[3],  C[4],  C[5],  C[6],  C[7])
    print('',C[8],  C[9],  C[10], C[11], C[12], C[13], C[14])
    print('',C[15], C[16], C[17], C[18], C[19], C[20], C[21])
    print('',C[22], C[23], C[24], C[25], C[26], C[27], C[28])
    print('',C[29], C[30], C[31], C[32], C[33], C[34], C[35])
    print('',C[36], C[37], C[38], C[39], C[40], C[41], C[42])

# ask user to continue or not
def another():

    ans = input("another [Y|N]? ")
    return (ans[0] == 'Y') or (ans[0] == 'y')

C = ['' for i in range(0,43)]

while True:
    mm,yy = map(int,input("\nmonth, year? ").split())
    put_month( mm, yy )
    if not another():
        break

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.