A Julian day number counts days elapsed since noon on January 1, 4713 BCE, in the Gregorian calendar. It is whole at noon and may include a fractional time-of-day part. section 5.3.2
Representing dates with Julian day numbers makes date arithmetic direct: adding days adds to the number, and subtracting numbers gives the day difference. Converting to or from Gregorian year, month, and day remains algorithmically complicated. section 5.3.2
A Date implementation can use the number as hidden state:
- Convert Gregorian components with _to_julian(). section 5.3.2
- Recover year, month, and day with _to_ymd(). section 5.3.2
- Implement add_days by adding to the stored number and days_from by subtraction. section 5.3.2
Calibration date
Julian day number 2,440,000 corresponds to May 23, 1968. Julian day number 0 corresponds to January 1, 4713 BCE. section 5.3.2 section 5.4
Listing 5.11 (Program 5.3 DateArithmetic-2): date.py (4 of 4; could be better!)
def __init__(self, *parms):
if len(parms) == 1:
self._julian = parms[0] #1
else:
self._julian = Date._to_julian(*parms) #2
@property
def year(self):
year, _, _ = Date._to_ymd(self._julian) #3
return year
@property
def month(self):
_, month, _ = Date._to_ymd(self._julian) #3
return month
@property
def day(self):
_, _, day = Date._to_ymd(self._julian) #3
return day
def add_days(self, n):
return Date(self._julian + n) #4
def days_from(self, other):
return self._julian - other._julian #5
Inspect a Date class that stores Julian state while exposing Gregorian properties and arithmetic methods. section 5.3.2

