A simple Common Era date class

The following is an exercise in object-oriented programming intended for teaching purposes. We will consider a class representing calendar dates, with support for constructing, displaying, incrementing, and decrementing dates. Unlike our earlier classes representing durations, this class represents a particular day in history.

Students have previously had the opportunity to explore algorithms for determining leap years and the historical transition from the Julian calendar to the Gregorian calendar. This exercise brings these ideas together in a single Date class.

For students interested in other calendar systems, two additional documents introduce the Islamic and Jewish calendars, found in the Islamic-calendar and Jewish-calendar subdirectories, respectively. These documents describe the mathematical rules underlying these calendars and how they might be implemented in software. Students interested in exploring these or other calendar systems are welcome to discuss them with me.

Calendar conventions

The class uses the Julian calendar for dates up to October 4, 1582, and the Gregorian calendar beginning October 15, 1582. Consequently, incrementing October 4, 1582 produces October 15, 1582, and decrementing October 15, 1582 produces October 4, 1582. The dates October 5 through 14, 1582 are not valid.

This follows the original Gregorian calendar reform of 1582. In practice, different countries adopted the Gregorian calendar at different times, but this exercise uses a single transition date for simplicity.

The class supports only dates in the Common Era, beginning with January 1, 1. There is no year zero, and dates before the Common Era are not supported. This avoids the additional complications of representing and calculating dates before the Common Era.

Specification and design

We begin with a requirements specification describing the public interface and expected behaviour of the class. A separate design document describes how these requirements are implemented.

Date class requirements specification

Date class design

Implementation and testing

The implementation uses private member variables to store the year, month, and day. The constructor checks that the specified date is valid, while the increment and decrement operators handle the lengths of months, leap years, and the calendar transition. These operations ensure that a valid date remains valid.

The C++ implementation is divided into a header file containing the declarations and a source file containing the definitions. Two additional programs demonstrate how the class is used and tested:

Comments and disclaimer

The purpose of this exercise is not to develop a comprehensive historical calendar library. Rather, it is to demonstrate how a class can maintain valid dates while handling special cases such as leap years, month boundaries, and changes in calendar conventions.

This exercise and its underlying ideas are those of Douglas Wilhelm Harder. The specification, design, and C++ implementation were developed with the assistance of ChatGPT.