Year-Month-Day-to-Day

From Tech
Jump to navigationJump to search

Sometimes you want to convert a Gregorian year-month-day to a simple day number, without using system libraries (for example, in embedded code that doesn't have those libraries). The following python-3 onliner does that (for days after 1900-02-28 and before 2100-03-01):

def calcday(y, m, d):
    """
    Return days since 2000-01-01
    y: year
    m: month (1=jan)
    d: day (1=first day of month)
    """ 
    return m*30 + 14*m//25+d - (m>2)*(2-(y%4==0))+365*y+(y-1)//4 - 730530