Next / Previous / Contents / TCC Help System / NM Tech homepage

9.5. dayNo(): Date to day number

This method returns the number of days between the given date dt, as either a datetime.date or datetime.datetime instance, and January 0 of that year.

sidereal.py
# - - -   d a y N o

def dayNo ( dt ):
    """Compute the day number within the year.

      [ dt is a date as a datetime.datetime or datetime.date ->
          return the number of days between dt and Dec. 31 of
          the preceding year ]
    """

This computation will use the Python datetime package's concept of the “proleptic Gregorian ordinal,” which is a day number based on January 1, 1AD. Both datetime.date and datetime.datetime instances have a .toordinal() method that returns this number.

The datetime.date() constructor will not accept a date of January 0 (“ValueError: day is out of range for month”), so we'll subtract the ordinal of January 1, and then add 1.

sidereal.py
    #-- 1 --
    # [ dateOrd  :=  proleptic Gregorian ordinal of dt
    #   jan1Ord  :=  proleptic Gregorian ordinal of January 1
    #                of year (dt.year) ]
    dateOrd  =  dt.toordinal()
    jan1Ord  =  datetime.date ( dt.year, 1, 1 ).toordinal()

    #-- 2 --
    return  dateOrd - jan1Ord + 1