Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

Core::Date Class Reference

#include <Date.h>

Inheritance diagram for Core::Date:

Inheritance graph
[legend]
Collaboration diagram for Core::Date:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 Date (int month=1, int day=1, int year=1900)
virtual StringclassName (void) const
 Answer receiver class name.
virtual int month (void) const
 Answer the month.
virtual int day (void) const
 Answer the day of the month.
virtual int year (void) const
 Answer the year.
virtual int weekday (void) const
 Answer the weekday.
virtual int century (void) const
 Answer the century.
virtual DateaddDays (int dayCount)
 Adds the given number of days to the current date.
virtual DateaddMonths (int monthCount)
 Adds the given number of months to the current date.
virtual DateaddYears (int yearCount)
 Adds the given number of years to the current date.
virtual DatesubtractDays (int dayCount)
virtual DatesubtractMonths (int monthCount)
virtual DatesubtractYears (int yearCount)
virtual unsigned long dayDifferenceFrom (Date *date)
virtual long hash (void) const
 Answer object hash value.
virtual bool isEqual (const Object *object) const
 Compare receiver with given object.
virtual bool isEqual (const Object &object) const
 Compare receiver with given object.
virtual unsigned long asDays (void) const
virtual void printOn (Stream *stream) const
 Print object identification into the stream.
virtual bool isLeapYear (void) const
 Determines if the Date's year is a leap year.

Static Public Member Functions

static Datetoday (void)
static int daysInMonth (int month, int year)
static bool isLeapYear (short year)
 Determines if the given year is a leap year.

Protected Member Functions

void gregorianDate (int &month, int &day, int &year) const

Static Protected Member Functions

static unsigned long julianDay (int month, int day, int year)

Protected Attributes

unsigned long julian_day_number

Private Member Functions

 Date (unsigned long julianDate)

Constructor & Destructor Documentation

Date::Date unsigned long  julianDate  )  [private]
 

Definition at line 32 of file Date.cc.

References julian_day_number.

Referenced by addDays(), addMonths(), addYears(), subtractDays(), subtractMonths(), subtractYears(), and today().

00033 {
00034     julian_day_number = julianDate;
00035 }

Date::Date int  month = 1,
int  day = 1,
int  year = 1900
 

Definition at line 37 of file Date.cc.

References julian_day_number, and julianDay().

00038 {
00039     julian_day_number = julianDay(month, day, year);
00040 }


Member Function Documentation

Date * Date::addDays int  dayCount  )  [virtual]
 

Adds the given number of days to the current date.

If the month/day combination isn't valid, the month rolls over.

Definition at line 110 of file Date.cc.

References Date(), and julian_day_number.

00111 {
00112     return new Date(julian_day_number + dayCount);
00113 }

Date * Date::addMonths int  monthCount  )  [virtual]
 

Adds the given number of months to the current date.

If the month/day combination isn't valid, the day is cut down. For example, adding 1 month to Jan 30 on a non leap year will result in the date Feb 28.

Definition at line 120 of file Date.cc.

References Date(), daysInMonth(), and gregorianDate().

00121 {
00122     int m, d, y;
00123     gregorianDate(m, d, y);
00124     m += monthCount;
00125     while (m > 12) {
00126         m -= 12; y += 1;
00127     }
00128     int dm = daysInMonth(m, y);
00129     if (d > dm) d = dm;
00130     return new Date(m, d, y);
00131 }

Date * Date::addYears int  yearCount  )  [virtual]
 

Adds the given number of years to the current date.

If the month/day/year combination isn't valid, the month/day rolls over.

Definition at line 136 of file Date.cc.

References Date(), and gregorianDate().

00137 {
00138     int m, d, y;
00139     gregorianDate(m, d, y);
00140     return new Date(m, d, y+yearCount);
00141 }

unsigned long Date::asDays void   )  const [virtual]
 

Definition at line 191 of file Date.cc.

References julian_day_number.

Referenced by dayDifferenceFrom().

00192 {
00193     return julian_day_number;
00194 }

int Date::century void   )  const [virtual]
 

Answer the century.

Definition at line 93 of file Date.cc.

References year().

00094 {
00095     return year() / 100;
00096 }

String * Date::className void   )  const [virtual]
 

Answer receiver class name.

Because there isn't any standard way to obtain class name this method comes to place.

Every class should rewrite this method but many didn't (yet).

Reimplemented from Core::Object.

Definition at line 43 of file Date.cc.

00044 {
00045     return new String("Date");
00046 }

int Date::day void   )  const [virtual]
 

Answer the day of the month.

Definition at line 66 of file Date.cc.

References gregorianDate().

Referenced by Core::Timestamp::fromDateAndTime().

00067 {
00068     int m, d, y;
00069     gregorianDate(m, d, y);
00070     return d;
00071 }

unsigned long Date::dayDifferenceFrom Date date  )  [virtual]
 

Definition at line 168 of file Date.cc.

References asDays().

00169 {
00170     unsigned long myDays, dateDays;
00171     myDays = asDays();
00172     dateDays = date->asDays();
00173     if (myDays > dateDays)
00174         return myDays - dateDays;
00175     return dateDays - myDays;
00176 }

int Date::daysInMonth int  month,
int  year
[static]
 

Definition at line 98 of file Date.cc.

References days_in_month, and isLeapYear().

Referenced by addMonths(), and subtractMonths().

00099 {
00100     int dayCount = days_in_month[month];
00101     if (month == 2 && isLeapYear(year))      // Take care of February
00102         dayCount = 29;
00103     return dayCount;
00104 }

void Date::gregorianDate int &  month,
int &  day,
int &  year
const [protected]
 

Definition at line 247 of file Date.cc.

References julian_day_number.

Referenced by addMonths(), addYears(), day(), month(), printOn(), subtractMonths(), subtractYears(), and year().

00248 {
00249     unsigned long j = julian_day_number - 1721119;
00250     unsigned long y = ((j<<2) - 1) / 146097;
00251     j = (j<<2) - 1 - 146097*y;
00252     unsigned long d = j>>2;
00253     j = ((d<<2) + 3) / 1461;
00254     d = (d<<2) + 3 - 1461*j;
00255     d = (d + 4)>>2;
00256     unsigned long m = (5*d - 3)/153;
00257     d = 5*d - 3 - 153*m;
00258     d = (d + 5)/5;
00259     y = 100*y + j;
00260     if (m < 10) m += 3;
00261     else {
00262         m -= 9;
00263         y++;
00264     }
00265     month = (int) m;
00266     day   = (int) d;
00267     year  = (int) y;
00268 }

long Date::hash void   )  const [virtual]
 

Answer object hash value.

The value should be same for objects found equal with isEqual() method. So if you rewrite this method you should rewrite isEqual() method too.

Reimplemented from Core::Object.

Definition at line 179 of file Date.cc.

References julian_day_number.

Referenced by isEqual().

00180 {
00181     return (long) julian_day_number;
00182 }

virtual bool Core::Date::isEqual const Object object  )  const [inline, virtual]
 

Compare receiver with given object.

Do not rewrite this method. It just a sugar for Object::isEqual(Object *).

  • object - object to compare receiver with

Reimplemented from Core::Object.

Definition at line 92 of file Date.h.

References Core::Object::isEqual().

00093       { return Object::isEqual(object); }

bool Date::isEqual const Object object  )  const [virtual]
 

Compare receiver with given object.

This method could compare objects with more sophisticated algorithm (eg. based on instance variables comparing or so).

If you rewrite this method you should rewrite hash() too.

  • object - object to compare receiver with

Reimplemented from Core::Object.

Definition at line 184 of file Date.cc.

References hash().

00185 {
00186     if (!object->className()->isEqual(this->className())) return false;
00187     return object->hash() == this->hash();
00188 }

bool Date::isLeapYear short  year  )  [static]
 

Determines if the given year is a leap year.

Definition at line 215 of file Date.cc.

00217 {
00218     return ((year & 3) == 0 && year % 100 != 0 || year % 400 == 0);
00219 }

bool Date::isLeapYear void   )  const [virtual]
 

Determines if the Date's year is a leap year.

Definition at line 209 of file Date.cc.

References year().

Referenced by daysInMonth(), and Core::Timestamp::isLeapYear().

00210 {
00211     return isLeapYear(year());
00212 }

unsigned long Date::julianDay int  month,
int  day,
int  year
[static, protected]
 

Definition at line 228 of file Date.cc.

Referenced by Date().

00229 {
00230     unsigned long c, ya;
00231     if (month > 2) month -= 3;
00232     else {
00233         month += 9;
00234         year--;
00235     }
00236     ya = year % 100;
00237     c = year / 100;
00238     return ((146097*c)>>2) + ((1461*ya)>>2) + (153*month + 2)/5 + day + 1721119;
00239 }

int Date::month void   )  const [virtual]
 

Answer the month.

This is a number between 1 and 12.

Definition at line 58 of file Date.cc.

References gregorianDate().

Referenced by Core::Timestamp::fromDateAndTime().

00059 {
00060     int m, d, y;
00061     gregorianDate(m, d, y);
00062     return m;
00063 }

void Date::printOn Stream stream  )  const [virtual]
 

Print object identification into the stream.

Object identification is formed from its className() by default. But complicated classes (eg. collections) may print some other information.

  • stream - stream to print to.

Reimplemented from Core::Object.

Definition at line 197 of file Date.cc.

References Core::String::format(), gregorianDate(), and Core::Stream::nextPutAll().

00198 {
00199     /* Print self in numbers. Locale should know month names. */
00200     String *dateStr;
00201     int m, d, y;
00202     gregorianDate(m, d, y);
00203     dateStr = String::format("%u.%u.%u", d, m, y);
00204     stream->nextPutAll(dateStr);
00205 }

Date * Date::subtractDays int  dayCount  )  [virtual]
 

Definition at line 143 of file Date.cc.

References Date(), and julian_day_number.

00144 {
00145     return new Date(julian_day_number - dayCount);
00146 }

Date * Date::subtractMonths int  monthCount  )  [virtual]
 

Definition at line 148 of file Date.cc.

References Date(), daysInMonth(), and gregorianDate().

00149 {
00150     int m, d, y;
00151     gregorianDate(m, d, y);
00152     m -= monthCount;
00153     while (m <= 0) {
00154         m += 12; y -= 1;
00155     }
00156     int dm = daysInMonth(m, y);
00157     if (d > dm) d = dm;
00158     return new Date(m, d, y);
00159 }

Date * Date::subtractYears int  yearCount  )  [virtual]
 

Definition at line 161 of file Date.cc.

References Date(), and gregorianDate().

00162 {
00163     int m, d, y;
00164     gregorianDate(m, d, y);
00165     return new Date(m, d, y-yearCount);
00166 }

Date * Date::today void   )  [static]
 

Definition at line 49 of file Date.cc.

References Date().

00050 {
00051     time_t timestamp = time(NULL);
00052     const struct tm* date = localtime(&timestamp);
00053     return new Date(date->tm_mon+1, date->tm_mday, date->tm_year+1900);
00054 }

int Date::weekday void   )  const [virtual]
 

Answer the weekday.

1 for Monday, 7 for Sunday

Definition at line 82 of file Date.cc.

References julian_day_number.

00088 {
00089     return ((((julian_day_number + 1) % 7) + 6) % 7) + 1;
00090 }

int Date::year void   )  const [virtual]
 

Answer the year.

Definition at line 74 of file Date.cc.

References gregorianDate().

Referenced by century(), Core::Timestamp::fromDateAndTime(), and isLeapYear().

00075 {
00076     int m, d, y;
00077     gregorianDate(m, d, y);
00078     return y;
00079 }


Member Data Documentation

unsigned long Core::Date::julian_day_number [protected]
 

Definition at line 38 of file Date.h.

Referenced by addDays(), asDays(), Date(), gregorianDate(), hash(), subtractDays(), and weekday().


The documentation for this class was generated from the following files:
Generated on Mon Nov 27 09:51:18 2006 for Smalltalk like C++ Class Library by  doxygen 1.4.2