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

Core::Timestamp Class Reference

#include <Timestamp.h>

Inheritance diagram for Core::Timestamp:

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

Collaboration graph
[legend]
List of all members.

Public Member Functions

 Timestamp (int month=1, int day=1, int year=1900, int hour=0, int minute=0, int second=0, int millisecond=0)
virtual String * className (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 int hour (void) const
virtual int minute (void) const
virtual int second (void) const
virtual int millisecond (void) const
virtual Timestamp * addDays (int dayCount)
virtual Timestamp * addMonths (int monthCount)
virtual Timestamp * addYears (int yearCount)
virtual Timestamp * addHours (int hours)
virtual Timestamp * addMinutes (int minutes)
virtual Timestamp * addSeconds (int seconds)
virtual Timestamp * addMilliseconds (int millis)
virtual Timestamp * subtractDays (int dayCount)
virtual Timestamp * subtractMonths (int monthCount)
virtual Timestamp * subtractYears (int yearCount)
virtual Timestamp * subtractHours (int hours)
virtual Timestamp * subtractMinutes (int minutes)
virtual Timestamp * subtractSeconds (int seconds)
virtual Timestamp * subtractMilliseconds (int millis)
virtual Time * differenceFrom (Timestamp *stamp)
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 Date * asDate (void) const
 Get the Date portion of the Timestamp.
virtual Time * asTime (void) const
 Get the Time portion of the Timestamp.
virtual time_t asUnixTimestamp (void) const
virtual void printOn (Stream *stream) const
 Print object identification into the stream.
virtual bool isLeapYear (void) const
 Determines if the Timestamp's year is a leap year.

Static Public Member Functions

static Timestamp * now (void)
 Gets the current date and time.
static Timestamp * fromUnixTimestamp (time_t timestamp)
static Timestamp * fromDateAndTime (Date *date, Time *time)

Protected Attributes

Core::Timestamp::_timestamp_t _timestamp

Classes

struct  _timestamp_t

Constructor & Destructor Documentation

Timestamp::Timestamp (  int  month = 1,
int  day = 1,
int  year = 1900,
int  hour = 0,
int  minute = 0,
int  second = 0,
int  millisecond = 0
) 
 

Definition at line 32 of file Timestamp.cc.

References _timestamp, Core::Timestamp::_timestamp_t::day, Core::Timestamp::_timestamp_t::hours, Core::Timestamp::_timestamp_t::milliseconds, Core::Timestamp::_timestamp_t::minutes, Core::Timestamp::_timestamp_t::month, Core::Timestamp::_timestamp_t::seconds, and Core::Timestamp::_timestamp_t::year.

Referenced by fromDateAndTime(), fromUnixTimestamp(), and now().


Member Function Documentation

Timestamp * Timestamp::addDays (  int  dayCount  )  [virtual]
 

Definition at line 131 of file Timestamp.cc.

References asDate(), asTime(), and fromDateAndTime().

Referenced by addHours(), and addMilliseconds().

00132 {
00133     return fromDateAndTime(asDate()->addDays(dayCount), asTime());
00134 }

Timestamp * Timestamp::addHours (  int  hours  )  [virtual]
 

Definition at line 146 of file Timestamp.cc.

References _timestamp, addDays(), asDate(), fromDateAndTime(), Core::Timestamp::_timestamp_t::hours, Core::Timestamp::_timestamp_t::milliseconds, Core::Timestamp::_timestamp_t::minutes, and Core::Timestamp::_timestamp_t::seconds.

00147 {
00148     hours += _timestamp.hours;
00149     int days = hours / 24;
00150     hours %= 24;
00151     Time *time = new Time(hours, _timestamp.minutes, _timestamp.seconds,
00152                           _timestamp.milliseconds);
00153     return fromDateAndTime(asDate()->addDays(days), time);
00154 }

Timestamp * Timestamp::addMilliseconds (  int  millis  )  [virtual]
 

Definition at line 166 of file Timestamp.cc.

References _timestamp, addDays(), asDate(), fromDateAndTime(), Core::Timestamp::_timestamp_t::hours, Core::Timestamp::_timestamp_t::milliseconds, Core::Timestamp::_timestamp_t::minutes, and Core::Timestamp::_timestamp_t::seconds.

Referenced by addSeconds().

00167 {
00168     int days, hours, minutes, seconds;
00169     millis += _timestamp.milliseconds;
00170     seconds = _timestamp.seconds + millis / 1000;
00171     minutes = _timestamp.minutes + seconds / 60;
00172     hours = _timestamp.hours + minutes / 60;
00173     days = hours / 24;
00174     hours = hours % 24;
00175     minutes = minutes % 60;
00176     seconds = seconds % 60;
00177     millis = millis % 1000;
00178     return fromDateAndTime(asDate()->addDays(days),
00179                            new Time(hours, minutes, seconds, millis));
00180 }

Timestamp * Timestamp::addMinutes (  int  minutes  )  [virtual]
 

Definition at line 156 of file Timestamp.cc.

References addSeconds().

00157 {
00158     return addSeconds(minutes * 60);
00159 }

Timestamp * Timestamp::addMonths (  int  monthCount  )  [virtual]
 

Definition at line 136 of file Timestamp.cc.

References asDate(), asTime(), and fromDateAndTime().

00137 {
00138     return fromDateAndTime(asDate()->addMonths(monthCount), asTime());
00139 }

Timestamp * Timestamp::addSeconds (  int  seconds  )  [virtual]
 

Definition at line 161 of file Timestamp.cc.

References addMilliseconds().

Referenced by addMinutes().

00162 {
00163     return addMilliseconds(seconds * 1000);
00164 }

Timestamp * Timestamp::addYears (  int  yearCount  )  [virtual]
 

Definition at line 141 of file Timestamp.cc.

References asDate(), asTime(), and fromDateAndTime().

00142 {
00143     return fromDateAndTime(asDate()->addYears(yearCount), asTime());
00144 }

Date * Timestamp::asDate (  void   )  const [virtual]
 

Get the Date portion of the Timestamp.

Definition at line 281 of file Timestamp.cc.

References _timestamp, Core::Timestamp::_timestamp_t::day, Core::Timestamp::_timestamp_t::month, and Core::Timestamp::_timestamp_t::year.

Referenced by addDays(), addHours(), addMilliseconds(), addMonths(), addYears(), differenceFrom(), printOn(), subtractDays(), subtractHours(), subtractMilliseconds(), subtractMonths(), subtractYears(), and weekday().

00282 {
00283     return new Date(_timestamp.month, _timestamp.day, _timestamp.year);
00284 }

Time * Timestamp::asTime (  void   )  const [virtual]
 

Get the Time portion of the Timestamp.

Definition at line 287 of file Timestamp.cc.

References _timestamp, Core::Timestamp::_timestamp_t::hours, Core::Timestamp::_timestamp_t::milliseconds, Core::Timestamp::_timestamp_t::minutes, and Core::Timestamp::_timestamp_t::seconds.

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

00288 {
00289     return new Time(_timestamp.hours, _timestamp.minutes, _timestamp.seconds,
00290                     _timestamp.milliseconds);
00291 }

time_t Timestamp::asUnixTimestamp (  void   )  const [virtual]
 

Definition at line 293 of file Timestamp.cc.

References _timestamp, Core::Timestamp::_timestamp_t::day, Core::Timestamp::_timestamp_t::hours, Core::Timestamp::_timestamp_t::minutes, Core::Timestamp::_timestamp_t::month, Core::Timestamp::_timestamp_t::seconds, and Core::Timestamp::_timestamp_t::year.

00294 {
00295     struct tm time;
00296 
00297     time.tm_sec = _timestamp.seconds;
00298     time.tm_min = _timestamp.minutes;
00299     time.tm_hour = _timestamp.hours;
00300     time.tm_mday = _timestamp.day;
00301     time.tm_mon = _timestamp.month - 1;
00302     time.tm_year = _timestamp.year - 1900;
00303     return mktime(&time);
00304 }

int Timestamp::century (  void   )  const [virtual]
 

Answer the century.

Definition at line 105 of file Timestamp.cc.

References year().

00106 {
00107     return year() / 100;
00108 }

String * Timestamp::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 45 of file Timestamp.cc.

Referenced by isEqual().

00046 {
00047     return new String("Timestamp");
00048 }

int Timestamp::day (  void   )  const [virtual]
 

Answer the day of the month.

Definition at line 87 of file Timestamp.cc.

References _timestamp, and Core::Timestamp::_timestamp_t::day.

00088 {
00089     return _timestamp.day;
00090 }

Time * Timestamp::differenceFrom (  Timestamp *  stamp  )  [virtual]
 

Definition at line 245 of file Timestamp.cc.

References asDate(), and asTime().

00246 {
00247     int days = asDate()->dayDifferenceFrom(stamp->asDate());
00248     return asTime()->differenceFrom(stamp->asTime())->addHours(days * 24);
00249 }

Timestamp * Timestamp::fromDateAndTime (  Date *  date,
Time *  time
)  [static]
 

Definition at line 72 of file Timestamp.cc.

References Core::Date::day(), Core::Time::hour(), Core::Time::millisecond(), Core::Time::minute(), Core::Date::month(), Core::Time::second(), Timestamp(), and Core::Date::year().

Referenced by addDays(), addHours(), addMilliseconds(), addMonths(), addYears(), subtractDays(), subtractHours(), subtractMilliseconds(), subtractMonths(), and subtractYears().

00073 {
00074     return new Timestamp(date->month(), date->day(), date->year(),
00075                          time->hour(), time->minute(), time->second(),
00076                          time->millisecond());
00077 }

Timestamp * Timestamp::fromUnixTimestamp (  time_t  timestamp  )  [static]
 

Definition at line 65 of file Timestamp.cc.

References Timestamp().

00066 {
00067     const struct tm* time = localtime(&timestamp);
00068     return new Timestamp(time->tm_mon+1, time->tm_mday, time->tm_year+1900,
00069                          time->tm_hour, time->tm_min, time->tm_sec);
00070 }

long Timestamp::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 252 of file Timestamp.cc.

References _timestamp, Core::Timestamp::_timestamp_t::day, Core::Timestamp::_timestamp_t::hours, Core::Timestamp::_timestamp_t::milliseconds, Core::Timestamp::_timestamp_t::minutes, Core::Timestamp::_timestamp_t::month, Core::Timestamp::_timestamp_t::seconds, and Core::Timestamp::_timestamp_t::year.

00253 {
00254     unsigned long hashVal, mon_year;
00255     mon_year = _timestamp.month;
00256     mon_year = _timestamp.year << 4;
00257     hashVal = _timestamp.milliseconds;
00258     hashVal += _timestamp.seconds << 10;
00259     hashVal += _timestamp.minutes << 16;
00260     hashVal += _timestamp.hours << 22;
00261     hashVal += _timestamp.day << 27;
00262     return (long) (hashVal ^ mon_year);
00263 }

int Timestamp::hour (  void   )  const [virtual]
 

Definition at line 110 of file Timestamp.cc.

References _timestamp, and Core::Timestamp::_timestamp_t::hours.

00111 {
00112     return _timestamp.hours;
00113 }

virtual bool Core::Timestamp::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 99 of file Timestamp.h.

References Core::Object::isEqual().

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

bool Timestamp::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 265 of file Timestamp.cc.

References _timestamp, className(), Core::Timestamp::_timestamp_t::day, Core::Timestamp::_timestamp_t::hours, Core::Timestamp::_timestamp_t::milliseconds, Core::Timestamp::_timestamp_t::minutes, Core::Timestamp::_timestamp_t::month, Core::Timestamp::_timestamp_t::seconds, and Core::Timestamp::_timestamp_t::year.

00266 {
00267     if (!object->className()->isEqual(className())) return false;
00268 
00269     const Timestamp *t = dynamic_cast<const Timestamp *>(object);
00270     return t->_timestamp.year == _timestamp.year &&
00271            t->_timestamp.month == _timestamp.month &&
00272            t->_timestamp.day == _timestamp.day &&
00273            t->_timestamp.hours == _timestamp.hours &&
00274            t->_timestamp.minutes == _timestamp.minutes &&
00275            t->_timestamp.seconds == _timestamp.seconds &&
00276            t->_timestamp.milliseconds == _timestamp.milliseconds;
00277 }

bool Timestamp::isLeapYear (  void   )  const [virtual]
 

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

Definition at line 316 of file Timestamp.cc.

References _timestamp, Core::Date::isLeapYear(), and Core::Timestamp::_timestamp_t::year.

00317 {
00318     return Date::isLeapYear(_timestamp.year);
00319 }

int Timestamp::millisecond (  void   )  const [virtual]
 

Definition at line 125 of file Timestamp.cc.

References _timestamp, and Core::Timestamp::_timestamp_t::milliseconds.

00126 {
00127     return _timestamp.milliseconds;
00128 }

int Timestamp::minute (  void   )  const [virtual]
 

Definition at line 115 of file Timestamp.cc.

References _timestamp, and Core::Timestamp::_timestamp_t::minutes.

00116 {
00117     return _timestamp.minutes;
00118 }

int Timestamp::month (  void   )  const [virtual]
 

Answer the month.

This is a number between 1 and 12.

Definition at line 81 of file Timestamp.cc.

References _timestamp, and Core::Timestamp::_timestamp_t::month.

00082 {
00083     return _timestamp.month;
00084 }

Timestamp * Timestamp::now (  void   )  [static]
 

Gets the current date and time.

Definition at line 52 of file Timestamp.cc.

References Timestamp().

00053 {
00054     struct timeval timestamp;
00055     struct timezone tz;
00056     Timestamp *inst;
00057 
00058     gettimeofday(&timestamp, &tz);
00059     const struct tm* time = localtime(&timestamp.tv_sec);
00060     return new Timestamp(time->tm_mon+1, time->tm_mday, time->tm_year+1900,
00061                          time->tm_hour, time->tm_min, time->tm_sec,
00062                          timestamp.tv_usec / 1000);
00063 }

void Timestamp::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 307 of file Timestamp.cc.

References asDate(), asTime(), and Core::Stream::space().

00308 {
00309     asDate()->printOn(stream);
00310     stream->space();
00311     asTime()->printOn(stream);
00312 }

int Timestamp::second (  void   )  const [virtual]
 

Definition at line 120 of file Timestamp.cc.

References _timestamp, and Core::Timestamp::_timestamp_t::seconds.

00121 {
00122     return _timestamp.seconds;
00123 }

Timestamp * Timestamp::subtractDays (  int  dayCount  )  [virtual]
 

Definition at line 182 of file Timestamp.cc.

References asDate(), asTime(), and fromDateAndTime().

Referenced by subtractHours(), and subtractMilliseconds().

00183 {
00184     return fromDateAndTime(asDate()->subtractDays(dayCount), asTime());
00185 }

Timestamp * Timestamp::subtractHours (  int  hours  )  [virtual]
 

Definition at line 197 of file Timestamp.cc.

References _timestamp, asDate(), fromDateAndTime(), Core::Timestamp::_timestamp_t::hours, Core::Timestamp::_timestamp_t::milliseconds, Core::Timestamp::_timestamp_t::minutes, Core::Timestamp::_timestamp_t::seconds, and subtractDays().

00198 {
00199     hours = _timestamp.hours - hours;
00200     int days = 0;
00201     while (hours < 0) {
00202         days++; hours += 24;
00203     }
00204     Time *time = new Time(hours, _timestamp.minutes, _timestamp.seconds,
00205                           _timestamp.milliseconds);
00206     return fromDateAndTime(asDate()->subtractDays(days), time);
00207 }

Timestamp * Timestamp::subtractMilliseconds (  int  millis  )  [virtual]
 

Definition at line 219 of file Timestamp.cc.

References _timestamp, asDate(), fromDateAndTime(), Core::Timestamp::_timestamp_t::hours, Core::Timestamp::_timestamp_t::milliseconds, Core::Timestamp::_timestamp_t::minutes, Core::Timestamp::_timestamp_t::seconds, and subtractDays().

Referenced by subtractSeconds().

00220 {
00221     int seconds = millis / 1000;
00222     millis = _timestamp.milliseconds - millis % 1000;
00223     while (millis < 0) {
00224         seconds++; millis += 1000;
00225     }
00226     int minutes = seconds / 60;
00227     seconds = _timestamp.seconds - seconds % 60;
00228     while (seconds < 0) {
00229         minutes++; seconds += 60;
00230     }
00231     int hours = minutes / 60;
00232     minutes = _timestamp.minutes - minutes % 60;
00233     while (minutes < 0) {
00234         hours++; minutes += 60;
00235     }
00236     int days = hours / 24;
00237     hours = _timestamp.hours - hours % 24;
00238     while (hours < 0) {
00239         days++; hours += 24;
00240     }
00241     return fromDateAndTime(asDate()->subtractDays(days),
00242                            new Time(hours, minutes, seconds, millis));
00243 }

Timestamp * Timestamp::subtractMinutes (  int  minutes  )  [virtual]
 

Definition at line 209 of file Timestamp.cc.

References subtractSeconds().

00210 {
00211     return subtractSeconds(minutes * 60);
00212 }

Timestamp * Timestamp::subtractMonths (  int  monthCount  )  [virtual]
 

Definition at line 187 of file Timestamp.cc.

References asDate(), asTime(), and fromDateAndTime().

00188 {
00189     return fromDateAndTime(asDate()->subtractMonths(monthCount), asTime());
00190 }

Timestamp * Timestamp::subtractSeconds (  int  seconds  )  [virtual]
 

Definition at line 214 of file Timestamp.cc.

References subtractMilliseconds().

Referenced by subtractMinutes().

00215 {
00216     return subtractMilliseconds(seconds * 1000);
00217 }

Timestamp * Timestamp::subtractYears (  int  yearCount  )  [virtual]
 

Definition at line 192 of file Timestamp.cc.

References asDate(), asTime(), and fromDateAndTime().

00193 {
00194     return fromDateAndTime(asDate()->subtractYears(yearCount), asTime());
00195 }

int Timestamp::weekday (  void   )  const [virtual]
 

Answer the weekday.

1 for Monday, 7 for Sunday

Definition at line 99 of file Timestamp.cc.

References asDate().

00100 {
00101     return asDate()->weekday();
00102 }

int Timestamp::year (  void   )  const [virtual]
 

Answer the year.

Definition at line 93 of file Timestamp.cc.

References _timestamp, and Core::Timestamp::_timestamp_t::year.

Referenced by century().

00094 {
00095     return _timestamp.year;
00096 }


Member Data Documentation

struct Core::Timestamp::_timestamp_t Core::Timestamp::_timestamp [protected]
 

Referenced by addHours(), addMilliseconds(), asDate(), asTime(), asUnixTimestamp(), day(), hash(), hour(), isEqual(), isLeapYear(), millisecond(), minute(), month(), second(), subtractHours(), subtractMilliseconds(), Timestamp(), and year().


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