简体   繁体   中英

days inaccurate in year one

I wrote the below code to give me a certain day at the end of the year in which the pay periods for the year begin. For some reason it is telling me that dec 22, 1 (the year one) was on a Saturday. Online calendars tell me it was on a Thursday.

from datetime import datetime, timedelta

def pp_firstday(year):
    print(type(year))
    firstday = datetime(1,12,22,0,0,0)
    yr = 2
    print(yr, " ", firstday.strftime("%b/%d/%Y  %a"))
    yr = 3
    while int(firstday.strftime("%Y")) != year-1:
        firstday += timedelta(weeks=52)
        if int(firstday.strftime("%m")) <= 12 and int(firstday.strftime("%d")) <= 12:
            firstday += timedelta(weeks=2)
            print("27: ",firstday.strftime("%b/%d/%Y  %a"))
        print(yr, " ",firstday.strftime("%b/%d/%Y  %a"))
        yr += 1
    return firstday

year = int(input("Year >>>")) result=pp_firstday(year) print(result.strftime("%b/%d/%Y  %a"))

The problem is that when you run this the output shows: 2 Dec/22/0001 Sat Python thinks Dec 22nd, year 1 was a Saturday. By the time we get to 2019 the output shows: 2020 Dec/21/2019 Sat thus the problem is corrected. What gives?

As per the calendar docs :

Most of these functions and classes rely on the datetime module which uses an idealized calendar, the current Gregorian calendar indefinitely extended in both directions. This matches the definition of the “proleptic Gregorian” calendar in Dershowitz and Reingold's book “Calendrical Calculations”, where it's the base calendar for all computations.

December 22, 1 was a Thursday in the Julian calendar , which was used back then. However, it's a Saturday in the proleptic Gregorian calendar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM