Date class

A relatively basic class designed to represent dates accurately over a very wide range, running from 4713BC to 9999AD, and handling the various calendar corrections, primarily the Julian/Gregorian changeover. It handles various kinds of date arithmetic efficiently, but conversions to and from more conventional month/day/year representations are more costly.

Member functions:

Constructors: default, copy, time_t, char *, 3 integers

Prototypes:

  • Date()
  • Date( const Date& )
  • Date( const time_t )
  • Date( const int, const int, const int )

The class provides a default constructor that initializes the object to January 1, 4713BC, and a standard copy constructor. It provides 3 additional constructors as well. One initializes the object from a Unix time_t value, extracting the month, day and year of the time_t via the standard library routines and initializing the object to that date. Another initializes the object from a character string produced by the ToString() method, primarily for recreating objects stored in data files. The third takes the day, month and year in that order and initializes an object corresponding to that date.

Assignment operator

Prototypes:

  • Date& operator=( const Date& )

A standard assignment operator is provided to assign one Date object to another correctly.

Relational operators

Prototypes:

  • int operator==( const Date& )
  • int operator!=( const Date& )
  • int operator<( const Date& )
  • int operator<=( const Date& )
  • int operator>( const Date& )
  • int operator>=( const Date& )

The complete set of relational operators is defined on pairs of Date objects. No comparisons to other types are defined.

Arithmetic operators: +, -, +=, -=, ++, --

Prototypes:

  • friend Date& operator+( const Date&, const long )
  • friend Date& operator+( const long, const Date& )
  • Date& operator+=( long )
  • friend Date& operator-( const Date&, const long )
  • friend Date& operator-( const long, const Date& )
  • long operator-( const Date& )
  • Date& operator-=( long )
  • Date& operator++()
  • Date operator++( int )
  • Date& operator--()
  • Date operator--( int )

Operators are defined to add longs to Dates, and to subtract longs from Dates. These operate by adding or subtracting the integer number of days to or from the Date object. Pre-increment and post-increment operators are defined analogous to the operators on integral types. The assignment addition and subtraction operators add and subtract the integral number of dates on the right side to or from the Date object on the left, eg. DateObject += 5; to add 5 days to a given date. Subtraction of two Date operators is defined, yielding the number of days between them as a long. Addition of two Date objects does not make sense and is not defined.

Conversion functions: ToString(), ToSysTime()

Prototypes:

  • time_t ToSysTime()
  • void ToString( char * )

ToSysTime() takes no arguments and returns the date as a time_t value corresponding to midnight on the given date. ToString returns no value, it's argument is a pointer to a 12-byte character string which will be filled in with a string representation of the object. This may be passed to the char * constructor to recreate the object, and is a convenient representation for storage in a data file. There really ought to be a conversion operator to a character string, but it is rather difficult to handle the memory issues correctly.

Data extraction functions

Prototypes:

  • int Year()
  • int Month()
  • int Day()
  • int DayOfYear()
  • int DayOfWeek()
  • int DayOfWorkWeek()
  • int IsLeapYear()
  • void YMD( int *, int *, int *)

These functions extract the obvious values from the date and return them to the program for use. Some non-obvious pointers:

  • DayOfWeek() returns Sunday as 0, while DayOfWorkWeek() returns Monday as 0.
  • YMD() returns all three of the year, month and day into the integers pointed to by it's arguments in that order, making it a convenient way to get those values in one operation for formatting and printing dates.
  • IsLeapYear() is a boolean function, returning non-zero if the year the date is in is a leap year and zero if it is not.

Static class functions:

Leap year determinations

Prototypes:

  • int IsLeapYear( int )

The sole static function in the class is the IsLeapYear() function, which tests if the year passed as it's argument is a leap year or not. It returns non-zero if the year is a leap year, and zero if it is not.

Licensing:

This code is licensed under the terms of the GPL v3, a copy of which you can find attached to this page.