liveBook · Manning

Julian day number

As explained in Software Design for Python Programmers

A Julian day number is a count of days elapsed since noon on January 1, 4713 BCE, in the Gregorian calendar, optionally including a fractional part for the time of day.

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

How Software Design for Python Programmers covers it

How deep it goes
Defines the epoch and demonstrates conversion algorithms, while noting their complexity and repeated getter cost.
Examples
Uses Julian day numbers 0, 2,440,000, and 3,000,000 to verify conversions against Gregorian dates.
What it assumes
Assumes Gregorian year-month-day notation while defining the elapsed-day representation from zero.
What you can do after
Apply it via a Date class to add days and calculate differences efficiently.
How it connects
Connects Gregorian calendar conversion, date arithmetic, private state, and lazy evaluation.

Also explained in

Compare how 2 books explain julian day number →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Software Design for Python Programmers. The section links point to the parts of the book it draws on.