WWC snapshot of http://www.alw.nih.gov/Docs/NIHCL/nihcl_10.html taken on Sat Jun 10 19:13:38 1995

Go to the previous, next section.

Date--Gregorian Calendar Date

SYNOPSIS

#include <nihcl/Date.h>

BASE CLASS

Object

DERIVED CLASSES

None

RELATED CLASSES

Time

DESCRIPTION

Instances of Class Date represent Gregorian calendar dates. The Gregorian calendar began on October 5, 1582, but was not adopted by Great Britain and her American colonies until September 14, 1752. Class Date does not check for pre-Gregorian dates.

Class Date provides a complete set of calendar functions, including computing a date some number of days before or after another, calculating the number of days between two dates, and determining the day of the week a particular date falls on.

Member functions of class Date use the following data types:

typedef unsigned short dayTy;
typedef unsigned short monthTy;
typedef unsigned short yearTy;
typedef unsigned long julTy;    // Julian day number

Class Date follows the convention that the days of the week are numbered from 1 to 7 beginning with 1 = Monday, days of the month and year are numbered beginning at 1, and months are numbered from 1 to 12.

STATIC MEMBER FUNCTIONS

static dayTy dayOfWeek(const char* dayName)
Returns the number, 1 = Monday through 7 = Sunday, of the day of the week named dayName. For example, dayOfWeek("Monday") is 1 and dayOfWeek("Sun") is 7. The dayName may be abbreviated to three characters and may be upper or lower case. If the name is invalid an NIHCL_BADDAYNAM exception is raised.

static bool dayWithinMonth(monthTy month, dayTy day, yearTy year)
Returns YES if day is a valid day in the specified month and year. For example, dayWithinMonth(2, 29, 1988) is YES and dayWithinMonth(2, 29, 1989) is NO.

static dayTy daysInYear(yearTy year)
Returns the number of days in the year year.

static julTy jday(monthTy m, dayTy d, yearTy y)
Returns the Julian day number for the Gregorian calander date specified by m, d, and y. See Algorithm 199 from Communications of the ACM, Volume 6, No. 8, (Aug. 1963).

static bool leapYear(yearTy year)
Returns YES if year is a leap year.

static const char* nameOfDay(dayTy weekDayNumber)
Converts the number of the day of week weekDayNumber into its name. For example, nameOfDay(1) returns a pointer to the character string "Monday" and nameOfDay(7) returns a pointer to the character string "Sunday". Raises the exception NIHCL_BADDAY if weekDayNumber is > 7.

static const char* nameOfMonth(monthTy monthNumber)
Converts the number of the month monthNumber into its name. For example,
nameOfMonth(1) returns a pointer to the character string "January" and
nameOfMonth(12) returns a pointer to the character string "December". Raises
the exception NIHCL_BADMONTH if monthNumber is > 12.

static monthTy numberOfMonth(const char* monthName)
Converts the name of a month into its number. The monthName may be abbreviated to three characters and may be upper or lower case. For example, numberOfMonth("FEB") returns 2.

CONSTRUCTORS

Date()
Constructs a Date for today's date.

Date(long dayCount)
Constructs a Date for dayCount days before (dayCount < 0) or after (dayCount > 0) January 1, 1901. For example, Date(-1) is December 31, 1900 and Date(1) is January 2, 1901.

Date(long dayCount, yearTy referenceYear)
Constructs a Date for the dayCount day of the referenceYear. Note that dayCount may be larger than 365, and may also be zero or negative, in which case the constructed Date is that of a year after or before the referenceYear, respectively. For example Date(367, 1984) is January 1, 1985, and Date(-1, 1984) is December 30, 1983.

Date(dayTy newDay, const char* monthName, yearTy newYear)
Constructs a Date for the specified day of month, month, and year. If an invalid day of month is specified, for example Date(29, "Feb", 1985), an NIHCL_BADMODAY exception is raised.

Date(istream& strm)
Constructs a Date by parsing input from input stream strm. The date may be in any of the following three formats:

MM/DD/YY        3/10/86
DD-MMM-YY       10-MAR-86
MMM DD, YY      March 10, 1986

Month names may be abbreviated to three characters, and may be in upper or lower case. Years from 00 to 99 are interpreted as 1900 to 1999, and any non-alphanumeric characters may be used as delimiters.

ARITHMETIC OPERATORS

friend Date operator+(const Date& dt, int dd)
friend Date operator+(int dd, const Date& dt)
Returns a Date that is dd days after the Date dt if dd is positive, or dd days before dt if dd is negative.

Date operator-(int dd) const
Returns a Date that is dd days before this Date if dd is positive, or dd days after this Date if dd is negative.

int operator-(const Date&) const
Returns the number of days between this Date and the argument Date. The result is negative if the argument Date is later than this Date.

void operator+=(int dd)
Replaces this Date by one that is dd days after this Date if dd is positive, or dd days before this Date if dd is negative.

void operator-=(int dd)
Replaces this Date by one that is dd days before this Date if dd is positive, or dd days after this Date if dd is negative.

RELATIONAL OPERATORS

bool operator<(const Date&) const
bool operator<=(const Date&) const
bool operator>(const Date&) const
bool operator>=(const Date&) const
bool operator==(const Date&) const
bool operator!=(const Date&) const
Returns YES if the indicated relation holds between this Date and the argument Date.

COMPARING DATES

Date max(const Date& dt) const
Returns the later of this Date and the argument Date.

Date min(const Date& dt) const
Returns the earlier of this Date and the argument Date.

virtual int compare(const Object& ob) const
Compares this Date with the argument object ob, which must also be an instance of class Date. Returns a negative result if this Date is earlier than ob, zero if this Date equals ob, and a positive result if this Date is later than ob.

TESTING DATES

bool between(const Date& min, const Date& max) const
Returns YES if this Date is later than or equal to min and earlier than or equal to max.

bool leap() const
Returns YES if this Date is a leap year.

virtual bool isEqual(const Object& ob) const
Returns YES if ob is of species Date and equals this Date.

CONVERTING DATES

dayTy day() const
Returns the day of the year of this Date. For example, Date(1,"Jan",1986).day() is 1, and Date(1,"Mar",1986).day() is 60.

dayTy dayOfMonth() const
Returns the day of the month of this Date. For example, Date(1,"Mar",1986).day() is 1.

dayTy firstDayOfMonth() const
Returns the number of the day of the year that is the first day of the month in the year of this Date. For example, Date(10,"March",1986).firstDayOfMonth() is 60.

dayTy firstDayOfMonth(monthTy month) const
Returns the number of the day of the year that is the first day of month in the year of this Date. For example, Date(10,"March",1986).firstDayOfMonth(2) is 32.

void mdy(monthTy& m, dayTy& d, yearTy& y) const
Returns the month m, day of month d, and year y of this Date.

monthTy month() const
Returns the number of this Date's month. For example, Date(10,"Mar",86) is 3.

const char* nameOfMonth() const
Returns a pointer to a character string with the name of this Date's month. For example, Date(10,"MAR",86).nameOfMonth() is a pointer to the string "March".

Date previous(const char* dayName) const
Returns the Date of the last weekday named dayName prior to or on this Date. For example, the date of the last Sunday in April in the year 1984 can be calculated by the expression:

Date(30,"APR",1984).previous("Sun")

dayTy weekDay() const
Returns the number, 1 = Monday through 7 = Sunday, of the day of the week for this Date.

yearTy year() const
Returns the year of this Date.

SEARCHING

virtual unsigned hash() const
Returns a number suitable for use as a hash table probe.

COPYING DATES

virtual void deepenShallowCopy()
This function is a no-op for class Date.

READING AND PRINTING DATES

virtual void printOn(ostream& strm =cout) const
Prints this Date on the output stream strm in the format DD-MMM-YY.

virtual void scanFrom(istream& strm)
Replaces this Date by that read from the input stream strm. The format is the same as that described for the constructor Date(istream&).

DATE SPECIES

virtual const Class* species() const
Returns a pointer to the class descriptor for class Date.

PROTECTED MEMBERS

Object I/O

virtual void storer(OIOofd& fd) const
virtual void storer(OIOout& strm) const
Stores this Date on fd or strm.

EXCEPTIONS RAISED

NIHCL_BADMODAY, NIHCL_BADDAYNAM, NIHCL_BADMONAM, NIHCL_BADMONTH,
NIHCL_BADDAY, NIHCL_RDFAIL

Go to the previous, next section.